• C++实例 菜单操作


    #include <afxwin.h> 
    #include "menu.h"    //载入资源文件所使用之标头文件
    
    class MyFrame : public CFrameWnd
    {
    private:
        CMenu MainMenu, *SysMenu, *PopMenu, * SubMenu;
        COLORREF color;
    public:
        MyFrame()    //建构符
        {
            Create(NULL,    //产生标准视窗
                        "Menu",    //视窗标题
                        WS_OVERLAPPEDWINDOW|WS_VISIBLE,    //视窗样式
                        rectDefault,    //视窗大小
                        NULL,    //指向父视窗的指针
                        MAKEINTRESOURCE(IDR_SMENU));    //使用选单的识别符
    
            SysMenu = GetSystemMenu(FALSE);    //取得系统选单
            SysMenu->AppendMenu(IDM_INSERT, MF_SEPARATOR);    //插入分隔线
            SysMenu->AppendMenu(MF_STRING, IDM_INSERT, "Insert by me!");
                    //插入选项
            color = RGB(255,0,0);    //将点的颜色预设为红色
        }
    
        afx_msg void OnSwitchMenu()//当Switch Menu选项被选取时的回应函数
        {
            CMenu InsMenu;    //建立选单控件
        
            InsMenu.LoadMenu(IDR_INSMENU);    //载入选单资源
            MainMenu.LoadMenu(IDR_MAINMENU);    //载入选单资源
    
            MainMenu.AppendMenu(MF_POPUP, (UINT) InsMenu.m_hMenu,
                            "Color Menu");//增加一个选单
    
            SubMenu = MainMenu.GetSubMenu(1);    //取得第2个子选单的指针
    
            SubMenu->AppendMenu(MF_SEPARATOR);    //插入分隔线
            SubMenu->InsertMenu(IDM_CreateItem, MF_BYCOMMAND | MF_STRING,
                            IDM_InsertItem, "Insert a Item");    //插入选项
            SubMenu->InsertMenu(IDM_CreateItem, MF_BYCOMMAND | MF_POPUP,
                            (UINT) InsMenu.m_hMenu, "Insert a Submenu");    //插入子选单
            SetCheck();    //设定应被选取的选项
    
            SubMenu->InsertMenu(IDM_CreateItem, MF_BYCOMMAND | MF_SEPARATOR);
                        //插入分隔线
            InsMenu.Detach();    //将选单资源与选单控件分离
    
            SetMenu(&MainMenu);    //设定应用程序所使用之选单
        }
    
        afx_msg void OnCreateItem()
        {        //当Dynamic Menu选单的Create Item选项被选取时的回应函数
            MainMenu.ModifyMenu(IDM_CreateItem,MF_BYCOMMAND,
                                IDM_DeleteItem,"Delete Item");
            SubMenu = MainMenu.GetSubMenu(1);    //取得第2个子选单的指针
            SubMenu->AppendMenu(MF_STRING, IDM_NewItem, "New Item");
                    //增加Create item选项
        }
    
        afx_msg void OnDeleteItem()    
        {        //当Dynamic Menu选单的Delete Item选项被选取时的回应函数
            MainMenu.ModifyMenu(IDM_DeleteItem,MF_BYCOMMAND,
                                IDM_CreateItem,"Create Item");        
            SubMenu = MainMenu.GetSubMenu(1);    //取得第2个子选单的指针
            SubMenu->DeleteMenu(IDM_NewItem, MF_BYCOMMAND);    //删除选项
        }
    
        afx_msg void OnContextMenu(CWnd* pWnd, CPoint point) 
        {        //按下鼠标右键时,将执行此成员函数产生弹出选单
            CMenu menu;
    
            menu.LoadMenu(IDR_POPMENU);    //载入选单资源
    
            PopMenu = menu.GetSubMenu(0);    //取得第1个选单的指针
    
            PopMenu->TrackPopupMenu(TPM_CENTERALIGN|TPM_RIGHTBUTTON,
                point.x,point.y,
                this);    //建立弹出选单
            menu.Detach();
        }
    
        afx_msg void OnRed()    //当Color Menu中Red选项被选取时
        {
            SetUnCheck();    //取消原被打勾的选项
            color = RGB(255,0,0);    //设定画在画布上的颜色
            SetCheck();    //设定应被打勾的选项
        }
    
        afx_msg void OnGreen()    //当Color Menu中Green选项被选取时
        {
            SetUnCheck();
            color = RGB(0,255,0);
            SetCheck();
        }
    
        afx_msg void OnBlue()    //当Color Menu中Blue选项被选取时
        {
            SetUnCheck();
            color = RGB(0,0,255);
            SetCheck();
        }
    
        void SetCheck()    //判别color属性,将Color Menu中对应的选项打勾
        {
            switch(color)
            {
            case RGB(255,0,0) :
                MainMenu.CheckMenuItem(IDM_Red,MF_BYCOMMAND | 
                                    MF_CHECKED);    //将该选项打勾
                break;
            case RGB(0,255,0) :
                MainMenu.CheckMenuItem(IDM_Green,MF_BYCOMMAND | MF_CHECKED);
                break;
            case RGB(0,0,255) :
                MainMenu.CheckMenuItem(IDM_Blue,MF_BYCOMMAND | MF_CHECKED);
                break;
            }
        }
    
        void SetUnCheck()    //取消Color Menu中被打勾的选项
        {
            switch(color)
            {
            case RGB(255,0,0) :
                MainMenu.CheckMenuItem(IDM_Red,MF_BYCOMMAND | 
                            MF_UNCHECKED);    //取消该选项打勾
                break;    
            case RGB(0,255,0) :
                MainMenu.CheckMenuItem(IDM_Green,MF_BYCOMMAND | MF_UNCHECKED);
                break;
            case RGB(0,0,255) :
                MainMenu.CheckMenuItem(IDM_Blue,MF_BYCOMMAND | MF_UNCHECKED);
                break;
            }
        }
    
        afx_msg void OnLButtonUp(UINT nFlags, CPoint point)
        { ReleaseCapture(); }    //当鼠标左键放开后的回应函数,释放鼠标讯息接收权
    
        afx_msg void OnLButtonDown(UINT nFlags, CPoint point)
        { SetCapture(); }    //当鼠标左键按下后的回应函数,取得鼠标讯息接收权
    
        afx_msg void OnMouseMove(UINT nFlags, CPoint point)
        {                //当鼠标移动时的回应函数
            if (this == GetCapture())    //判断鼠标光标是否在视窗之上
            {
                CClientDC aDC(this);    //建立一个画布
                aDC.SetPixel(point, color);    //在画布上画出颜色为color的点
            }
        }
    
        DECLARE_MESSAGE_MAP()    //声明讯息映射表
    };
    
    //建立MyFrame类别的讯息映射表
    BEGIN_MESSAGE_MAP(MyFrame, CFrameWnd)
        ON_COMMAND(IDR_SwitchMenu, OnSwitchMenu)
        ON_COMMAND(IDM_CreateItem, OnCreateItem)
        ON_COMMAND(IDM_DeleteItem, OnDeleteItem)
        ON_COMMAND(IDM_Red, OnRed)
        ON_COMMAND(IDM_Green, OnGreen)
        ON_COMMAND(IDM_Blue, OnBlue)
        ON_WM_CONTEXTMENU()    //按下鼠标右键时,将呼叫OnContextMenu
                //回应鼠标讯息
        ON_WM_LBUTTONDOWN()
        ON_WM_MOUSEMOVE()
        ON_WM_LBUTTONUP()
    END_MESSAGE_MAP()
    
    class MyApp : public CWinApp    //应用程序类别
    {
    public:
        BOOL InitInstance()    //程序进入点
        {
            CFrameWnd *Frame = new MyFrame;
            m_pMainWnd = Frame;
            Frame->ShowWindow(SW_SHOW);    //显示视窗
            
            return true;
        }
    } a_app;    //声明应用程序控件
    学习笔记转摘于: 丝酷网 http://www.pythonschool.com/
  • 相关阅读:
    Redis 优化之 tcp-backlog
    linux下生成带符号的随机密码
    mysqldump导出sql文件中insert多行问题
    /usr/lib64/python2.6/site-packages/cryptography/__init__.py:26: DeprecationWarning: Python 2.6 is no longer supported by the Python core team
    ldconfig: /usr/lib/libpython2.6.so.1.0-gdb.py is not an ELF file
    [Errno 14] problem making ssl connection Trying other mirror.
    docker commit 显示“invalid reference format”
    (转)从Python的0.1输出0.1000000000000001说浮点数的二进制
    mysql中replicate_wild_do_table和replicate_do_db区别
    ipsec验证xl2tpd报错:handle_packet: bad control packet!
  • 原文地址:https://www.cnblogs.com/pythonschool/p/2956156.html
Copyright © 2020-2023  润新知