def __init__ (self):
wx.Frame.__init__ (self, None, title="Clipboard viewer", size=(250,150))
self.first = True
self.nextWnd = None
# Get native window handle of this wxWidget Frame.
self.hwnd = self.GetHandle ()
# Set the WndProc to our function.
self.oldWndProc = win32gui.SetWindowLong (self.hwnd,
win32con.GWL_WNDPROC,
self.MyWndProc)
try:
self.nextWnd = win32clipboard.SetClipboardViewer (self.hwnd)
except win32api.error:
if win32api.GetLastError () == 0:
# information that there is no other window in chain
pass
else:
raise
python类SetWindowLong()的实例源码
def MyWndProc (self, hWnd, msg, wParam, lParam):
if msg == win32con.WM_CHANGECBCHAIN:
self.OnChangeCBChain (msg, wParam, lParam)
elif msg == win32con.WM_DRAWCLIPBOARD:
self.OnDrawClipboard (msg, wParam, lParam)
# Restore the old WndProc. Notice the use of win32api
# instead of win32gui here. This is to avoid an error due to
# not passing a callable object.
if msg == win32con.WM_DESTROY:
if self.nextWnd:
win32clipboard.ChangeClipboardChain (self.hwnd, self.nextWnd)
else:
win32clipboard.ChangeClipboardChain (self.hwnd, 0)
win32api.SetWindowLong (self.hwnd,
win32con.GWL_WNDPROC,
self.oldWndProc)
# Pass all messages (in this case, yours may be different) on
# to the original WndProc
return win32gui.CallWindowProc (self.oldWndProc,
hWnd, msg, wParam, lParam)
def __init__(self, gtk_window):
self._window = gtk_window
self._hwnd = gtk_window.window.handle
self._message_map = {}
# Windows transparency is only supported windows2000 and above.
if sys.getwindowsversion()[0] <= 4:
self.alpha = None
else:
self.alpha = 100
self._transparency = False
self.notify_icon = None
# Sublass the window and inject a WNDPROC to process messages.
self._oldwndproc = win32gui.SetWindowLong(self._hwnd, GWL_WNDPROC,
self._wndproc)
gtk_window.connect('unrealize', self.remove)
def set_alpha(self, alpha=100, colorkey=0, mask=False):
""" Sets the transparency of the window. """
if self.alpha != None:
if not self._transparency:
style = win32gui.GetWindowLong(self._hwnd, GWL_EXSTYLE)
if (style & WS_EX_LAYERED) != WS_EX_LAYERED:
style = style | WS_EX_LAYERED
win32gui.SetWindowLong(self._hwnd, GWL_EXSTYLE, style)
self._transparency = True
if mask and colorkey:
flags = LWA_COLORKEY
else:
flags = LWA_ALPHA
if colorkey:
flags = flags | LWA_COLORKEY
win_alpha = int(float(alpha)/100*255)
winxpgui.SetLayeredWindowAttributes(self._hwnd, colorkey,
win_alpha, flags)
self.alpha = int(alpha)
def OnPaint_1(hwnd, msg, wp, lp):
dc, ps=win32gui.BeginPaint(hwnd)
win32gui.SetGraphicsMode(dc, win32con.GM_ADVANCED)
br=win32gui.CreateSolidBrush(win32api.RGB(255,0,0))
win32gui.SelectObject(dc, br)
angle=win32gui.GetWindowLong(hwnd, win32con.GWL_USERDATA)
win32gui.SetWindowLong(hwnd, win32con.GWL_USERDATA, angle+2)
r_angle=angle*(math.pi/180)
win32gui.SetWorldTransform(dc,
{'M11':math.cos(r_angle), 'M12':math.sin(r_angle), 'M21':math.sin(r_angle)*-1, 'M22':math.cos(r_angle),'Dx':250,'Dy':250})
win32gui.MoveToEx(dc,250,250)
win32gui.BeginPath(dc)
win32gui.Pie(dc, 10, 70, 200, 200, 350, 350, 75, 10)
win32gui.Chord(dc, 200, 200, 850, 0, 350, 350, 75, 10)
win32gui.LineTo(dc, 300,300)
win32gui.LineTo(dc, 100, 20)
win32gui.LineTo(dc, 20, 100)
win32gui.LineTo(dc, 400, 0)
win32gui.LineTo(dc, 0, 400)
win32gui.EndPath(dc)
win32gui.StrokeAndFillPath(dc)
win32gui.EndPaint(hwnd, ps)
return 0
def TestGradientFill():
wc = win32gui.WNDCLASS()
wc.lpszClassName = 'test_win32gui_2'
wc.style = win32con.CS_GLOBALCLASS|win32con.CS_VREDRAW | win32con.CS_HREDRAW
wc.hbrBackground = win32con.COLOR_WINDOW+1
wc.lpfnWndProc=wndproc_2
class_atom=win32gui.RegisterClass(wc)
hwnd = win32gui.CreateWindowEx(0, class_atom,'Kaleidoscope',
win32con.WS_CAPTION|win32con.WS_VISIBLE|win32con.WS_THICKFRAME|win32con.WS_SYSMENU,
100,100,900,900, 0, 0, 0, None)
s=win32gui.GetWindowLong(hwnd,win32con.GWL_EXSTYLE)
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, s|win32con.WS_EX_LAYERED)
win32gui.SetLayeredWindowAttributes(hwnd, 0, 175, win32con.LWA_ALPHA)
for x in xrange(30):
win32gui.InvalidateRect(hwnd,None,True)
win32gui.PumpWaitingMessages()
time.sleep(0.3)
win32gui.DestroyWindow(hwnd)
win32gui.UnregisterClass(class_atom,None)
def OnPaint_1(hwnd, msg, wp, lp):
dc, ps=win32gui.BeginPaint(hwnd)
win32gui.SetGraphicsMode(dc, win32con.GM_ADVANCED)
br=win32gui.CreateSolidBrush(win32api.RGB(255,0,0))
win32gui.SelectObject(dc, br)
angle=win32gui.GetWindowLong(hwnd, win32con.GWL_USERDATA)
win32gui.SetWindowLong(hwnd, win32con.GWL_USERDATA, angle+2)
r_angle=angle*(math.pi/180)
win32gui.SetWorldTransform(dc,
{'M11':math.cos(r_angle), 'M12':math.sin(r_angle), 'M21':math.sin(r_angle)*-1, 'M22':math.cos(r_angle),'Dx':250,'Dy':250})
win32gui.MoveToEx(dc,250,250)
win32gui.BeginPath(dc)
win32gui.Pie(dc, 10, 70, 200, 200, 350, 350, 75, 10)
win32gui.Chord(dc, 200, 200, 850, 0, 350, 350, 75, 10)
win32gui.LineTo(dc, 300,300)
win32gui.LineTo(dc, 100, 20)
win32gui.LineTo(dc, 20, 100)
win32gui.LineTo(dc, 400, 0)
win32gui.LineTo(dc, 0, 400)
win32gui.EndPath(dc)
win32gui.StrokeAndFillPath(dc)
win32gui.EndPaint(hwnd, ps)
return 0
def TestGradientFill():
wc = win32gui.WNDCLASS()
wc.lpszClassName = 'test_win32gui_2'
wc.style = win32con.CS_GLOBALCLASS|win32con.CS_VREDRAW | win32con.CS_HREDRAW
wc.hbrBackground = win32con.COLOR_WINDOW+1
wc.lpfnWndProc=wndproc_2
class_atom=win32gui.RegisterClass(wc)
hwnd = win32gui.CreateWindowEx(0, class_atom,'Kaleidoscope',
win32con.WS_CAPTION|win32con.WS_VISIBLE|win32con.WS_THICKFRAME|win32con.WS_SYSMENU,
100,100,900,900, 0, 0, 0, None)
s=win32gui.GetWindowLong(hwnd,win32con.GWL_EXSTYLE)
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, s|win32con.WS_EX_LAYERED)
win32gui.SetLayeredWindowAttributes(hwnd, 0, 175, win32con.LWA_ALPHA)
for x in range(30):
win32gui.InvalidateRect(hwnd,None,True)
win32gui.PumpWaitingMessages()
time.sleep(0.3)
win32gui.DestroyWindow(hwnd)
win32gui.UnregisterClass(class_atom,None)
def _hide_w32_window(self):
try:
w32win = win32gui.FindWindow(None, self.title)
win32gui.ShowWindow(w32win, SW_HIDE)
win32gui.SetWindowLong(
w32win,
GWL_EXSTYLE,
win32gui.GetWindowLong(
w32win, GWL_EXSTYLE) | WS_EX_TOOLWINDOW
)
win32gui.ShowWindow(w32win, SW_SHOW)
self._return_focus_w32()
except Exception:
tb = traceback.format_exc()
Logger.error(
'Notification: An error occured in {}\n'
'{}'.format(self.title, tb)
)
def _CreateMainWindow(self, prev, settings, browser, rect):
# Creates a parent window that hosts the view window. This window
# gets the control notifications etc sent from the child.
style = win32con.WS_CHILD | win32con.WS_VISIBLE #
wclass_name = "ShellViewDemo_DefView"
# Register the Window class.
wc = win32gui.WNDCLASS()
wc.hInstance = win32gui.dllhandle
wc.lpszClassName = wclass_name
wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW
try:
win32gui.RegisterClass(wc)
except win32gui.error, details:
# Should only happen when this module is reloaded
if details[0] != winerror.ERROR_CLASS_ALREADY_EXISTS:
raise
message_map = {
win32con.WM_DESTROY: self.OnDestroy,
win32con.WM_COMMAND: self.OnCommand,
win32con.WM_NOTIFY: self.OnNotify,
win32con.WM_CONTEXTMENU: self.OnContextMenu,
win32con.WM_SIZE: self.OnSize,
}
self.hwnd = win32gui.CreateWindow( wclass_name, "", style, \
rect[0], rect[1], rect[2]-rect[0], rect[3]-rect[1],
self.hwnd_parent, 0, win32gui.dllhandle, None)
win32gui.SetWindowLong(self.hwnd, win32con.GWL_WNDPROC, message_map)
print "View 's hwnd is", self.hwnd
return self.hwnd
def CreateViewWindow(self, prev, settings, browser, rect):
print "ScintillaShellView.CreateViewWindow", prev, settings, browser, rect
# Make sure scintilla.dll is loaded. If not, find it on sys.path
# (which it generally is for Pythonwin)
try:
win32api.GetModuleHandle("Scintilla.dll")
except win32api.error:
for p in sys.path:
fname = os.path.join(p, "Scintilla.dll")
if not os.path.isfile(fname):
fname = os.path.join(p, "Build", "Scintilla.dll")
if os.path.isfile(fname):
win32api.LoadLibrary(fname)
break
else:
raise RuntimeError("Can't find scintilla!")
style = win32con.WS_CHILD | win32con.WS_VSCROLL | \
win32con.WS_HSCROLL | win32con.WS_CLIPCHILDREN | \
win32con.WS_VISIBLE
self.hwnd = win32gui.CreateWindow("Scintilla", "Scintilla", style,
rect[0], rect[1], rect[2]-rect[0], rect[3]-rect[1],
self.hwnd_parent, 1000, 0, None)
message_map = {
win32con.WM_SIZE: self.OnSize,
}
# win32gui.SetWindowLong(self.hwnd, win32con.GWL_WNDPROC, message_map)
file_data = file(self.filename, "U").read()
self._SetupLexer()
self._SendSci(scintillacon.SCI_ADDTEXT, len(file_data), file_data)
if self.lineno != None:
self._SendSci(scintillacon.SCI_GOTOLINE, self.lineno)
print "Scintilla's hwnd is", self.hwnd
def _CreateMainWindow(self, prev, settings, browser, rect):
# Creates a parent window that hosts the view window. This window
# gets the control notifications etc sent from the child.
style = win32con.WS_CHILD | win32con.WS_VISIBLE #
wclass_name = "ShellViewDemo_DefView"
# Register the Window class.
wc = win32gui.WNDCLASS()
wc.hInstance = win32gui.dllhandle
wc.lpszClassName = wclass_name
wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW
try:
win32gui.RegisterClass(wc)
except win32gui.error, details:
# Should only happen when this module is reloaded
if details[0] != winerror.ERROR_CLASS_ALREADY_EXISTS:
raise
message_map = {
win32con.WM_DESTROY: self.OnDestroy,
win32con.WM_COMMAND: self.OnCommand,
win32con.WM_NOTIFY: self.OnNotify,
win32con.WM_CONTEXTMENU: self.OnContextMenu,
win32con.WM_SIZE: self.OnSize,
}
self.hwnd = win32gui.CreateWindow( wclass_name, "", style, \
rect[0], rect[1], rect[2]-rect[0], rect[3]-rect[1],
self.hwnd_parent, 0, win32gui.dllhandle, None)
win32gui.SetWindowLong(self.hwnd, win32con.GWL_WNDPROC, message_map)
print "View 's hwnd is", self.hwnd
return self.hwnd
def CreateViewWindow(self, prev, settings, browser, rect):
print "ScintillaShellView.CreateViewWindow", prev, settings, browser, rect
# Make sure scintilla.dll is loaded. If not, find it on sys.path
# (which it generally is for Pythonwin)
try:
win32api.GetModuleHandle("Scintilla.dll")
except win32api.error:
for p in sys.path:
fname = os.path.join(p, "Scintilla.dll")
if not os.path.isfile(fname):
fname = os.path.join(p, "Build", "Scintilla.dll")
if os.path.isfile(fname):
win32api.LoadLibrary(fname)
break
else:
raise RuntimeError("Can't find scintilla!")
style = win32con.WS_CHILD | win32con.WS_VSCROLL | \
win32con.WS_HSCROLL | win32con.WS_CLIPCHILDREN | \
win32con.WS_VISIBLE
self.hwnd = win32gui.CreateWindow("Scintilla", "Scintilla", style,
rect[0], rect[1], rect[2]-rect[0], rect[3]-rect[1],
self.hwnd_parent, 1000, 0, None)
message_map = {
win32con.WM_SIZE: self.OnSize,
}
# win32gui.SetWindowLong(self.hwnd, win32con.GWL_WNDPROC, message_map)
file_data = file(self.filename, "U").read()
self._SetupLexer()
self._SendSci(scintillacon.SCI_ADDTEXT, len(file_data), file_data)
if self.lineno != None:
self._SendSci(scintillacon.SCI_GOTOLINE, self.lineno)
print "Scintilla's hwnd is", self.hwnd
def _CreateMainWindow(self, prev, settings, browser, rect):
# Creates a parent window that hosts the view window. This window
# gets the control notifications etc sent from the child.
style = win32con.WS_CHILD | win32con.WS_VISIBLE #
wclass_name = "ShellViewDemo_DefView"
# Register the Window class.
wc = win32gui.WNDCLASS()
wc.hInstance = win32gui.dllhandle
wc.lpszClassName = wclass_name
wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW
try:
win32gui.RegisterClass(wc)
except win32gui.error as details:
# Should only happen when this module is reloaded
if details[0] != winerror.ERROR_CLASS_ALREADY_EXISTS:
raise
message_map = {
win32con.WM_DESTROY: self.OnDestroy,
win32con.WM_COMMAND: self.OnCommand,
win32con.WM_NOTIFY: self.OnNotify,
win32con.WM_CONTEXTMENU: self.OnContextMenu,
win32con.WM_SIZE: self.OnSize,
}
self.hwnd = win32gui.CreateWindow( wclass_name, "", style, \
rect[0], rect[1], rect[2]-rect[0], rect[3]-rect[1],
self.hwnd_parent, 0, win32gui.dllhandle, None)
win32gui.SetWindowLong(self.hwnd, win32con.GWL_WNDPROC, message_map)
print("View 's hwnd is", self.hwnd)
return self.hwnd
def CreateViewWindow(self, prev, settings, browser, rect):
print("ScintillaShellView.CreateViewWindow", prev, settings, browser, rect)
# Make sure scintilla.dll is loaded. If not, find it on sys.path
# (which it generally is for Pythonwin)
try:
win32api.GetModuleHandle("Scintilla.dll")
except win32api.error:
for p in sys.path:
fname = os.path.join(p, "Scintilla.dll")
if not os.path.isfile(fname):
fname = os.path.join(p, "Build", "Scintilla.dll")
if os.path.isfile(fname):
win32api.LoadLibrary(fname)
break
else:
raise RuntimeError("Can't find scintilla!")
style = win32con.WS_CHILD | win32con.WS_VSCROLL | \
win32con.WS_HSCROLL | win32con.WS_CLIPCHILDREN | \
win32con.WS_VISIBLE
self.hwnd = win32gui.CreateWindow("Scintilla", "Scintilla", style,
rect[0], rect[1], rect[2]-rect[0], rect[3]-rect[1],
self.hwnd_parent, 1000, 0, None)
message_map = {
win32con.WM_SIZE: self.OnSize,
}
# win32gui.SetWindowLong(self.hwnd, win32con.GWL_WNDPROC, message_map)
file_data = file(self.filename, "U").read()
self._SetupLexer()
self._SendSci(scintillacon.SCI_ADDTEXT, len(file_data), file_data)
if self.lineno != None:
self._SendSci(scintillacon.SCI_GOTOLINE, self.lineno)
print("Scintilla's hwnd is", self.hwnd)
def add_titlebar(hwnd):
"""
Sets the window style to include a titlebar if it doesn't have one.
Args:
hwnd (int): The window handler.
"""
style = wg.GetWindowLong(hwnd, wc.GWL_STYLE)
style |= wc.WS_CAPTION
wg.SetWindowLong(hwnd, wc.GWL_STYLE, style)
maximize(hwnd)
restore(hwnd)
def remove_titlebar(hwnd):
"""
Sets window style to caption (no titlebar).
Args:
hwnd (int): The window handler.
"""
style = wg.GetWindowLong(hwnd, wc.GWL_STYLE)
style &= ~wc.WS_CAPTION
wg.SetWindowLong(hwnd, wc.GWL_STYLE, style)
maximize(hwnd)
restore(hwnd)
def hide_extra_ui(self, hwnd=None, remove=True):
"""
:param hwnd: Hwnd to remove all styling from. If not supplied, then the default hwnd is used
:param remove: If true: Removes all styling. If false: Adds back the removed styles
:return: NoneType
"""
logging.debug('Trying to manipulate UI')
if hwnd is None:
hwnd = self.get_hwnd()
style = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
if remove:
logging.debug('Removing UI')
style = style | win32con.WS_POPUP
style = style & ~win32con.WS_OVERLAPPEDWINDOW
else:
logging.debug('Adding UI')
style = style & ~win32con.WS_POPUP
style = style | win32con.WS_OVERLAPPEDWINDOW
win32gui.ShowWindow(hwnd, win32con.SW_HIDE)
win32gui.SetWindowLong(hwnd, win32con.GWL_STYLE, style)
win32gui.ShowWindow(hwnd, win32con.SW_SHOW)
def WndProcManage(wnd):
if not hasattr(wnd, 'GetHandle'):
return False
# Make a dictionary of message names to be used for printing below
if PRINT_MESSAGES:
msgdict = dict()
for name in dir(win32con):
if name.startswith('WM_'):
value = getattr(win32con, name)
msgdict[value] = name
_oldWndProc = win32gui.GetWindowLong(wnd.GetHandle(),
win32con.GWL_WNDPROC)
def MyWndProc(self, hwnd, msg, wParam, lParam):
# Display what we've got.
if PRINT_MESSAGES:
print (msgdict.get(msg), msg, wParam, lParam)
# Restore the old WndProc. Notice the use of win32api
# instead of win32gui here. This is to avoid an error due to
# not passing a callable object.
if msg == win32con.WM_DESTROY:
win32api.SetWindowLong(self.GetHandle(),
win32con.GWL_WNDPROC,
_oldWndProc)
stopproc = False
for cb, cbtrigger in CALLBACKS[self]:
if cbtrigger(self, msg, wParam, lParam):
if not cb(self, msg, wParam, lParam):
stopproc = True
break
if stopproc:
return
# Pass all messages (in this case, yours may be different) on to
# the original WndProc
return win32gui.CallWindowProc(_oldWndProc,
hwnd, msg,
wParam,
lParam)
# Bind the function to the passed object
_newWndProc = MyWndProc.__get__(wnd, wnd.__class__)
# Set the WndProc to our function
win32gui.SetWindowLong(wnd.GetHandle(),
win32con.GWL_WNDPROC,
_newWndProc)
return True