Python Forum
Control click to minimized window - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Control click to minimized window (/thread-11710.html)



Control click to minimized window - abdulrahmanok - Jul-22-2018

Hi Brothers,
I'm Struggling with send mouse click to minimized (Calc.exe) to specific area
in autoit I was just need to get handle window and then use control send to it
so I tried with python :

hwnd = win32gui.FindWindow(None, "Calc")

but I coudn't any similar Func in python is there is any way to click on minimized window in python?



RE: Control click to minimized window - Larz60+ - Jul-22-2018

show a large enough snippet of code that we can run it.


RE: Control click to minimized window - abdulrahmanok - Jul-22-2018

Thanks for response,
This is One of my tries:
import win32gui
import win32con
import win32api
from time import sleep
import win32gui
import win32api
import win32con

hwndMain = win32gui.FindWindow(None, "Calculator")

while(True):
    temp = win32api.PostMessage(hwndChild, win32con.WM_CHAR, 0x44, 0)
    lParam = win32api.MAKELONG(501, 139)
    win32api.SendMessage(hwndChild, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
    win32api.SendMessage(hwndChild, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, lParam)
    #print(temp) prints the returned value of temp, into the console
    print(temp)
    #sleep(1) this waits 1 second before looping through again
    sleep(1)



RE: Control click to minimized window - Larz60+ - Jul-23-2018

1. you don't need to import twice.
2. Are you a C programmer? This looks a lot like old C code that was converted directly to python.
there are much easier and better ways to do this with native code.
write exactly what you are trying to achieve.
There's a better way to write it.


RE: Control click to minimized window - abdulrahmanok - Jul-24-2018

No I'm not a C programmer and I found this code is some forums and modified some data.
And Sorry I cant provide full code like this in Python.

I got idea that to get screenshot of minimized window and then search inside captured minimized window About the button i want.
So I tried with this code:
import win32gui
import win32ui
from ctypes import windll
from  PIL import Image

hwnd = win32gui.FindWindow(None, 'Calculator')

# Change the line below depending on whether you want the whole window
# or just the client area. 
#left, top, right, bot = win32gui.GetClientRect(hwnd)
left, top, right, bot = win32gui.GetWindowRect(hwnd)
w = right - left
h = bot - top

hwndDC = win32gui.GetWindowDC(hwnd)
mfcDC  = win32ui.CreateDCFromHandle(hwndDC)
saveDC = mfcDC.CreateCompatibleDC()

saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)

saveDC.SelectObject(saveBitMap)

# Change the line below depending on whether you want the whole window
# or just the client area. 
#result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 1)
result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0)
print (result)

bmpinfo = saveBitMap.GetInfo()
bmpstr = saveBitMap.GetBitmapBits(True)

im = Image.frombuffer(
    'RGB',
    (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
    bmpstr, 'raw', 'BGRX', 0, 1)

win32gui.DeleteObject(saveBitMap.GetHandle())
saveDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(hwnd, hwndDC)

if result == 1:
    #PrintWindow Succeeded
    im.save("test.png")
it's successfully capture calculator but win I press (minimize) from Calculator application and try to take screenshot window again it gains only top of calculator window....