简体   繁体   中英

i want to AnimateWindow() in pywin32 module

I want to do what the following code does in Python using a certain function AnimateWindow() (from Api Windows functions)

var
   hWind: HWND;

begin
   Label1.Caption := 'function: AnimateWindow';
   Label1.Font.Size := 15;

   hWind := Form1.Handle;
   If (AnimateWindow(hWind, 1500, AW_HIDE) = True) Then
       Edit1.Text := 'Done'
   Else
   Begin
       Edit1.Text := 'ERROR';
       Edit2.Text := IntToStr(GetLastError());
   End;
end;

I tried the following code which didn't work

import win32gui


hwnd = win32gui.FindWindow(None, "Untitled - Notepad")
win32gui.AnimateWindow(hwnd, 1500,0x00010000, True)
bbox = win32gui.GetWindowRect(hwnd)

Problems:

  1. This is not a [SO]: How to create a Minimal, Reproducible Example (reprex (mcve)) (doesn't contain expected and actual outputs), so people have to guess what the problem is

  2. [ME.TimGolden]: win32gui.AnimateWindow (part of [GitHub]: mhammond/pywin32 - Python for Windows (pywin32) Extensions ) only takes 3 arguments, but you're passing 4

  3. According to (wrapped) [MS.Learn]: AnimateWindow function (winuser.h) ( emphasis is mine):

    [in] hWnd

    Type: HWND

    A handle to the window to animate. The calling thread must own this window .

    So, this can be done on windows that are owned by your (thread or) by your process (as opposed to other functions (like ShowWindow ) that work with any windows (if user has enough privileges)).

    I'm sure that your Notepad windows doesn't meet the criteria, at it was most likely started independently from your script.

I prepared a small example.

code00.py :

#!/usr/bin/env python

import sys
import traceback as tb

import pywintypes as pwts
import win32api as wapi
import win32con as wcon
import win32gui as wgui


def main(*argv):
    if argv:  # Pass (at least) an argument when launching the script
        print("Create window")
        title = "Dummy Title"
        res = wgui.CreateWindow("SysListView32", title, 0, 1500, 700, 160, 90, None, None, None, None)
        print("CreateWindow returned {:}".format(res))
    else:
        title = "Untitled - Notepad"
    hwnd = wgui.FindWindow(None, title)
    if not hwnd:
        print("No window found")
        return -1
    print("Window {:} (title: {:s}) original rect: {:}".format(hwnd, title, wgui.GetWindowRect(hwnd)))
    flags = wcon.AW_CENTER
    print("Start animation")
    try:
        wgui.AnimateWindow(hwnd, 1500, flags)
    except pwts.error as e:
        tb.print_exc()
        return -1
    print("Animation done")
    print("Window final rect: {:}".format(wgui.GetWindowRect(hwnd)))
    input("Press <Enter> to exit... ")


if __name__ == "__main__":
    print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(*sys.argv[1:])
    print("\nDone.\n")
    sys.exit(rc)

Output :

 [cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q074968476]> "e:\Work\Dev\VEnvs\py_pc064_03.10_test0\Scripts\python.exe" code00.py Python 3.10.9 (tags/v3.10.9:1dd9be6, Dec 6 2022, 20:01:21) [MSC v.1934 64 bit (AMD64)] 064bit on win32 Window 2623562 (title: Untitled - Notepad) original rect: (146, 161, 1834, 920) Start animation Traceback (most recent call last): File "e:\Work\Dev\StackOverflow\q074968476\code00.py", line 28, in main wgui.AnimateWindow(hwnd, 1500, flags) pywintypes.error: (0, 'AnimateWindow', 'No error message is available') Done. [cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q074968476]> "e:\Work\Dev\VEnvs\py_pc064_03.10_test0\Scripts\python.exe" code00.py 1 Python 3.10.9 (tags/v3.10.9:1dd9be6, Dec 6 2022, 20:01:21) [MSC v.1934 64 bit (AMD64)] 064bit on win32 Create window CreateWindow returned 1183484 Window 1183484 (title: Dummy Title) original rect: (1500, 700, 1660, 790) Start animation Animation done Window final rect: (1500, 700, 1660, 790) Press <Enter> to exit... Done.

And a screenshot took during the 2 nd run, while the window is expanding ("growing"):

图像0

thanks for reply

my output:

 Window 1312618 (title: Untitled - Notepad) original rect: (395, 182, 1019, 726)
Start animation

Traceback (most recent call last):
  File "C:\Users\XXXXXX\Desktop\222.py", line 28, in main
    wgui.AnimateWindow(hwnd, 1500, flags)
pywintypes.error: (0, 'AnimateWindow', 'No error message is available')

Done.

What do you think is the problem?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM