Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I want to get the status bar text of a window! I'm using win32gui.GetWindowText , but I can't get the status bar text. I just get the title! How can I get the status bar text?

#coding=utf-8
import win32gui
# get main window handle
f = win32gui.FindWindow("TMDIForm",None)
print f,win32gui.GetWindowText(f)
#get child window handle of main window
ex=win32gui.FindWindowEx(f,None,"TPanel",None)
#get child window handle of ex window
exx=win32gui.FindWindowEx(ex,None,"TStatusBar",None)
print exx,win32gui.GetWindowText(exx)
                What application are you trying to do this on? Most status bars contain more than one section. You will possibly need to investigate msctls_statusbar32.
– Martin Evans
                Sep 20, 2015 at 14:45
                The class of the status bar is "TStatusBar" and  I have get the handle of the statusbar  use SPY++ .but i can't get the text
– 李凤春
                Sep 21, 2015 at 13:07

The following should help, you cannot use GetWindowText on a status bar. A status bar usually consists of multiple sub items. To access these use need to use SendMessage with SB_GETTEXT.

#coding=utf-8
import win32gui
import win32api
import win32con
# get main window handle
f = win32gui.FindWindow("TMDIForm",None)
print f,win32gui.GetWindowText(f)
#get child window handle of main window
ex=win32gui.FindWindowEx(f,None,"TPanel",None)
#get child window handle of ex window
exx=win32gui.FindWindowEx(ex,None,"TStatusBar",None)
SB_GETTEXT = win32con.WM_USER + 2
SB_GETTEXTLENGTH = win32con.WM_USER + 3
sub_item = 0
sb_retcode = win32api.SendMessage(exx, SB_GETTEXTLENGTH, sub_item, 0)
sb_type = sb_retcode & 0xFFFF
sb_length = (sb_retcode >> 16) & 0xFFFF
text_buffer = win32gui.PyMakeBuffer(1 + sb_length)
sb_retcode = win32api.SendMessage(exx, SB_GETTEXT, sub_item, text_buffer)
print text_buffer

I have not been able to test this, as I was not able to find a suitable Window.

If the target window is owned by the current process, GetWindowText causes a WM_GETTEXT message to be sent to the specified window or control. If the target window is owned by another process and has a caption, GetWindowText retrieves the window caption text. If the window does not have a caption, the return value is a null string. This behavior is by design. It allows applications to call GetWindowText without becoming unresponsive if the process that owns the target window is not responding. However, if the target window is not responding and it belongs to the calling application, GetWindowText will cause the calling application to become unresponsive.

To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText.

  • https://www.programcreek.com/python/example/89831/win32gui.GetClassName
  • https://github.com/certsocietegenerale/fame_modules/blob/fb7a6fb34124fa2ae026719a0f16767cab731c6d/processing/cutthecrap/cutthecrap.py#L66

    # hwnd = your TStatusBar or TToolBar or anything
    buffer_len = win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 0, 0) + 1
    text = array('b', b'\x00\x00' * buffer_len)
    text_len = win32gui.SendMessage(hwnd, win32con.WM_GETTEXT, buffer_len, text)
    text = win32gui.PyGetString(text.buffer_info()[0], buffer_len - 1)
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.