前言

本文实例讲述了Python基于win32ui模块创建弹出式菜单。分享给大家供大家参考,具体如下:

本文实例讲述了Python创建普通菜单的方法。分享给大家供大家参考,具体如下:

本文实例讲述了Python处理菜单消息操作。分享给大家供大家参考,具体如下:

今天为大家介绍一个利用python开发创建弹出式菜单,基于python第三方库win32ui模块,模拟电脑桌面上面的复制,粘贴,剪切的各项功能,下面就来看看着实现过程吧

一、代码:

一、代码

一、代码

导入第三方库

# -*- coding:utf-8 -*-
#! python3
import win32ui
import win32api
from win32con import *
from pywin.mfc import window
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),\
                 'www.jb51.net - MFC GUI',WS_OVERLAPPEDWINDOW,\
                 (10,10,800,500),None,0,None)
    self.HookMessage(self.OnRClick,WM_RBUTTONDOWN)
  def OnClose(self):
    self.EndModalLoop(0)
  def OnRClick(self,param):
    submenu = win32ui.CreatePopupMenu()
    submenu.AppendMenu(MF_STRING,1054,'Copy')
    submenu.AppendMenu(MF_STRING,1055,'Paste')
    submenu.AppendMenu(MF_STRING,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)
# -*- coding:utf-8 -*-
#! python3
import win32ui
import win32api
from win32con import *
from pywin.mfc import window
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),\
                 'www.jb51.net - MFC GUI',WS_OVERLAPPEDWINDOW,\
                 (10,10,800,800),None,0,None)
    submenu = win32ui.CreateMenu()
    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_STRING,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)
  def OnClose(self):
    self.EndModalLoop(0)
w = MyWnd()
w.ShowWindow()
w.UpdateWindow()
w.RunModalLoop(1)
# -*- coding:utf-8 -*-
#! python3
import win32ui
import win32api
from win32con import *
from pywin.mfc import window
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),\
                 'www.jb51.net - MFC GUI',WS_OVERLAPPEDWINDOW,\
                 (10,10,800,500),None,0,None)
    submenu = win32ui.CreateMenu()
    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')
    self._obj_.SetMenu(menu)
    self.HookCommand(self.MenuClick,1051)
    self.HookCommand(self.MenuClick,1052)
    self.HookCommand(self.MenuClick,1053)
  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('Sava','python',MB_OK)
    else:
      self.OnClose()
w = MyWnd()
w.ShowWindow()
w.UpdateWindow()
w.RunModalLoop(1)

图片 1

二、运行效果:

二、运行效果:

二、运行效果:

创建窗口

图片 2

图片 3

图片 4

相关文章

网站地图xml地图