限制关闭 python 应用程序 (win32gui)
restrict close python application (win32gui)
我想限制任何用户关闭应用程序。
我该怎么做?禁用 "close" 按钮或使其成为 "minimize" 按钮?
此程序将监视特定文件的复制粘贴,因此它必须在系统上持续 运行。
import wx
import win32api
import win32gui
import win32con
import win32clipboard
import filelocationvariable
class TestFrame (wx.Frame):
def __init__ (self):
wx.Frame.__init__ (self, None, title="Clipboard viewer", size=(0,0))
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
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 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:
if win32clipboard.IsClipboardFormatAvailable(win32con.CF_TEXT):
try:
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
self.comparing(data)
except TypeError:
pass
if win32clipboard.IsClipboardFormatAvailable(win32con.CF_HDROP):
try:
win32clipboard.OpenClipboard()
text = win32clipboard.GetClipboardData(win32con.CF_HDROP)
win32clipboard.CloseClipboard()
self.comparing(text[0])
except TypeError:
pass
if self.nextWnd:
# pass the message to the next window in chain
win32api.SendMessage (self.nextWnd, msg, wParam, lParam)
def comparing(self,text):
try:
fp = open(filelocationvariable.implocationlist,'r')
str2 = text
for lines in fp:
str1 = lines[0:len(lines)-1]
if str1 == str2:
try:
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.CloseClipboard()
except:
pass
except IOError:
pass
finally:
fp.close()
app = wx.App(False)
frame = TestFrame ()
frame.Show ()
app.MainLoop ()
将框架绑定到EVT_CLOSE事件
self.Bind(wx.EVT_CLOSE, self.on_close)
然后决定你想如何处理关闭事件,即
def on_close(self, event):
if self.should_close:
event.Skip() # calling event.Skip() will allow the window to close
我想限制任何用户关闭应用程序。 我该怎么做?禁用 "close" 按钮或使其成为 "minimize" 按钮?
此程序将监视特定文件的复制粘贴,因此它必须在系统上持续 运行。
import wx
import win32api
import win32gui
import win32con
import win32clipboard
import filelocationvariable
class TestFrame (wx.Frame):
def __init__ (self):
wx.Frame.__init__ (self, None, title="Clipboard viewer", size=(0,0))
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
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 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:
if win32clipboard.IsClipboardFormatAvailable(win32con.CF_TEXT):
try:
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
self.comparing(data)
except TypeError:
pass
if win32clipboard.IsClipboardFormatAvailable(win32con.CF_HDROP):
try:
win32clipboard.OpenClipboard()
text = win32clipboard.GetClipboardData(win32con.CF_HDROP)
win32clipboard.CloseClipboard()
self.comparing(text[0])
except TypeError:
pass
if self.nextWnd:
# pass the message to the next window in chain
win32api.SendMessage (self.nextWnd, msg, wParam, lParam)
def comparing(self,text):
try:
fp = open(filelocationvariable.implocationlist,'r')
str2 = text
for lines in fp:
str1 = lines[0:len(lines)-1]
if str1 == str2:
try:
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.CloseClipboard()
except:
pass
except IOError:
pass
finally:
fp.close()
app = wx.App(False)
frame = TestFrame ()
frame.Show ()
app.MainLoop ()
将框架绑定到EVT_CLOSE事件
self.Bind(wx.EVT_CLOSE, self.on_close)
然后决定你想如何处理关闭事件,即
def on_close(self, event):
if self.should_close:
event.Skip() # calling event.Skip() will allow the window to close