Python win32api 模块,SendMessage() 实例源码
我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用win32api.SendMessage()。
def release_button(self, coords, button = "left"):
if "right" in button.lower():
_button_up = win32con.WM_RBUTTONUP
elif "left" in button.lower():
_button_up = win32con.WM_LBUTTONUP
elif "middle" in button.lower():
_button_up = win32con.WM_MBUTTONUP
else:
raise SyntaxError('"Button" needs to contain "left", "right" or "middle"')
if all(isinstance(elem, float) for elem in coords):
coords = self.to_pixel(coords)
x = coords[0]
y = coords[1]
l_param = win32api.MAKELONG(x, y)
hwnd = self.win_handler.get_hwnd()
win32api.SendMessage(hwnd, _button_up, 0, l_param)
raise NotImplementedError
def OnChangeCBChain (self, msg, wParam, lParam):
if self.nextWnd == wParam:
# repair the chain
self.nextWnd = lParam
if self.nextWnd:
# pass the message to the next window in chain
win32api.SendMessage (self.nextWnd, msg, wParam, lParam)
def OnDrawClipboard (self, msg, wParam, lParam):
if self.first:
self.first = False
else:
print "clipboard content changed"
if self.nextWnd:
# pass the message to the next window in chain
win32api.SendMessage (self.nextWnd, msg, wParam, lParam)
def _get_prop(self, prop_id):
"""Retrieve an AIMP property."""
return win32api.SendMessage(self._aimp_window, WM_AIMP_PROPERTY, prop_id | AIMP_RA_PROPVALUE_GET, 0)
def _set_prop(self, prop_id, value):
"""Set an AIMP property."""
win32api.SendMessage(self._aimp_window, WM_AIMP_PROPERTY, prop_id | AIMP_RA_PROPVALUE_SET, value)
def _send_command(self, command_id, parameter=None):
"""Send an AIMP command."""
return win32api.SendMessage(self._aimp_window, WM_AIMP_COMMAND, command_id, parameter)
def SetText(cls, whd, text):
win32api.SendMessage(whd,win32con.WM_SETTEXT,None, text)
def MainCloseIEWindow(self,strControlName):
print ">>>MainCloseIEWindow"
clss = "IEFrame"
try:
hwnd = win32gui.FindWindow(clss,strControlName)
time.sleep(1)
win32api.SendMessage(hwnd,win32con.WM_CLOSE,0,0)
return True
except:
log_public(WEB_ERR_NO_0018)
self.m_ERROR_MSG = WEB_ERR_NO_0018
return False
def clickControl(self, hwnd, args):
""" Used for simple dialogs that just have buttons such as 'ok', 'cancel'
or 'clear'.
parameters:
hwnd: - The handle to the dialog.
args[0] - The button control name.
returns:
Nothing
"""
controlText = args[0];
if self.showDebugging:
x = self.wga.dumpWindow(hwnd) # dump out all the controls
pprint.pprint(x) # print out all the controls
button = self.wga.findControl(hwnd,
wantedClass="Button",
wantedText=controlText)
self.wga.clickButton(button)
#find more one time. if find another save name dialog,click it. chen sheng cong/2013/03/14
time.sleep(1)
hwnd = self.wn.FindWindow("#32770", self.popupName)
if hwnd != 0:
button = self.wga.findControl(hwnd,
wantedClass="Button",
wantedText=controlText)
self.wga.clickButton(button) #for popup two dialogs.
hwnd = win32gui.FindWindow(IECLASS,IETITLE)
print "test1",hwnd
if hwnd != 0:
print "test2"
time.sleep(1)
win32api.SendMessage(hwnd,win32con.WM_CLOSE,0,0)
def MainCloseIEWindow(self,strControlName):
print ">>>MainCloseIEWindow"
clss = "IEFrame"
try:
hwnd = win32gui.FindWindow(clss,strControlName)
time.sleep(1)
win32api.SendMessage(hwnd,win32con.WM_CLOSE,0,0)
return True
except:
log_public(WEB_ERR_NO_0018)
self.m_ERROR_MSG = WEB_ERR_NO_0018
return False
def click(self, coords, button="left",hold=False):
"""
Args:
coords (touple): coords takes two arguments, either both float
or int. If float is supplied, it will try to treat them as
percentages. X, Y
button (string): either "left","right" or "middle". Decides what button that
will be sent to the running program.
Returns:
bool: True if successful, False otherwise.
Raises:
SyntaxError: The button param does not contain "left","right og "middle"
"""
hwnd = self.win_handler.get_hwnd()
if all(isinstance(elem, float) for elem in coords):
coords = self.to_pixel(coords)
logging.debug("Trying to click on:" + str(coords) + " with " + button + " button")
x = coords[0]
y = coords[1]
if "right" in button.lower():
_button_state = win32con.MK_RBUTTON
_button_down = win32con.WM_RBUTTONDOWN
_button_up = win32con.WM_RBUTTONUP
elif "left" in button.lower():
_button_state = win32con.MK_LBUTTON
_button_down = win32con.WM_LBUTTONDOWN
_button_up = win32con.WM_LBUTTONUP
elif "middle" in button.lower():
_button_state = win32con.MK_MBUTTON
_button_down = win32con.WM_MBUTTONDOWN
_button_up = win32con.WM_MBUTTONUP
else:
raise SyntaxError('"Button" needs to contain "left", "right" or "middle"')
l_param = win32api.MAKELONG(x, y)
win32api.SendMessage(hwnd, win32con.WM_MOUSEMOVE,0,l_param)
time.sleep(0.2)
win32api.SendMessage(hwnd,_button_down, _button_state, l_param)
time.sleep(0.1)
if not hold: #Do not release the button if hold is true
win32api.SendMessage(hwnd, _button_up, 0, l_param)
self._last_x = x
self._last_y = y
return True
def hold_and_drag(self,start,end,steps,button="left"):
hwnd = self.win_handler.get_hwnd()
if all(isinstance(elem, float) for elem in start):
start = self.to_pixel(start)
if all(isinstance(elem, float) for elem in end):
end = self.to_pixel(end)
step_x = (float(end[0] - start[0])) / steps
step_y = (float(end[1] - start[1])) / steps
if "right" in button.lower():
_button_state = win32con.MK_RBUTTON
_button_down = win32con.WM_RBUTTONDOWN
_button_up = win32con.WM_RBUTTONUP
elif "left" in button.lower():
_button_state = win32con.MK_LBUTTON
_button_down = win32con.WM_LBUTTONDOWN
_button_up = win32con.WM_LBUTTONUP
elif "middle" in button.lower():
_button_state = win32con.MK_MBUTTON
_button_down = win32con.WM_MBUTTONDOWN
_button_up = win32con.WM_MBUTTONUP
else:
raise SyntaxError('"Button" needs to contain "left", "right" or "middle"')
self.move(start)
l_param = win32api.MAKELONG(start[0], start[1])
time.sleep(0.1)
win32api.SendMessage(hwnd,_button_down,_button_state,l_param)
time.sleep(0.1)
x, y = start
for step in range(0,steps):
x += step_x
y += step_y
self.move((int(x),int(y)), button=button)
time.sleep(0.01)
l_param = win32api.MAKELONG(int(x), int(y))
win32api.SendMessage(hwnd,_button_up,0,l_param)
self._last_x = x
self._last_y = y