This forum is in archive mode. You will not be able to post new content.

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Lenoch

Pages: [1]
1
C - C++ / [C++] making some screenshots with gdiplus
« on: December 12, 2015, 04:21:13 PM »
Hello you guys. I'm working a bit on improving my C++ so I decided to mess around with screenshots. it's windows based an uses gdiplus and windows api. It gets your window with the windows api then handles the bitmap that it generates with gdiplus and saves it to a file.

The screenshot header file
Code: [Select]
#include <Windows.h>
#include <gdiplus.h>


#pragma comment(lib, "gdiplus.lib")
#pragma once

class Screenshot
{
public:
    Screenshot();
    ~Screenshot();
    void makeScreenshot(const WCHAR*);
private:
    ULONG_PTR token;
    int GetEncoderClsid(const WCHAR*, CLSID*);
};

The screenshot class

Code: [Select]
#include "Screenshot.h"

ULONG_PTR gdiToken;

Screenshot::Screenshot()
{
    Gdiplus::GdiplusStartupInput StartupInput;
    Gdiplus::GdiplusStartup(&gdiToken, &StartupInput, NULL);
}


Screenshot::~Screenshot()
{
    Gdiplus::GdiplusShutdown(gdiToken);
}

int Screenshot::GetEncoderClsid(const WCHAR* format, CLSID* pClsid) {
    UINT num = 0;
    UINT size = 0;

    Gdiplus::ImageCodecInfo* pImageCodecInfo = NULL;
    Gdiplus::GetImageEncodersSize(&num, &size);
   
    if (size == 0)
        return -1;

    pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size));

    if (pImageCodecInfo == NULL)
        return -1;

    Gdiplus::GetImageEncoders(num, size, pImageCodecInfo);

    for (UINT codec = 0; codec < num; ++codec)
    {
        if (wcscmp(pImageCodecInfo[codec].MimeType, format) == 0)
        {
            *pClsid = pImageCodecInfo[codec].Clsid;
            free(pImageCodecInfo);
            return codec;
        }
    }

    free(pImageCodecInfo);
    return -1;
}

void Screenshot::makeScreenshot(const WCHAR* filename) {
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);
    int screenWidth = GetSystemMetrics(SM_CXSCREEN);

    HWND desktopWindow = GetDesktopWindow();

    HDC desktopDC = GetDC(desktopWindow);
    HDC captureDC = CreateCompatibleDC(desktopDC);

    HBITMAP captureBitmap = CreateCompatibleBitmap(desktopDC, screenWidth, screenHeight);
    SelectObject(captureDC, captureBitmap);
    BitBlt(captureDC, 0, 0, screenWidth, screenHeight, desktopDC, 0, 0, SRCCOPY | CAPTUREBLT);

    Gdiplus::Bitmap *bitmap = new Gdiplus::Bitmap(captureBitmap, NULL);
    CLSID clsId;

    int retVal = this->GetEncoderClsid(L"image/png", &clsId);
    bitmap->Save(filename, &clsId, NULL);
   
    ReleaseDC(desktopWindow, desktopDC);
    DeleteDC(captureDC);
    DeleteObject(captureBitmap);
}

3
Scripting Languages / [Ruby] a weather notification script
« on: August 18, 2015, 01:15:54 AM »
A little script I made that fits in my desktop workflow

Code: [Select]
#!/usr/bin/ruby

require 'rubygems'
require 'json'
require 'etc'
require 'net/http'
require 'fileutils'
require 'configparser'

class Weather
  def initialize(location, link)
    @location = location
    @link = link
    @data = get_data
  end

  def kelvin_to_celcius(kelvin)
    ((kelvin - 273.15) * 1).round.to_s
  end

  def get_data
    uri = URI(@link + @location)
    Net::HTTP.get(uri)
  end

  def get_degrees
    json = JSON.parse( @data )
    kelvin_to_celcius(json["main"]["temp_min"].to_i)
  end

  def to_s
    get_degrees + "°C In " + @location
  end
end

conf_dir = '/home/' + ENV['USER'] + '/.weatherdzen/'

unless File.exists? conf_dir
  FileUtils.mkpath conf_dir
  FileUtils.touch conf_dir + 'config'
 
  File.open(conf_dir + 'config', "w") do |f|
    f.puts('[basic]')
    f.puts('location=Antwerp')
    f.puts('notify=notifysend')
    f.close
  end
end

cfg = ConfigParser.new(conf_dir + 'config')

weather = Weather.new(
  cfg["basic"]["location"],
  "http://api.openweathermap.org/data/2.5/weather?q="
)

notifier = cfg["basic"]["notify"]
wtr =  weather.to_s

if notifier == "dzen"
  exec 'echo ' + wtr + '| dzen2 -title-name "brightness" -l 3 -bg "#263238" -fg "#b1fff8" -x 790 -y 480 -w 270 -h 70 -p 3 -e'
elsif notifier == "notifysend"
  exec 'notify-send "' + wtr + '"'
end


Needs  etc and configparser gems + dzen2 installed.

EDIT:autowrite config, autowrite based on users location coming later
EDIT2: for people with xfce and unity and any other DE that uses notify-send you can now use that and configure in the config file

Pages: [1]


Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.