Various added methods to HwndWrapper and documentation string updates to win32_controls.py

This commit is contained in:
markm 2006-04-18 23:22:40 +00:00
parent 95e82a5ed3
commit f7ff045616
2 changed files with 162 additions and 44 deletions

View File

@ -55,7 +55,7 @@ from pywinauto import handleprops
# also import MenuItemNotEnabled so that it is
# accessible from HwndWrapper module
from menuwrapper import Menu, MenuItemNotEnabled
from menuwrapper import Menu #, MenuItemNotEnabled
#====================================================================
class ControlNotEnabled(RuntimeError):
@ -517,7 +517,12 @@ class HwndWrapper(object):
#-----------------------------------------------------------
def SendMessageTimeout(
self, message, wparam = 0 , lparam = 0, timeout = None):
self,
message,
wparam = 0 ,
lparam = 0,
timeout = None,
timeoutflags = win32defines.SMTO_NORMAL):
"""Send a message to the control and wait for it to return or to timeout
If no timeout is given then a default timeout of .4 of a second will
@ -528,8 +533,10 @@ class HwndWrapper(object):
timeout = Timings.sendmessagetimeout_timeout
result = ctypes.c_long()
win32functions.SendMessageTimeout(self, message, wparam, lparam,
win32defines.SMTO_NORMAL, int(timeout * 1000), ctypes.byref(result))
win32functions.SendMessageTimeout(self,
message, wparam, lparam,
timeoutflags, int(timeout * 1000),
ctypes.byref(result))
return result.value
@ -546,26 +553,26 @@ class HwndWrapper(object):
#return result.value
#-----------------------------------------------------------
def NotifyMenuSelect(self, menu_id):
"""Notify the dialog that one of it's menu items was selected
**This method is Deprecated**
"""
import warnings
warning_msg = "HwndWrapper.NotifyMenuSelect() is deprecated - " \
"equivalent functionality is being moved to the MenuWrapper class."
warnings.warn(warning_msg, DeprecationWarning)
self.SetFocus()
msg = win32defines.WM_COMMAND
return self.SendMessageTimeout(
msg,
win32functions.MakeLong(0, menu_id), #wparam
)
# #-----------------------------------------------------------
# def NotifyMenuSelect(self, menu_id):
# """Notify the dialog that one of it's menu items was selected
#
# **This method is Deprecated**
# """
#
# import warnings
# warning_msg = "HwndWrapper.NotifyMenuSelect() is deprecated - " \
# "equivalent functionality is being moved to the MenuWrapper class."
# warnings.warn(warning_msg, DeprecationWarning)
#
# self.SetFocus()
#
# msg = win32defines.WM_COMMAND
# return self.SendMessageTimeout(
# msg,
# win32functions.MakeLong(0, menu_id), #wparam
# )
#
#-----------------------------------------------------------
def NotifyParent(self, message):
@ -744,7 +751,6 @@ class HwndWrapper(object):
"Double click at the specified coordinates"
_perform_click_input(self, button, coords, double = True)
#-----------------------------------------------------------
def RightClick(
self, pressed = "", coords = (0, 0)):
@ -1140,8 +1146,92 @@ class HwndWrapper(object):
#-----------------------------------------------------------
#def Close(self):
# self.SendMessage(win32defines.WM_CLOSE)
def Close(self):
"""Close the window
Code modified from http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/
"""
# tell the window it must close
self.PostMessage(win32defines.WM_CLOSE)
start = time.time()
# Keeps trying while
# we have not timed out and
# window is still a valid handle and
# window is still visible
# any one of these conditions evaluates to false means the window is
# closed
while (
(time.time() - start) < Timings.after_windowclose_timeout and
win32functions.IsWindow(self) and
self.IsVisible()):
time.sleep(min(
Timings.after_windowclose_retry,
Timings.after_windowclose_timeout - (time.time() - start) ))
# # get a handle we can wait on
# process_wait_handle = win32functions.OpenProcess(
# win32defines.SYNCHRONIZE | win32defines.PROCESS_TERMINATE ,
# False ,
# self.ProcessID())
#
# # wait for the window to close
# win32functions.WaitForSingleObject(
# process_wait_handle,
# )
#-----------------------------------------------------------
def Maximize(self):
"""Maximize the window"""
win32functions.ShowWindow(self, win32defines.SW_MAXIMIZE)
#-----------------------------------------------------------
def Minimize(self):
"""Minimize the window"""
win32functions.ShowWindow(self, win32defines.SW_MINIMIZE)
#-----------------------------------------------------------
def Restore(self):
"""Restore the window"""
# do it twice just in case the window was minimized from being
# maximized - because then the window would come up maximized
# after the first ShowWindow, and Restored after the 2nd
win32functions.ShowWindow(self, win32defines.SW_RESTORE)
win32functions.ShowWindow(self, win32defines.SW_RESTORE)
#-----------------------------------------------------------
def GetShowState(self):
"""Get the show state and Maximized/minimzed/restored state
Returns a value that is a union of the following
SW_HIDE the window is hidden.
SW_MAXIMIZE the window is maximized
SW_MINIMIZE the window is minimized
SW_RESTORE the window is in the 'restored'
state (neither minimized or maximized)
SW_SHOW The window is not hidden
"""
wp = win32structures.WINDOWPLACEMENT()
wp.lenght = ctypes.sizeof(wp)
ret = win32functions.GetWindowPlacement(self, ctypes.byref(wp))
if not ret:
raise ctypes.WinError()
return wp.showCmd
#-----------------------------------------------------------
@ -1213,6 +1303,8 @@ class HwndWrapper(object):
"""
self.appdata = appdata
#
#def MouseLeftClick():
# pass

View File

@ -31,7 +31,7 @@ import HwndWrapper
from pywinauto import win32functions
from pywinauto import win32defines
from pywinauto import win32structures
from pywinauto import findbestmatch
#from pywinauto import findbestmatch
from pywinauto import tests
from pywinauto.timings import Timings
@ -56,6 +56,7 @@ class ButtonWrapper(HwndWrapper.HwndWrapper):
def _set_if_needs_image(self, value):
"Does nothing see _get_if_needs_image"
pass
#-----------------------------------------------------------
def _get_if_needs_image(self):
@ -84,10 +85,10 @@ class ButtonWrapper(HwndWrapper.HwndWrapper):
controls based on their style. They can look like the following
controls:
- Buttons
- CheckBoxes
- RadioButtons
- GroupBoxes
- Buttons, this method returns "Button"
- CheckBoxes, this method returns "CheckBox"
- RadioButtons, this method returns "RadioButton"
- GroupBoxes, this method returns "GroupBox"
"""
# get the least significant bit
@ -116,7 +117,18 @@ class ButtonWrapper(HwndWrapper.HwndWrapper):
#-----------------------------------------------------------
def GetCheckState(self):
"Return the check state of the checkbox"
"""Return the check state of the checkbox
The check state is represented by an integer
0 - unchecked
1 - checked
2 - indeterminate
The following constants are defined in the win32defines module
BST_UNCHECKED = 0
BST_CHECKED = 1
BST_INDETERMINATE = 2
"""
return self.SendMessage(win32defines.BM_GETCHECK)
#-----------------------------------------------------------
@ -156,16 +168,18 @@ class ButtonWrapper(HwndWrapper.HwndWrapper):
# return this control so that actions can be chained.
return self
#-----------------------------------------------------------
def IsDialog(self):
"Buttons are never dialogs so return False"
return False
#-----------------------------------------------------------
def Click(self):
def Click(self, *args, **kwargs):
"Click the Button control"
# import win32functions
# win32functions.WaitGuiThreadIdle(self)
# self.NotifyParent(win32defines.BN_CLICKED)
HwndWrapper.HwndWrapper.Click(self)
HwndWrapper.HwndWrapper.Click(self, *args, **kwargs)
# win32functions.WaitGuiThreadIdle(self)
time.sleep(Timings.after_button_click_wait)
@ -217,11 +231,6 @@ class ComboBoxWrapper(HwndWrapper.HwndWrapper):
"DroppedRect",
])
#-----------------------------------------------------------
def SelectedIndex(self):
"Return the selected index"
return self.SendMessage(win32defines.CB_GETCURSEL)
#-----------------------------------------------------------
def DroppedRect(self):
"Get the dropped rectangle of the combobox"
@ -242,8 +251,14 @@ class ComboBoxWrapper(HwndWrapper.HwndWrapper):
"Return the number of items in the combobox"
return self.SendMessage(win32defines.CB_GETCOUNT)
#-----------------------------------------------------------
def SelectedIndex(self):
"Return the selected index"
return self.SendMessage(win32defines.CB_GETCURSEL)
#-----------------------------------------------------------
def _get_item_index(self, ident):
"Get the index for the item with this 'ident'"
if isinstance(ident, (int, long)):
if ident >= self.ItemCount():
@ -254,6 +269,7 @@ class ComboBoxWrapper(HwndWrapper.HwndWrapper):
# negative index
if ident < 0:
# convert it to a positive index
ident = (self.ItemCount() + ident)
elif isinstance(ident, basestring):
@ -266,6 +282,7 @@ class ComboBoxWrapper(HwndWrapper.HwndWrapper):
#-----------------------------------------------------------
def ItemData(self, item):
"Return the item data associted with this item"
index = self._get_item_index(item)
"Returns the item data associated with the item if any"
return self.SendMessage(win32defines.CB_GETITEMDATA, index)
@ -330,8 +347,12 @@ class ComboBoxWrapper(HwndWrapper.HwndWrapper):
# return this control so that actions can be chained.
return self
#-----------------------------------------------------------
#def Deselect(self, item):
# Not implemented because it doesn't make sense for combo boxes.
#TODO def EditControl(self):
#
#TODO def ListControl(self):
#TODO def ItemText(self, index):
@ -377,6 +398,7 @@ class ListBoxWrapper(HwndWrapper.HwndWrapper):
#-----------------------------------------------------------
def _get_item_index(self, ident):
"Return the index of the item 'ident'"
if isinstance(ident, (int, long)):
if ident >= self.ItemCount():
@ -396,8 +418,6 @@ class ListBoxWrapper(HwndWrapper.HwndWrapper):
return ident
#-----------------------------------------------------------
def ItemCount(self):
"Return the number of items in the ListBox"
@ -727,7 +747,9 @@ class DialogWrapper(HwndWrapper.HwndWrapper):
def ClientAreaRect(self):
"""Return the client area rectangle
TODO explain how this is different from ClientRect"""
From MSDN
The client area of a control is the bounds of the control, minus the
nonclient elements such as scroll bars, borders, title bars, and menus."""
rect = win32structures.RECT(self.Rectangle())
self.SendMessage(win32defines.WM_NCCALCSIZE, 0, ctypes.byref(rect))
return rect
@ -767,6 +789,7 @@ class PopupMenuWrapper(HwndWrapper.HwndWrapper):
def _menu_handle(self):
"Get the menu handle for the popup menu menu"
mbi = win32structures.MENUBARINFO()
mbi.cbSize = ctypes.sizeof(mbi)
ret = win32functions.GetMenuBarInfo(
@ -775,6 +798,9 @@ class PopupMenuWrapper(HwndWrapper.HwndWrapper):
0,
ctypes.byref(mbi))
if not ret:
raise ctypes.WinError()
return mbi.hMenu