# -*- coding:utf-8 -*-
#
import win32gui
from win32con import *
# Windows Message Process Function
def WndProc(hwnd, msg, wParam, lParam):
if msg == WM_PAINT:
hdc,ps = win32gui.BeginPaint(hwnd)
rect = win32gui.GetClientRect(hwnd)
win32gui.DrawText(hdc,
'GUI Python',
len('GUI Python'),
rect,
DT_SINGLELINE|DT_CENTER|DT_VCENTER)
win32gui.EndPaint(hwnd,ps)
if msg == WM_DESTROY:
win32gui.PostQuitMessage(0)
return win32gui.DefWindowProc(hwnd, msg, wParam, lParam)
# Generate Windows Class, and set the related item value
wc = win32gui.WNDCLASS()
wc.hbrBackground = COLOR_BTNFACE+1
wc.hCursor = win32gui.LoadCursor(0, IDC_ARROW)
wc.hIcon = win32gui.LoadIcon(0, IDI_APPLICATION)
wc.lpszClassName = "Python on Windows"
wc.lpfnWndProc = WndProc
# Register Windows Class
reg = win32gui.RegisterClass(wc)
# Create Window
hwnd = win32gui.CreateWindow(
reg, 'Python', WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
0, 0, 0, None)
# Display Window
win32gui.ShowWindow(hwnd, SW_SHOWNORMAL)
win32gui.UpdateWindow(hwnd)
# Into MessageLoop, Util close the window
win32gui.PumpMessages()
* 使用MFC创建窗口
首先,创建窗口类;然后,重载消息处理方法;最后类实例化,创建窗口。
当窗口创建以后,调用RunModalLoop方法进入窗口消息循环,否则窗口将立刻关闭。
使用EndModalLoop方法结束消息循环。
# -*- coding:utf-8 -*-
#
import win32ui
import win32api
from win32con import *
from pywin.mfc import window
# define windows class
class MyWnd(window.Wnd):
def __init__(self):
window.Wnd.__init__(self, win32ui.CreateWnd())
self._obj_.CreateWindowEx(WS_EX_CLIENTEDGE,\
win32ui.RegisterWndClass(0, 0, COLOR_WINDOW+1),\
'MFC GUI', WS_OVERLAPPEDWINDOW,\
(100, 100, 400, 300), None, 0, None)
# reload OnClose Method
def OnClose(self):
self.EndModalLoop(0)
# reload OnPaint Method, which in windows print "MFC GUI"
def OnPaint(self):
dc, ps = self.BeginPaint()
dc.DrawText("MFC GUI",
self.GetClientRect(),
DT_SINGLELINE|DT_CENTER|DT_VCENTER)
self.EndPaint(ps)
w = MyWnd()
w.ShowWindow()
w.UpdateWindow()
w.RunModalLoop(1)
# -*- coding:utf-8 -*-
#
import win32ui
import win32con
from pywin.mfc import dialog
# define windows class
class MyDialog(dialog.Dialog):
def OnInitDialog(self):
dialog.Dialog.OnInitDialog(self)
style = (win32con.DS_MODALFRAME |
win32con.WS_POPUP|
win32con.WS_VISIBLE|
win32con.WS_CAPTION|
win32con.WS_SYSMENU|
win32con.DS_SETFONT)
di = ['Python',
(0,0,300,180),
style,
None,
(8, 'MS Sans Serif')]
init = []
init.append(di)
mydialog = MyDialog(init)
mydialog.DoModal()
import win32ui
import win32con
from pywin.mfc import dialog
# define windows class
class MyDialog(dialog.Dialog):
def OnInitDialog(self):
dialog.Dialog.OnInitDialog(self)
def OnOK(self):
win32ui.MessageBox('Press Ok',\
'Python',
win32con.MB_OK)
self.EndDialog(1)
def OnCancel(self):
win32ui.MessageBox('Press Cancel',\
'Python',
win32con.MB_OK)
self.EndDialog(1)
style = (win32con.DS_MODALFRAME |
win32con.WS_POPUP|
win32con.WS_VISIBLE|
win32con.WS_CAPTION|
win32con.WS_SYSMENU|
win32con.DS_SETFONT)
childstyle = (win32con.WS_CHILD|win32con.WS_VISIBLE)
buttonstyle = win32con.WS_TABSTOP|childstyle
di = ['Python',
(0,0,300,180),
style,
None,
(8, 'MS Sans Serif')]
ButOK = (['Button',
"OK",
win32con.IDOK,
(80,150,50,14),
buttonstyle|win32con.BS_PUSHBUTTON])
ButCancel = (['Button',
"Cancel",
win32con.IDCANCEL,
(160,150,50,14),
buttonstyle|win32con.BS_PUSHBUTTON])
Stadic=(['Static',
"Python Dialog",
12,
(130, 50, 60, 14),
childstyle])
Edit = (['Edit',
"",
13,
(130, 80, 60, 14),
childstyle|win32con.ES_LEFT|win32con.WS_BORDER|win32con.WS_TABSTOP])
init = []
init.append(di)
init.append(ButOK)
init.append(ButCancel)
init.append(Stadic)
init.append(Edit)
init.append
mydialog = MyDialog(init)
mydialog.DoModal()
# -*- coding:utf-8 -*-
#
import win32ui
import win32con
from pywin.mfc import dialog
# define windows class
class MyDialog(dialog.Dialog):
def OnInitDialog(self):
dialog.Dialog.OnInitDialog(self)
self.HookCommand(self.OnButton1, 1051)
self.HookCommand(self.OnButton2, 1052)
def OnButton1(self, wParam, lParam):
win32ui.MessageBox('Button1', 'Python',win32con.MB_OK)
self.EndDialog(1)
def OnButton2(self, wParam, lParam):
text = self.GetDlgItemText(1024)
win32ui.MessageBox(text, 'Python', win32con.MB_OK)
self.EndDialog(1)
style = (win32con.DS_MODALFRAME |
win32con.WS_POPUP|
win32con.WS_VISIBLE|
win32con.WS_CAPTION|
win32con.WS_SYSMENU|
win32con.DS_SETFONT)
childstyle = (win32con.WS_CHILD|win32con.WS_VISIBLE)
buttonstyle = win32con.WS_TABSTOP|childstyle
di = ['Python',
(0,0,300,180),
style,
None,
(8, 'MS Sans Serif')]
Button1 = (['Button',
"Button1",
1051,
(80,150,50,14),
buttonstyle|win32con.BS_PUSHBUTTON])
Button2 = (['Button',
"Button2",
1052,
(160,150,50,14),
buttonstyle|win32con.BS_PUSHBUTTON])
Stadic=(['Static',
"Python Dialog",
12,
(130, 50, 60, 14),
childstyle])
Edit = (['Edit',
"",
13,
(130, 80, 60, 14),
childstyle|win32con.ES_LEFT|win32con.WS_BORDER|win32con.WS_TABSTOP])
init = []
init.append(di)
init.append(Button1)
init.append(Button2)
init.append(Stadic)
init.append(Edit)
mydialog = MyDialog(init)
mydialog.DoModal()
# -*- coding:utf-8 -*-
#
import win32ui
import win32api
from win32con import *
from pywin.mfc import window
# define windows class
class MyWnd(window.Wnd):
def __init__(self):
window.Wnd.__init__(self, win32ui.CreateWnd())
self._obj_.CreateWindowEx(WS_EX_CLIENTEDGE,\
win32ui.RegisterWndClass(0, 0, COLOR_WINDOW+1),\
'MFC GUI', WS_OVERLAPPEDWINDOW,\
(10, 10, 800, 500), None, 0, None)
# 捕获右键单击消息
submenu = win32ui.CreatePopupMenu()
menu = win32ui.CreateMenu()
submenu.AppendMenu(MF_STRING, 1051,'&Open')
submenu.AppendMenu(MF_STRING, 1052,'&Close')
submenu.AppendMenu(MF_STRING, 1053,'&Save')
menu.AppendMenu(MF_STRING|MF_POPUP,submenu.GetHandle(),'&File')
submenu = win32ui.CreateMenu()
submenu.AppendMenu(MF_STRING, 1054, '&Copy')
submenu.AppendMenu(MF_STRING, 1055, '&Paste')
submenu.AppendMenu(MF_SEPARATOR, 1056, None)
submenu.AppendMenu(MF_STRING, 1057,'&Cut')
menu.AppendMenu(MF_STRING|MF_POPUP, submenu.GetHandle(), '&Edit')
submenu=win32ui.CreateMenu()
submenu.AppendMenu(MF_STRING, 1058, 'Tools')
submenu.AppendMenu(MF_STRING|MF_GRAYED, 1059, 'Setting')
m = win32ui.CreateMenu()
m.AppendMenu(MF_STRING|MF_POPUP|MF_CHECKED, submenu.GetHandle(),'Option')
menu.AppendMenu(MF_STRING|MF_POPUP,m.GetHandle(),'&Other')
self._obj_.SetMenu(menu)
# reload OnClose Method
def OnClose(self):
self.EndModalLoop(0)
# process the RClick event
w = MyWnd()
w.ShowWindow()
w.UpdateWindow()
w.RunModalLoop(1)
# -*- coding:utf-8 -*-
#
import win32ui
import win32api
from win32con import *
from pywin.mfc import window
# define windows class
class MyWnd(window.Wnd):
def __init__(self):
window.Wnd.__init__(self, win32ui.CreateWnd())
self._obj_.CreateWindowEx(WS_EX_CLIENTEDGE,\
win32ui.RegisterWndClass(0, 0, COLOR_WINDOW+1),\
'MFC GUI', WS_OVERLAPPEDWINDOW,\
(10, 10, 800, 500), None, 0, None)
# 捕获右键单击消息
self.HookMessage(self.OnRClick, WM_RBUTTONDOWN)
# reload OnClose Method
def OnClose(self):
self.EndModalLoop(0)
# process the RClick event
def OnRClick(self, param):
submenu = win32ui.CreatePopupMenu()
submenu.AppendMenu(MF_STRING, 1054, 'Copy')
submenu.AppendMenu(MF_STRING, 1055, 'Paste')
submenu.AppendMenu(MF_SEPARATOR, 1056, None)
submenu.AppendMenu(MF_STRING, 1057, 'Cut')
flag = TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON
submenu.TrackPopupMenu(param[5], flag, self)
w = MyWnd()
w.ShowWindow()
w.UpdateWindow()
w.RunModalLoop(1)
* 使用DLL中的菜单
* 处理菜单消息
# -*- coding:utf-8 -*-
#
import win32ui
import win32api
from win32con import *
from pywin.mfc import window
# define windows class
class MyWnd(window.Wnd):
def __init__(self):
window.Wnd.__init__(self, win32ui.CreateWnd())
self._obj_.CreateWindowEx(WS_EX_CLIENTEDGE,\
win32ui.RegisterWndClass(0, 0, COLOR_WINDOW+1),\
'MFC GUI', WS_OVERLAPPEDWINDOW,\
(10, 10, 800, 500), None, 0, None)
# create menu object
submenu = win32ui.CreatePopupMenu()
menu = win32ui.CreateMenu()
# add sth into menu
submenu.AppendMenu(MF_STRING, 1051,'&Open')
submenu.AppendMenu(MF_STRING, 1052,'&Close')
submenu.AppendMenu(MF_STRING, 1053,'&Save')
menu.AppendMenu(MF_STRING|MF_POPUP,submenu.GetHandle(),'&File')
# add menu into window
self._obj_.SetMenu(menu)
# set menu process message
self.HookCommand(self.MenuClick, 1051)
self.HookCommand(self.MenuClick, 1052)
self.HookCommand(self.MenuClick, 1053)
# reload OnClose Method
def OnClose(self):
self.EndModalLoop(0)
def MenuClick(self, lParam, wParam):
if lParam == 1051:
self.MessageBox('Open', 'Python', MB_OK)
elif lParam == 1053:
self.MessageBox('Save', 'Python', MB_OK)
else:
self.OnClose()
w = MyWnd()
w.ShowWindow()
w.UpdateWindow()
w.RunModalLoop(1)
PythonWin也是通过扩展的形式对MFC的函数进行封装的。
通过使用PythonWin中的win32gui和win32ui模块可以调用WindowsAPI,
或者使用MFC来创建GUI界面。