[Python-au] win32gui question

jt1masters at optusnet.com.au jt1masters at optusnet.com.au
Mon Apr 18 03:58:27 CEST 2005


Hi all, 

I'm new to the list, and new to python for that matter.
I have a question regarding the win32gui module/win32 extensions.

My current task is to investigate the possibility of automated gui testing for one
of our products using python, specifically WATSUP (Windows Application Test System
Using Python)

A concern was raised that edit boxes would not be able to be distinguished because
it was not possible to name an edit box without the name appearing in the edit box.
Writing a small MFC app with a single richedit box, I was able to name the edit box
using python by calling win32gui.SetWindowText() and it would not show up in the
box, and I was able to verify the caption of the edit box using Spy++.

I then tried to achieve the same result with MFC code using the
CWnd::SetWindowText() but for some reason this behaved differently. The text
appeared in the box, and a caption was not set.

I then used Spy++ to check the messages being sent for each operation, and as far as
I can tell, win32gui.SetWindowText doesn't send any messages, but achieves the
desired result. 

What I'd like to know is: What is win32gui.SetWindowText actually doing so we can do
the same thing in the MFC code so that  the gui will have all the controls named on
startup?

Below is the python code I used to test this, the source for setEditText(minus the
commenting), the MFC code I used and the Spy++ output I got when running the python
code and the mfc code.

Any help would be most appreciated
Thanks

Justin

----------------------------------------------------------------
Python code

form=findTopWindow(wantedText='MFCTestProgram',wantedClass='#32770')  	#from winGuiAuto
edit=findControl(form,wantedClass='RICHEDIT')			      	#from winGuiAuto
setEditText(edit, 'Calling SetWindowText on next line')		      	#from winGuiAuto
win32gui.SetWindowText(edit, 'BondEditBox')
setEditText(edit, 'SetWindowText was called')				#from winGuiAuto

----------------------------------------------------------------
setEditText()

def setEditText(hwnd, text, append=False):
    
    # Ensure that text is a list        
    try:
        text + ''
        text = [text]
    except TypeError:
        pass

    # Set the current selection range, depending on append flag
    if append:
        win32gui.SendMessage(hwnd,
                             win32con.EM_SETSEL,
                             -1,
                             0)
    else:
        win32gui.SendMessage(hwnd,
                             win32con.EM_SETSEL,
                             0,
                             -1)
                             
    # Send the text
    win32gui.SendMessage(hwnd,
                         win32con.EM_REPLACESEL,
                         True,
                         os.linesep.join(text))

----------------------------------------------------------------
MFC Code

CWnd* pWnd = GetDlgItem(IDC_RICHEDIT1);
pWnd->SetWindowText(_T("ButtonClicked"));

----------------------------------------------------------------
Spy++ Output

PYTHON

<00001> 000C0370 S EM_SETSEL nStart:0 nEnd:-1 (select all text)
<00002> 000C0370 R EM_SETSEL 
<00003> 000C0370 S EM_REPLACESEL lpszReplace:0012FD0C("Calling SetWindowText on next
line")
<00004> 000C0370 R EM_REPLACESEL 
<00005> 000C0370 P WM_PAINT hdc:00000000
<00006> 000C0370 S EM_SETSEL nStart:0 nEnd:-1 (select all text)
<00007> 000C0370 R EM_SETSEL 
<00008> 000C0370 S EM_REPLACESEL lpszReplace:0012FD14("SetWindowText was called")
<00009> 000C0370 R EM_REPLACESEL 
<00010> 000C0370 P WM_PAINT hdc:00000000

MFC

<00023> 000C0370 S WM_GETDLGCODE
<00024> 000C0370 R WM_GETDLGCODE
fuDlgCode:DLGC_WANTARROWS|DLGC_WANTTAB|DLGC_HASSETSEL|DLGC_WANTCHARS
<00025> 000C0370 S .WM_SETTEXT lpsz:00415108("ButtonClicked")
<00026> 000C0370 R .WM_SETTEXT fSucceeded:True
<00027> 000C0370 P WM_PAINT hdc:00000000






More information about the python-au mailing list