Win32-Api und Python3

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Schlaaaange
User
Beiträge: 34
Registriert: Sonntag 5. Januar 2020, 18:05

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!
Schlaaaange
User
Beiträge: 34
Registriert: Sonntag 5. Januar 2020, 18:05

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!
Benutzeravatar
__blackjack__
User
Beiträge: 13938
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

@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()
“Java is a DSL to transform big Xml documents into long exception stack traces.”
— Scott Bellware
Schlaaaange
User
Beiträge: 34
Registriert: Sonntag 5. Januar 2020, 18:05

Danke für Verbesserung!

Jetzt sieht das ja so schön professionell aus.
Antworten