Make a PyQT window bring to the front

from win32gui import SetWindowPos
import win32con

             win32con.HWND_TOPMOST, # = always on top. only reliable way to bring it to the front on windows
             0, 0, 0, 0,
             win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW)
             win32con.HWND_NOTOPMOST, # disable the always on top, but leave window at its top position
             0, 0, 0, 0,
             win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW)


Move and Click Mouse in C#

public static extern void mouse_event(MouseEventFlags dwFlags, uint dx, uint dy, uint dwData, MouseEventDataXButtons dwExtraInfo);

public static extern long SetCursorPos(int x, int y);
public enum MouseEventFlags : uint
    LEFTDOWN = 0x00000002,
    LEFTUP = 0x00000004,
    MIDDLEDOWN = 0x00000020,
    MIDDLEUP = 0x00000040,
    MOVE = 0x00000001,
    ABSOLUTE = 0x00008000,
    RIGHTDOWN = 0x00000008,
    RIGHTUP = 0x00000010,
    WHEEL = 0x00000800,
    XDOWN = 0x00000080,
    XUP = 0x00000100

//Use the values of this enum for the 'dwData' parameter
//to specify an X button when using MouseEventFlags.XDOWN or
//MouseEventFlags.XUP for the dwFlags parameter.
public enum MouseEventDataXButtons : uint
    XBUTTON1 = 0x00000001,
    XBUTTON2 = 0x00000002
public static void MoveMouse(int x, int y)
    PInvoke.SetCursorPos(x, y);

public static void ClickMouse(int x, int y)
    PInvoke.mouse_event(MouseEventFlags.LEFTDOWN | MouseEventFlags.ABSOLUTE, (uint)x, (uint)y, 0, 0);
    PInvoke.mouse_event(MouseEventFlags.LEFTUP | MouseEventFlags.ABSOLUTE, (uint)x, (uint)y, 0, 0);


Take Screenshot in C#

First Set Process DPI Aware:

Get Screen Size on C#

private void takeScreenShot()
    Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    using (Graphics g = Graphics.FromImage(bmp))
        g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
        bmp.Save("screenshot.png");  // saves the image


Get Screen Size on C#

The process is running under DPI virtualization. So we should apply the DPI aware manifest option to stop DPI virtualization

[DllImport("user32.dll", SetLastError=true)]
static extern bool SetProcessDPIAware();


public enum SystemMetric
    VirtualScreenWidth = 78, // CXVIRTUALSCREEN 0x0000004E 
    VirtualScreenHeight = 79, // CYVIRTUALSCREEN 0x0000004F 

public static extern int GetSystemMetrics(SystemMetric metric);

public static Size GetVirtualDisplaySize()
    var width = GetSystemMetrics(SystemMetric.VirtualScreenWidth);
    var height = GetSystemMetrics(SystemMetric.VirtualScreenHeight);

    return new Size(width, height);


Take Screenshot on Windows Using Python

from PIL import ImageGrab
im = ImageGrab.grab()'screenshot.png')


import win32gui, win32ui, win32con, win32api
hwin = win32gui.GetDesktopWindow()
width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
hwindc = win32gui.GetWindowDC(hwin)
srcdc = win32ui.CreateDCFromHandle(hwindc)
memdc = srcdc.CreateCompatibleDC()
bmp = win32ui.CreateBitmap()
bmp.CreateCompatibleBitmap(srcdc, width, height)
memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
bmp.SaveBitmapFile(memdc, 'screenshot.bmp')

Don’t forget to¬†SetProcessDPIAware if necessary

import ctypes

user32 = ctypes.windll.user32