Python win32api 模块,GetCursorPos() 实例源码
我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用win32api.GetCursorPos()。
def OnItemRightClick(self, notify_data, extra):
# First select the item we right-clicked on.
pt = self.ScreenToClient(win32api.GetCursorPos())
flags, hItem, subitem = self.HitTest(pt)
if hItem==-1 or commctrl.TVHT_ONITEM & flags==0:
return None
self.SetItemState(hItem, commctrl.LVIS_SELECTED, commctrl.LVIS_SELECTED)
menu = win32ui.CreatePopupMenu()
menu.AppendMenu(win32con.MF_STRING|win32con.MF_ENABLED,1000, "Edit item")
menu.AppendMenu(win32con.MF_STRING|win32con.MF_ENABLED,1001, "Delete item")
dockbar = self.GetParent()
if dockbar.IsFloating():
hook_parent = win32ui.GetMainFrame()
else:
hook_parent = self.GetParentFrame()
hook_parent.HookCommand(self.OnEditItem, 1000)
hook_parent.HookCommand(self.OnDeleteItem, 1001)
menu.TrackPopupMenu(win32api.GetCursorPos()) # track at mouse position.
return None
def OnItemRightClick(self, notify_data, extra):
# First select the item we right-clicked on.
pt = self.ScreenToClient(win32api.GetCursorPos())
flags, hItem = self.HitTest(pt)
if hItem==0 or commctrl.TVHT_ONITEM & flags==0:
return None
self.Select(hItem, commctrl.TVGN_CARET)
menu = win32ui.CreatePopupMenu()
menu.AppendMenu(win32con.MF_STRING|win32con.MF_ENABLED,1000, "Add Key")
menu.AppendMenu(win32con.MF_STRING|win32con.MF_ENABLED,1001, "Add Value")
menu.AppendMenu(win32con.MF_STRING|win32con.MF_ENABLED,1002, "Delete Key")
self.HookCommand(self.OnAddKey, 1000)
self.HookCommand(self.OnAddValue, 1001)
self.HookCommand(self.OnDeleteKey, 1002)
menu.TrackPopupMenu(win32api.GetCursorPos()) # track at mouse position.
return None
def OnItemRightClick(self, notify_data, extra):
# First select the item we right-clicked on.
pt = self.ScreenToClient(win32api.GetCursorPos())
flags, hItem, subitem = self.HitTest(pt)
if hItem==-1 or commctrl.TVHT_ONITEM & flags==0:
return None
self.SetItemState(hItem, commctrl.LVIS_SELECTED, commctrl.LVIS_SELECTED)
menu = win32ui.CreatePopupMenu()
menu.AppendMenu(win32con.MF_STRING|win32con.MF_ENABLED,1000, "Edit item")
menu.AppendMenu(win32con.MF_STRING|win32con.MF_ENABLED,1001, "Delete item")
dockbar = self.GetParent()
if dockbar.IsFloating():
hook_parent = win32ui.GetMainFrame()
else:
hook_parent = self.GetParentFrame()
hook_parent.HookCommand(self.OnEditItem, 1000)
hook_parent.HookCommand(self.OnDeleteItem, 1001)
menu.TrackPopupMenu(win32api.GetCursorPos()) # track at mouse position.
return None
def OnItemRightClick(self, notify_data, extra):
# First select the item we right-clicked on.
pt = self.ScreenToClient(win32api.GetCursorPos())
flags, hItem = self.HitTest(pt)
if hItem==0 or commctrl.TVHT_ONITEM & flags==0:
return None
self.Select(hItem, commctrl.TVGN_CARET)
menu = win32ui.CreatePopupMenu()
menu.AppendMenu(win32con.MF_STRING|win32con.MF_ENABLED,1000, "Add Key")
menu.AppendMenu(win32con.MF_STRING|win32con.MF_ENABLED,1001, "Add Value")
menu.AppendMenu(win32con.MF_STRING|win32con.MF_ENABLED,1002, "Delete Key")
self.HookCommand(self.OnAddKey, 1000)
self.HookCommand(self.OnAddValue, 1001)
self.HookCommand(self.OnDeleteKey, 1002)
menu.TrackPopupMenu(win32api.GetCursorPos()) # track at mouse position.
return None
def MouseLButtonClick(self):
if self.element == None:
win32api.SetCursorPos((self.posX, self.posY))
FX_MouseLClick(self.posX, self.posY)
return True
try:
self.element.SetFocus()
except:
print('Focus error')
pos = self.element.GetClickablePoint()
if pos[1] == False:
return False
win32api.SetCursorPos((pos[0].x, pos[0].y))
#pos = win32api.GetCursorPos()
FX_MouseLClick(pos[0].x, pos[0].y)
return True
def main():
set_window_top()
print u'????? http://cn.vonvon.net/quiz/743 ??????'
print u'????????????????? y/n\n> ',
choice = raw_input()
offset_x, offset_x_r, offset_y, offset_y_r = 0, 0, 0, 0
if choice == 'y':
offset_x = 770
offset_y = 265
offset_x_r = 1145
offset_y_r = 640
elif choice == 'n':
print u'???????, ??????????????'
count_down(3)
offset_x, offset_y = win32api.GetCursorPos()
print u'?????, ????????'
count_down(3)
offset_x_r, offset_y_r = win32api.GetCursorPos()
print u'????????...'
global im
count = 50
while count:
count -= 1
im = grab((offset_x, offset_y, offset_x_r, offset_y_r))
mouse_move([sum(i) for i in zip((offset_x, offset_y), get_position())])
sleep(0.1)
mouse_click()
sleep(0.2)
def get_position(self):
"""get mouse position"""
return win32api.GetCursorPos()
#-----------------------------------------------------------------------------------------
#Added functions
#-----------------------------------------------------------------------------------------
def invisible_click(self,pos=(-1, -1), button_name="left"):
"""Click in specified place without moving mouse"""
xcur,ycur = win32gui.GetCursorPos()
ctypes.windll.user32.SetCursorPos(pos[0],pos[1])
self.click(pos,button_name)
ctypes.windll.user32.SetCursorPos(xcur,ycur)
def invisible_click_rel(self,handle,pos, button_name="left"):
"""Click in window coordinates without moving mouse"""
#get window info
xleft, ytop, xright, ybottom = win32gui.GetWindowRect(handle)
xcur,ycur = win32gui.GetCursorPos()
ctypes.windll.user32.SetCursorPos(pos[0]+xleft,pos[1]+ytop)
self.click((pos[0]+xleft,pos[1]+ytop),button_name)
ctypes.windll.user32.SetCursorPos(xcur,ycur)
def _click(x, y):
handle = win32gui.GetForegroundWindow()
x_old, y_old = win32api.GetCursorPos()
# see https://github.com/asweigart/pyautogui/issues/23
try:
pyautogui.click(x, y, 1)
except FileNotFoundError:
pass
win32api.SetCursorPos((x_old, y_old))
win32gui.SetForegroundWindow(handle)
def MouseLButtonDown(self):
pos = self.element.GetClickablePoint()
if pos[1] == False:
return False
win32api.SetCursorPos((pos[0].x, pos[0].y))
#pos = win32api.GetCursorPos()
FX_MouseLDown(pos[0].x, pos[0].y)
return True
def MouseMove(self):
pos = self.element.GetClickablePoint()
if pos[1] == False:
return False
win32api.SetCursorPos((pos[0].x, pos[0].y))
return True
##
## def MouseLButtonUp(self):
## pos = self.element.GetClickablePoint()
## if pos[1] == False:
## return False
## win32api.SetCursorPos((pos[0].x, pos[0].y))
## #pos = win32api.GetCursorPos()
## FX_MouseLDown(pos[0].x, pos[0].y)
## return True
def OnContextMenu(self, hwnd, msg, wparam, lparam):
# Get the selected items.
pidls = []
n = -1
while 1:
n = win32gui.SendMessage(self.hwnd_child,
commctrl.LVM_GETNEXTITEM,
n,
commctrl.LVNI_SELECTED)
if n==-1:
break
pidls.append(self.children[n][-1:])
spt = win32api.GetCursorPos()
if not pidls:
print "Ignoring background click"
return
# Get the IContextMenu for the items.
inout, cm = self.folder.GetUIObjectOf(self.hwnd_parent, pidls, shell.IID_IContextMenu, 0)
hmenu = win32gui.CreatePopupMenu()
sel = None
# As per 'Q179911', we need to determine if the default operation
# should be 'open' or 'explore'
try:
flags = 0
try:
self.browser.GetControlWindow(shellcon.FCW_TREE)
flags |= shellcon.CMF_EXPLORE
except pythoncom.com_error:
pass
id_cmd_first = 1 # TrackPopupMenu makes it hard to use 0
cm.QueryContextMenu(hmenu, 0, id_cmd_first, -1, flags)
tpm_flags = win32con.TPM_LEFTALIGN | win32con.TPM_RETURNCMD | \
win32con.TPM_RIGHTBUTTON
sel = win32gui.TrackPopupMenu(hmenu,
tpm_flags,
spt[0], spt[1],
0, self.hwnd, None)
print "TrackPopupMenu returned", sel
finally:
win32gui.DestroyMenu(hmenu)
if sel:
ci = 0, self.hwnd_parent, sel-id_cmd_first, None, None, 0, 0, 0
cm.InvokeCommand(ci)
def handleEvents(self, args):
if isinstance(args, MouseEvent):
if args.current_key == 'LButton' and args.event_type == 'key down':
self.mouseX = args.mouse_x
self.mouseY = args.mouse_y
self.xAndY = win32api.GetCursorPos()
if args.current_key == 'LButton' and args.event_type == 'key up':
if self.mouseX != args.mouse_x or self.mouseY != args.mouse_y:
keyboard.press_and_release('ctrl+c')
# if isinstance(args, KeyboardEvent):
# if 'control' in args.current_key and args.event_type == 'key down':
# self.ctrlDown = True
# if 'control' in args.current_key and args.event_type == 'key up':
# self.ctrlDown = False
# if args.current_key == 'C' and self.ctrlDown:
# if self.sysCopy:
# pass
# else:
# self.allowCopy = True
# down = False
# def handleEvents(args):
# global down
# if isinstance(args, MouseEvent):
# if args.current_key == 'LButton' and args.event_type == 'key up':
# keyboard.press_and_release('ctrl+c')
# if isinstance(args, KeyboardEvent):
# if 'control' in args.current_key and args.event_type == 'key down':
# down = True
# if 'control' in args.current_key and args.event_type == 'key up':
# down = False
# if args.current_key == 'C' and down:
# print(1)
# def onClipboardChanged()
# data = clipboard.mimeData()
# if data.hasText():
# print(data.text())
# app = QApplication([])
# clipboard = app.clipboard()
# clipboard.dataChanged.connect(onClipboardChanged)
def OnContextMenu(self, hwnd, msg, wparam, lparam):
# Get the selected items.
pidls = []
n = -1
while 1:
n = win32gui.SendMessage(self.hwnd_child,
commctrl.LVM_GETNEXTITEM,
n,
commctrl.LVNI_SELECTED)
if n==-1:
break
pidls.append(self.children[n][-1:])
spt = win32api.GetCursorPos()
if not pidls:
print "Ignoring background click"
return
# Get the IContextMenu for the items.
inout, cm = self.folder.GetUIObjectOf(self.hwnd_parent, pidls, shell.IID_IContextMenu, 0)
hmenu = win32gui.CreatePopupMenu()
sel = None
# As per 'Q179911', we need to determine if the default operation
# should be 'open' or 'explore'
try:
flags = 0
try:
self.browser.GetControlWindow(shellcon.FCW_TREE)
flags |= shellcon.CMF_EXPLORE
except pythoncom.com_error:
pass
id_cmd_first = 1 # TrackPopupMenu makes it hard to use 0
cm.QueryContextMenu(hmenu, 0, id_cmd_first, -1, flags)
tpm_flags = win32con.TPM_LEFTALIGN | win32con.TPM_RETURNCMD | \
win32con.TPM_RIGHTBUTTON
sel = win32gui.TrackPopupMenu(hmenu,
tpm_flags,
spt[0], spt[1],
0, self.hwnd, None)
print "TrackPopupMenu returned", sel
finally:
win32gui.DestroyMenu(hmenu)
if sel:
ci = 0, self.hwnd_parent, sel-id_cmd_first, None, None, 0, 0, 0
cm.InvokeCommand(ci)
def CalcDynamicLayout(self, length, mode):
# Support for diagonal sizing.
if self.IsFloating():
self.GetParent().GetParent().ModifyStyle(win32ui.MFS_4THICKFRAME, 0)
if mode & (win32ui.LM_HORZDOCK | win32ui.LM_VERTDOCK):
flags = win32con.SWP_NOSIZE | win32con.SWP_NOMOVE | win32con.SWP_NOZORDER |\
win32con.SWP_NOACTIVATE | win32con.SWP_FRAMECHANGED
self.SetWindowPos(0, (0, 0, 0, 0,), flags)
self.dockSite.RecalcLayout()
return self._obj_.CalcDynamicLayout(length, mode)
if mode & win32ui.LM_MRUWIDTH:
return self.sizeFloat
if mode & win32ui.LM_COMMIT:
self.sizeFloat = length, self.sizeFloat[1]
return self.sizeFloat
# More diagonal sizing.
if self.IsFloating():
dc = self.dockContext
pt = win32api.GetCursorPos()
windowRect = self.GetParent().GetParent().GetWindowRect()
hittest = dc.nHitTest
if hittest==win32con.HTTOPLEFT:
cx = max(windowRect[2] - pt[0], self.cMinWidth) - self.cxBorder
cy = max(windowRect[3] - self.cCaptionSize - pt[1],self.cMinHeight) - 1
self.sizeFloat = cx, cy
top = min(pt[1], windowRect[3] - self.cCaptionSize - self.cMinHeight) - self.cyBorder
left = min(pt[0], windowRect[2] - self.cMinWidth) - 1
dc.rectFrameDragHorz = left, top, dc.rectFrameDragHorz[2], dc.rectFrameDragHorz[3]
return self.sizeFloat
if hittest==win32con.HTTOPRIGHT:
cx = max(pt[0] - windowRect[0], self.cMinWidth)
cy = max(windowRect[3] - self.cCaptionSize - pt[1], self.cMinHeight) - 1
self.sizeFloat = cx, cy
top = min(pt[1], windowRect[3] - self.cCaptionSize - self.cMinHeight) - self.cyBorder
dc.rectFrameDragHorz = dc.rectFrameDragHorz[0], top, dc.rectFrameDragHorz[2], dc.rectFrameDragHorz[3]
return self.sizeFloat
if hittest==win32con.HTBOTTOMLEFT:
cx = max(windowRect[2] - pt[0], self.cMinWidth) - self.cxBorder
cy = max(pt[1] - windowRect[1] - self.cCaptionSize, self.cMinHeight)
self.sizeFloat = cx, cy
left = min(pt[0], windowRect[2] -self.cMinWidth) - 1
dc.rectFrameDragHorz = left, dc.rectFrameDragHorz[1], dc.rectFrameDragHorz[2], dc.rectFrameDragHorz[3]
return self.sizeFloat
if hittest==win32con.HTBOTTOMRIGHT:
cx = max(pt[0] - windowRect[0], self.cMinWidth)
cy = max(pt[1] - windowRect[1] - self.cCaptionSize, self.cMinHeight)
self.sizeFloat = cx, cy
return self.sizeFloat
if mode & win32ui.LM_LENGTHY:
self.sizeFloat = self.sizeFloat[0], max(self.sizeMin[1], length)
return self.sizeFloat
else:
return max(self.sizeMin[0], length), self.sizeFloat[1]
def OnContextMenu(self, hwnd, msg, wparam, lparam):
# Get the selected items.
pidls = []
n = -1
while 1:
n = win32gui.SendMessage(self.hwnd_child,
commctrl.LVM_GETNEXTITEM,
n,
commctrl.LVNI_SELECTED)
if n==-1:
break
pidls.append(self.children[n][-1:])
spt = win32api.GetCursorPos()
if not pidls:
print("Ignoring background click")
return
# Get the IContextMenu for the items.
inout, cm = self.folder.GetUIObjectOf(self.hwnd_parent, pidls, shell.IID_IContextMenu, 0)
hmenu = win32gui.CreatePopupMenu()
sel = None
# As per 'Q179911', we need to determine if the default operation
# should be 'open' or 'explore'
try:
flags = 0
try:
self.browser.GetControlWindow(shellcon.FCW_TREE)
flags |= shellcon.CMF_EXPLORE
except pythoncom.com_error:
pass
id_cmd_first = 1 # TrackPopupMenu makes it hard to use 0
cm.QueryContextMenu(hmenu, 0, id_cmd_first, -1, flags)
tpm_flags = win32con.TPM_LEFTALIGN | win32con.TPM_RETURNCMD | \
win32con.TPM_RIGHTBUTTON
sel = win32gui.TrackPopupMenu(hmenu,
tpm_flags,
spt[0], spt[1],
0, self.hwnd, None)
print("TrackPopupMenu returned", sel)
finally:
win32gui.DestroyMenu(hmenu)
if sel:
ci = 0, self.hwnd_parent, sel-id_cmd_first, None, None, 0, 0, 0
cm.InvokeCommand(ci)
def CalcDynamicLayout(self, length, mode):
# Support for diagonal sizing.
if self.IsFloating():
self.GetParent().GetParent().ModifyStyle(win32ui.MFS_4THICKFRAME, 0)
if mode & (win32ui.LM_HORZDOCK | win32ui.LM_VERTDOCK):
flags = win32con.SWP_NOSIZE | win32con.SWP_NOMOVE | win32con.SWP_NOZORDER |\
win32con.SWP_NOACTIVATE | win32con.SWP_FRAMECHANGED
self.SetWindowPos(0, (0, 0, 0, 0,), flags)
self.dockSite.RecalcLayout()
return self._obj_.CalcDynamicLayout(length, mode)
if mode & win32ui.LM_MRUWIDTH:
return self.sizeFloat
if mode & win32ui.LM_COMMIT:
self.sizeFloat = length, self.sizeFloat[1]
return self.sizeFloat
# More diagonal sizing.
if self.IsFloating():
dc = self.dockContext
pt = win32api.GetCursorPos()
windowRect = self.GetParent().GetParent().GetWindowRect()
hittest = dc.nHitTest
if hittest==win32con.HTTOPLEFT:
cx = max(windowRect[2] - pt[0], self.cMinWidth) - self.cxBorder
cy = max(windowRect[3] - self.cCaptionSize - pt[1],self.cMinHeight) - 1
self.sizeFloat = cx, cy
top = min(pt[1], windowRect[3] - self.cCaptionSize - self.cMinHeight) - self.cyBorder
left = min(pt[0], windowRect[2] - self.cMinWidth) - 1
dc.rectFrameDragHorz = left, top, dc.rectFrameDragHorz[2], dc.rectFrameDragHorz[3]
return self.sizeFloat
if hittest==win32con.HTTOPRIGHT:
cx = max(pt[0] - windowRect[0], self.cMinWidth)
cy = max(windowRect[3] - self.cCaptionSize - pt[1], self.cMinHeight) - 1
self.sizeFloat = cx, cy
top = min(pt[1], windowRect[3] - self.cCaptionSize - self.cMinHeight) - self.cyBorder
dc.rectFrameDragHorz = dc.rectFrameDragHorz[0], top, dc.rectFrameDragHorz[2], dc.rectFrameDragHorz[3]
return self.sizeFloat
if hittest==win32con.HTBOTTOMLEFT:
cx = max(windowRect[2] - pt[0], self.cMinWidth) - self.cxBorder
cy = max(pt[1] - windowRect[1] - self.cCaptionSize, self.cMinHeight)
self.sizeFloat = cx, cy
left = min(pt[0], windowRect[2] -self.cMinWidth) - 1
dc.rectFrameDragHorz = left, dc.rectFrameDragHorz[1], dc.rectFrameDragHorz[2], dc.rectFrameDragHorz[3]
return self.sizeFloat
if hittest==win32con.HTBOTTOMRIGHT:
cx = max(pt[0] - windowRect[0], self.cMinWidth)
cy = max(pt[1] - windowRect[1] - self.cCaptionSize, self.cMinHeight)
self.sizeFloat = cx, cy
return self.sizeFloat
if mode & win32ui.LM_LENGTHY:
self.sizeFloat = self.sizeFloat[0], max(self.sizeMin[1], length)
return self.sizeFloat
else:
return max(self.sizeMin[0], length), self.sizeFloat[1]