MVC the source code of the entire sample

Source: Internet
Author: User

The following is the source code for the whole MVC sample:

#windows应用程序 # Developer: Cai Junsheng (qq:9073204) Shenzhen 2014-9-24 #使用类来描写叙述from ctypes import *from ctypes.wintypes import *ws_ex_appwindow = 0x40000ws_overlappedwindow = 0xcf0000ws_caption = 0xc00000sw_shownormal = 1sw_show = 5cs_hredraw = 2CS_VREDRAW = 1CW_USE         DEFAULT = 0x80000000mk_lbutton = 0x0001mk_rbutton = 0x0002mk_shift = 0x0004mk_control = 0x0008mk_mbutton = 0x0010wm_null = 0wm_destroy = 2wm_nccreate = 0x0081wm_paint = 0x000fwm_mousemove = 0x0200WM_LBU Ttondown = 0x0201wm_lbuttonup = 0x0202wm_capturechanged = 0x0215white_brush = 0gwl_userdata = -21IDC_ARROW = 32512WNDPROCT YPE = Winfunctype (C_int, HWND, C_uint, WPARAM, LPARAM) #定义窗体类结构class wndclassex (Structure): _fields_ = [("Cbsize", C_uin                T), ("style", C_uint), ("Lpfnwndproc", Wndproctype), ("Cbclsextra", C_int),                ("Cbwndextra", C_int), ("HInstance", HANDLE), ("Hicon", HANDLE),   ("Hcursor", HANDLE),             ("Hbrush", HANDLE), ("Lpszmenuname", LPCWSTR), ("lpszClassName", LPCWSTR), ("Hiconsm", HANDLE)] #定义创建窗体的參数结构class createstructw (Structure): _fields_ = [("lpCreateParams", LPVOID), ("HInstance", HANDLE ), ("HMenu", HMenu), ("hWndParent", HWND), ("Cy", C_int), ("CX ", C_int), (" Y ", C_int), (" X ", C_int), (" style ", LONG), (" Lpsz Name ", LPCWSTR), (" Lpszclass ", LPCWSTR), (" dwExStyle ", DWORD)] #定义窗体画图结构class paintstruct (Str                Ucture): _fields_ = [(' hdc ', C_int), (' Ferase ', C_int), (' Rcpaint ', RECT),     (' Frestore ', C_int), (' Fincupdate ', C_int), (' rgbreserved ', C_wchar * +)] #窗体类class window: def __init__ (Self, hwnd): Self.hwnd = HWnd def Display (self, cmdshow): Windll.user32.sHowwindow (Self.hwnd, Cmdshow) Windll.user32.UpdateWindow (Self.hwnd) def capturemouse (self): Windll.user32 . SetCapture (Self.hwnd) def releasemouse (self): Windll.user32.ReleaseCapture () def hascapture (s ELF): Return False def isctrl (self, WParam): Return (WParam & mk_control)! = 0 def isshift (Self, WParam): Return (WParam & mk_shift)! = 0 def islbutton (self, WParam): Return (WParam & Mk_ Lbutton)! = 0 def ismbutton (self, WParam): Return (WParam & mk_mbutton)! = 0 def isrbutton (self, WPar AM): Return (WParam & mk_rbutton)! = 0 #窗体类型注冊类class winclassmaker:def __init__ (self, Wndpro C, ClassName, hInst): Self.wndclass = Wndclassex () self.wndClass.cbSize = sizeof (wndclassex) Self.wnd Class.style = Cs_hredraw | Cs_vredraw Self.wndClass.lpfnWndProc = WndProc Self.wndClass.cbClsExtra = 0 self.wndClass.cbWndExTra = 0 Self.wndClass.hInstance = HInst Self.wndClass.hIcon = 0 Self.wndClass.hCursor = Windll.user32 . LOADCURSORW (0, idc_arrow) Self.wndClass.hBrush = Windll.gdi32.GetStockObject (White_brush) self.wndClass.lpsz        MenuName = 0 Self.wndClass.lpszClassName = ClassName self.wndClass.hIconSm = 0 def Register (self): Return Windll.user32.RegisterClassExW (ByRef (self.wndclass)) #创建窗体class winmaker:def __init__ (self, className, hInst) : Self.classname = className Self.hinst = hInst Self.style = Ws_overlappedwindow |        Ws_caption Self.exstyle = 0 self.x = Cw_usedefault self.y = 0 Self.width = Cw_usedefault Self.height = 0 Self.hwndparent = hwnd (0) Self.hmenu = hwnd (0) Self.wndcreatdata = c_void_p (0) d EF Create (self, Wndcreatdata, title): Self.hwnd = Windll.user32.CreateWindowExW (Self.exstyle, S Elf.classname, title, Self.style, self.x, Self.y, Self.width, Self.height, Self.hwndparent, Self.hmenu, Self.hinst, Py_object (wndcreatdata)) if not self.hWnd:print (' Failed to create win        Dow ') exit (0) return Self.hwndclass controller:def __init__ (self): Self.hwnd = HWnd () Self.msgfun = {wm_destroy:self. OnDestroy, Wm_paint:self. OnPaint, Wm_mousemove:self. OnMouseMove, Wm_lbuttondown:self. OnLButtonDown, Wm_lbuttonup:self. OnLButtonUp, Wm_capturechanged:self. Oncapturechanged, Wm_null:self. Onnull #最后一个函数处理, convenient to insert function in front} def setwnd (self, hwnd): Self.hwnd = hwnd def GETW nd (self): return Self.hwnd def getpoint (self, lParam): pt = Point () pt.x = LParam & 0x FFFF Pt.y= (lparam>>16) & 0xFFFF return pt def mywndproc (self, hWnd, MSG, WParam, LParam): If Ms            G in Self.msgfun and Self.msgfun[msg] (WParam, LParam): return 0 Else:        Return Windll.user32.DefWindowProcW (HWnd, MSG, WParam, LParam) def onnull (self, WParam, LParam): Pass        Return False def ondestroy (self, WParam, LParam): Windll.user32.PostQuitMessage (0) print (' OnDestroy ') Return True def OnPaint (self, WParam, LParam): Pass return False def OnMouseMove (self, WParam, LParam): Pass return False def onlbuttondown (self, WParam, LParam): Pass return False de        F OnLButtonUp (self, WParam, LParam): Pass return False def oncapturechanged (self, WParam, LParam): Pass return False #窗体消息处理回调函数def pywndproc (hWnd, MSG, WParam, lParam): Pyctrl = Windll.user32.GetWindowLongW    (HWnd, Gwl_userdata)if Msg = = WM_NCCREATE:CREATSTRUCTW = Cast (LParam, POINTER (CREATESTRUCTW)). Contents Pyctrl = CREATSTRUCTW.LP        CreateParams Windll.user32.SetWindowLongW (hWnd, Gwl_userdata, Pyctrl) #获取控制类对象 CTRL = CAST (Pyctrl, Py_object). Value CTRL. Setwnd (hWnd) if Pyctrl: #获取控制类对象 Ctrl = CAST (Pyctrl, py_object). Value Return CTRL . Mywndproc (hwnd, MSG, WParam, LParam) Else:return Windll.user32.DefWindowProcW (hwnd, MSG, WParam, LParam ) #画布class Canvas:def __init__ (Self, hdc): SELF.HDC = hdc def gethdc (self): return self.hdc def Set Pixel (self, x, y, color): Windll.gdi32.SetPixel (SELF.HDC, x, y, color) def MoveTo (self, x, y): Windll.gdi 32.MoveToEx (SELF.HDC, x, y, 0) def LineTo (self, x, y): Windll.gdi32.LineTo (SELF.HDC, X, y) def line (s Elf, x1, y1, x2, y2): Windll.gdi32.MoveToEx (SELF.HDC, x1, y1, 0) windll.gdi32.LineTo (Self.hdc, x2, y2) def Text (self, x, y, text): Windll.gdi32.TextOutW (SELF.HDC, x, y, text, len (text)) d EF RGB (self, r, G, b): R = r & 0xFF g = g & 0xFF B = B & 0xFF return (b << 1 6) | (g << 8) | R class Paintcanvas (Canvas): "" "for OnPaint Message Processing" "" Def __init__ (Self, hwnd): Self.hwnd = hwnd SELF.PA int = paintstruct () hdc = Windll.user32.BeginPaint (Self.hwnd, ByRef (Self.paint)) canvas.__init__ (sel F, HDC) def __del__ (self): Windll.user32.EndPaint (Self.hwnd, ByRef (self.paint)) class Updatecanvas (Canvas): "" "For update Form" "" Def __init__ (Self, hwnd): HDC = Windll.user32.GetDC (hwnd) canvas.__init__ (self, hdc) SE Lf.hwnd = HWnd def __del__ (self): Windll.user32.ReleaseDC (Self.hwnd, SELF.HDC) #画笔class pen:def __init__ (sel        F, Penstyle, width, color): Self.hpe = Windll.gdi32.CreatePen (penstyle, width, color) def __del__ (self): WiNdll.gdi32.DeleteObject (SELF.HPE) def gethpe (self): return self.hpeclass holder:def __init__ (self, HDC, hobj ): SELF.HDC = hdc Self.old = Windll.gdi32.SelectObject (hdc, hobj) def __del__ (self): windll.gdi32. SelectObject (SELF.HDC, Self.old) #MVC类 # Model classes Class Model:def __init__ (self): Self.ptlist = [] Self.maxli ST = def addpoint (self, x, Y, Isstart): If Len (self.ptlist) > Self.maxList:self.ptList.remov        E (0) #以元组的方式保存到列表 self.ptList.append ((X,y,isstart)) def getpoint (self): return self.ptlist# view class        Class View:def __init__ (self): self.x = 0 self.y = 0 def Paint (self, canvas, ptlist): Pen = Pen (0, 0, canvas. RGB (255, 0, 0)) Penhold = Holder (canvas. GETHDC (), pen. GETHPE ()) self. Printpos (Canvas, self.x, SELF.Y) for PT in Ptlist:if pt[2]: canvas.          MoveTo (Pt[0], pt[1]) Else:      Canvas. LineTo (Pt[0], pt[1]) def printpos (self, canvas, X, y): self.x = x self.y = y strout = U ' Mo Use:%04d,%04d '% (x, y) canvas. Text (0, 0, strout) def MoveTo (self, canvas, x, Y, isVisible): If Isvisible:pen = Pen (0, 0, canvas. RGB (255, 0, 0)) Penhold = Holder (canvas. GETHDC (), pen. GETHPE ()) canvas. Line (self.x, Self.y, x, y) self. Printpos (canvas, x, y) #控制类 class Ctrlall (Controller): def __init__ (self): controller.__init__ (self) #调用基类 Construction Method Self.model = Model () Self.view = view () Self.win = Window (self. Getwnd ()) def OnPaint (self, WParam, lParam): Paint = Paintcanvas (self. Getwnd ()) Self.view.Paint (Paint, Self.model.GetPoint ()) return True def OnMouseMove (self, wParam, LParam) : Paint = Updatecanvas (self. Getwnd ()) pt = self. GetPoint (LParam) if Self.win.IsLButton (WParam):            Self.model.AddPoint (Pt.x, Pt.y, False) self.view.MoveTo (Paint, Pt.x, Pt.y, True) Else: Self.view.PrintPos (Paint, Pt.x, Pt.y) return True def onlbuttondown (self, WParam, LParam): Self.win . CaptureMouse () paint = Updatecanvas (self. Getwnd ()) pt = self.        GetPoint (LParam) self.view.MoveTo (Paint, Pt.x, Pt.y, False) self.model.AddPoint (Pt.x, Pt.y, True)    return True def onlbuttonup (self, WParam, LParam): Self.win.ReleaseMouse () return True #主函数入口 def main (): #程序实例句柄 hInst = Windll.kernel32.GetModuleHandleW (None) WndProc = Wndproctype (pywndproc) #窗体类名  Called ClassName = U ' shenzhencai ' #窗体标题 wname = U ' MVC ' #创建窗体类型 winclass = Winclassmaker (WndProc, ClassName, HInst) Winclass.register () #构造窗体对象 maker = Winmaker (ClassName, hInst) #控制类 ctrl = Ctrlall () #创建 Form win = Window (maker. Create (CTRL, Wname)) #创建显示 win.Display (sw_show) #消息循环 msg = MSG () lpmsg = pointer (msg) print (' Entering message loop ') while Windll.use R32. Getmessagew (lpmsg, 0, 0, 0)! = 0:windll.user32.translatemessage (lpmsg) Windll.user32.DispatchMessageW (lpmsg        ) print (' Done. ')           if __name__ = = "__main__": Print ("Win32 application in Python") Main ()


MVC the source code of the entire sample

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.