• MFC架构


    MFC的六大关键技术:

    1)MFC程序的初始化过程

    2)消息映射

    3)运行时类型识别(RTTI)

    4)动态创建

    5)永久保存

    6)消息传递

    一、MFC的初始化过程:

    MFC的架构组成:

    1.要有CWinApp的派生类

    2.必须在全局区定义一个派生类的对象

    3.在CWinApp派生类中必须对InitInstance()函数进行重写

    【在MFC软件工程中,以APP类中的InitInstance()函数作为主函数,连接MFC的平台使用static Library静态链接库】

    #include "stdafx.h"
    
    class CMyApp:public CWinApp
    {
        virtual BOOL InitInstance()
        {        
            AfxMessageBox("xx");
            return TRUE;
        }
    };
    CMyApp theApp;
    /*
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
         // TODO: Place code here.
        MessageBox(NULL,"Win32测试MFC程序","测试",0);
        return 0;
    }
    */

    消息映射机制:
    1.必须使用类向导建立一个窗口类(CWin)的派生类
    2.必须建立派生类的对象,来接受客户界面返回的消息
    //在窗口派生类中,每个消息都与一个成员函数相对应
    3.消息映射函数,必须通过类向导(ClassWizard)建立

     

    Win32的消息处理机制:

    #include "stdafx.h"
    BOOL CALLBACK DialogProc(
      HWND hwndDlg,  // handle to dialog box
      UINT uMsg,     // message
      WPARAM wParam, // first message parameter
      LPARAM lParam  // second message parameter
    )
    {
        if(uMsg==WM_COMMAND)
        {
            if(LOWORD(wParam)==IDCANCEL)
            {
                EndDialog(hwndDlg,IDCANCEL);
                return TRUE;
            }
        }
        if(uMsg==WM_LBUTTONDOWN)
        {
            int x=LOWORD(lParam);
            int y=HIWORD(lParam);
            char ch[22];
            sprintf(ch,"(%3d,%3d)",x,y);
            //MessageBox(NULL,ch,"",MB_OK);
            SetWindowText(hwndDlg,ch);
        }
        if(uMsg==WM_MOUSEMOVE)
        {
            int x=LOWORD(lParam);
            int y=HIWORD(lParam);
            char ch[22];
            sprintf(ch,"(%3d,%3d)",x,y);
            //MessageBox(NULL,ch,"",MB_OK);
            SetWindowText(hwndDlg,ch);
        }
        return FALSE;
    
    }
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
         // TODO: Place code here.
        //MessageBox(NULL,"Win32测试MFC程序","测试",0);
        DialogBox(hInstance,(LPCSTR)IDD_DIALOG1,NULL,DialogProc);
        return 0;
    
    }

     MFC的消息处理机制

    //theApp:
    
    #include "stdafx.h"
    #include"MainDlg.h"
    class MyApp:public CWinApp
    {
        virtual BOOL InitInstance( )
        {
            //AfxMessageBox("cc",NULL,MB_OK);
            //CDialog *dlg=new CDialog(IDD_DIALOG1);
            //dlg->DoModal();
            MainDlg *dlg=new MainDlg();
            dlg->DoModal();
            return TRUE;
        }
    };
    MyApp theApp;
    //MainDlg派生类
    #include "stdafx.h"
    #include "MainDlg.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    /////////////////////////////////////////////////////////////////////////////
    // MainDlg dialog
    MainDlg::MainDlg(CWnd* pParent /*=NULL*/)
        : CDialog(MainDlg::IDD, pParent)
    {
        //{{AFX_DATA_INIT(MainDlg)
            // NOTE: the ClassWizard will add member initialization here
        //}}AFX_DATA_INIT
    }
    
    void MainDlg::DoDataExchange(CDataExchange* pDX)
    {
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(MainDlg)
            // NOTE: the ClassWizard will add DDX and DDV calls here
        //}}AFX_DATA_MAP
    }
    
    BEGIN_MESSAGE_MAP(MainDlg, CDialog)
        //{{AFX_MSG_MAP(MainDlg)
        ON_WM_LBUTTONDOWN()
        ON_WM_MOUSEMOVE()
        ON_WM_RBUTTONDOWN()
        //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // MainDlg message handlers
    BOOL MainDlg::OnInitDialog()
    {
        CDialog::OnInitDialog();
        //Title
        CDialog::SetWindowText("测试");
        //Icon
        HICON hicon=LoadIcon(AfxGetInstanceHandle(),(LPCSTR)IDI_ICON1);
        CDialog::SetIcon(hicon,TRUE);
        return TRUE;
    }
    void MainDlg::OnLButtonDown(UINT nFlags, CPoint point) 
    {
        // TODO: Add your message handler code here and/or call default
        
        CDialog::OnLButtonDown(nFlags, point);
        CString str;
        str.Format("(%d,%d)",point.x,point.y);
        if(MK_SHIFT&nFlags)
            str+="按下了Shift键";
        if(MK_CONTROL&nFlags)
            str+="按下了Ctrl键";
        if(MK_LBUTTON&nFlags)
            str+="按下了鼠标左键";
        if(MK_MBUTTON&nFlags)
            str+="按下了鼠标中键";
        if(MK_RBUTTON&nFlags)
            str+="按下了鼠标右键";
        AfxMessageBox(str);
        //CDialog::GetDlgItemText(IDC_TEXT_COORD,str);
    }
    void MainDlg::OnRButtonDown(UINT nFlags, CPoint point) 
    {
        // TODO: Add your message handler code here and/or call default
        
        CDialog::OnRButtonDown(nFlags, point);
        CDialog::OnLButtonDown(nFlags, point);
        CString str;
        str.Format("(%d,%d)",point.x,point.y);
        if(MK_SHIFT&nFlags)
            str+="按下了Shift键";
        if(MK_CONTROL&nFlags)
            str+="按下了Ctrl键";
        if(MK_LBUTTON&nFlags)
            str+="按下了鼠标左键";
        if(MK_MBUTTON&nFlags)
            str+="按下了鼠标中键";
        if(MK_RBUTTON&nFlags)
            str+="按下了鼠标右键";
        AfxMessageBox(str);
    }
    void MainDlg::OnMouseMove(UINT nFlags, CPoint point) 
    {
        // TODO: Add your message handler code here and/or call default
        
        CDialog::OnMouseMove(nFlags, point);
        CString str;
        str.Format("(%d,%d)",point.x,point.y);
        GetDlgItemText(IDC_TEXT_COORD,str);
    }
  • 相关阅读:
    字符串加密和解密的常类
    vs2013中使用nuget下载cefsharp winform包
    序列积第m小元素 二分答案优化
    贪心 park
    struct结构体 重载运算符
    最小生成树kruskal 知识点讲解+模板
    c++快读与快输模板
    MZOJ #82 总统竞选
    MZOJ #83 位运算
    MZOJ #81 最小得分和
  • 原文地址:https://www.cnblogs.com/mypsq/p/4900963.html
Copyright © 2020-2023  润新知