Seite 1 von 1

Win32-Api und Python3

Verfasst: Sonntag 17. Januar 2021, 18:59
von Schlaaaange
Hallo liebe Freunde der Python-Programmiersprache,

ich experimentiere schon ganz schön mit Python rum.
Mit tkinter komme ich mittlerweile auch ganz gut klar.

Ich probiere jetzt etwas mit der Win32-Api unter Windows10 herum.
Dafür habe ich mir "win32gui" installiert.

Wie bekomme ich folgendes Script unter Windows 10 zum laufen?
(Was mache ich falsch?)

//Zeichnen hinter den Desktop-Icons

Code: Alles auswählen

from win32gui import *
import win32.lib.win32con as win32con

global hWorkerW
hWorkerW = 0

def EnumProcedure(hWnd, Parameter):
    h = FindWindowEx(hWnd, 0, "SHELLDLL_DefView", None)
    print(h)
    if h != 0 :
        print("Alles Gut!")
        hWorker = FindWindowEx(0, hWnd, "WorkerW", None)
        if hWorker != 0 :
            hWorkerW = hWorker
    return 1

def Test():
    hWnd = FindWindowEx(0, 0, "Progman", None)
    print(int(hWnd))
    if SendMessageTimeout(hWnd, win32con.WM_NULL, 0, 0, win32con.SMTO_ABORTIFHUNG, 1000):
        if SendMessageTimeout(hWnd, 0x52C, 0, 0, win32con.SMTO_NORMAL, 1000) == 0 :
            print("Error!")
        else:
            print("hier!!!")
            EnumWindows(EnumProcedure, None)
            if hWorkerW != 0 :
                hdc = GetDCEx(hWorkerW, 0, 0x403)
                hBrush = CreateSolidBrush(GetSysColor(RGB(255,0,0)))
                (left, top, right, bottom) = GetClientRect(hWorkerW)
                FillRect(hdc, (left, top, right, bottom), hBrush)
                ReleaseDC(hWorkerW, hdc)

Test()
Bei print(h) bekomme ich immer 0, aber ich verstehe nicht warum.
Danke!

Re: Win32-Api und Python3

Verfasst: Sonntag 17. Januar 2021, 19:39
von Schlaaaange
Ich war zu doof zum programmieren mit Python.
Ich habe das "global" verhunst.

So geht es:

Code: Alles auswählen

from win32gui import *
import win32api, win32con

hWorkerW = 0

def EnumProcedure(hWnd, Parameter):
    global hWorkerW
    h = FindWindowEx(hWnd, 0, "SHELLDLL_DefView", None)
    print(h)
    if h != 0 :
        print("Alles Gut!")
        hWorker = FindWindowEx(0, hWnd, "WorkerW", None)
        if hWorker != 0 :
            hWorkerW = hWorker
    return 1


def Test():
    global hWorkerW
    hWnd = FindWindowEx(0, 0, "Progman", None)
    print(int(hWnd))
    if SendMessageTimeout(hWnd, 0, 0, 0, win32con.SMTO_ABORTIFHUNG, 1000):
        if SendMessageTimeout(hWnd, 0x52C, 0, 0, win32con.SMTO_NORMAL, 1000) == 0 :
            print("Error!")
        else:
            print("hier!!!")
            EnumWindows(EnumProcedure, None)
            print("Worker", hWorkerW)
            if hWorkerW != 0 :
                hdc = GetDC(hWorkerW)
                hBrush = CreateSolidBrush(win32api.RGB(255,255,0))
                (left, top, right, bottom) = GetClientRect(hWorkerW)
                print(right)
                FillRect(hdc, (left, top, right, bottom), hBrush)
                ReleaseDC(hWorkerW, hdc)

Test()
Aber warum gibt es eigentlich noch keine GetDCEx - Funktion?
Naja, egal!

Re: Win32-Api und Python3

Verfasst: Sonntag 17. Januar 2021, 21:14
von __blackjack__
@Schlaaaange: Das verhunzte an ``global`` ist, dass Du das überhaupt verwendest. Das macht man nicht.

Ungetestet:

Code: Alles auswählen

import win32api
import win32con
from win32gui import *


def enum_windows_callback(hWnd, results):
    if FindWindowEx(hWnd, 0, "SHELLDLL_DefView", None):
        worker = FindWindowEx(0, hWnd, "WorkerW", None)
        if worker:
            results.append(worker)
    return True


def test():
    hWnd = FindWindowEx(0, 0, "Progman", None)
    print(int(hWnd))
    if SendMessageTimeout(hWnd, 0, 0, 0, win32con.SMTO_ABORTIFHUNG, 1000):
        if not SendMessageTimeout(
            hWnd, 0x52C, 0, 0, win32con.SMTO_NORMAL, 1000
        ):
            print("Error!")
        else:
            worker_handles = []
            EnumWindows(enum_windows_callback, worker_handles)
            hWorkerW = worker_handles.pop()
            print("Worker", hWorkerW)
    hWnd = FindWindowEx(0, 0, "Progman", None)
    print(int(hWnd))
    if SendMessageTimeout(hWnd, 0, 0, 0, win32con.SMTO_ABORTIFHUNG, 1000):
        if not SendMessageTimeout(
            hWnd, 0x52C, 0, 0, win32con.SMTO_NORMAL, 1000
        ):
            print("Error!")
        else:
            worker_handles = []
            EnumWindows(enum_windows_callback, worker_handles)
            hWorkerW = worker_handles.pop()
            print("Worker", hWorkerW)
            hdc = GetDC(hWorkerW)
            try:
                FillRect(
                    hdc,
                    GetClientRect(hWorkerW),
                    CreateSolidBrush(win32api.RGB(255, 255, 0)),
                )
            finally:
                ReleaseDC(hWorkerW, hdc)


if __name__ == "__main__":
    test()

Re: Win32-Api und Python3

Verfasst: Sonntag 17. Januar 2021, 21:25
von Schlaaaange
Danke für Verbesserung!

Jetzt sieht das ja so schön professionell aus.