• Win32封装对话框类


    [主程序入口.cpp]
    
    #include <windows.h>
    #include <tchar.h>
    #include "resource.h"
    #include "CMyApp.h"
    
    //这个还没用到,就先注释起来
    //HINSTANCE m_hInstance;
    HWND m_hWnd;
    int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
    {
    	CMyApp* mApp = new CMyApp;
    	m_hWnd=mApp->ShowDialog();
    
    	return 0;
    }
    
    //对话框父类的声明.h
    [CWinDialog.h]
    #pragma once
    #include <windows.h>
    #include <tchar.h>
    
    class CWinDialog
    {
    public:
    	CWinDialog(DWORD _IDD);
    	~CWinDialog();
    
    public:
    	HWND ShowDialog();
    	static INT_PTR CALLBACK WinDlgPrce(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    	INT_PTR CALLBACK MYWinDlgPrce(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    protected:
    	DWORD IDD;
    	HINSTANCE m_hInstance;
    	HWND m_hWnd;
    protected:
            //写的是虚函数,在子类去实例化
            //要用的消息就在这理写个虚函数,在子类中实例化,
            //相当于消息映射.
    	virtual BOOL OnInitDlg();    //初始化对话框
    	virtual BOOL OnRClick(WPARAM wParam, LPARAM lParam);    // 右键弹出菜单
    	virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);   // 右键菜单命令消息
    private:
    	
    };
    
    // 对话框父类的实现.cpp
    [CWinDialog.cpp]
    #include "CWinDialog.h"
    
    CWinDialog* TheApp;
    CWinDialog::CWinDialog(DWORD _IDD)
    {
    	IDD = _IDD;
    	m_hWnd = NULL;
    	m_hInstance = ::GetModuleHandleW(NULL);//获得程序的实例句柄
    	TheApp = this;
    }
    
    CWinDialog::~CWinDialog()
    {
    	delete TheApp;
    	delete m_hInstance;
    	delete m_hWnd;
    
    }
    
    //创建和显示对话框
    HWND CWinDialog::ShowDialog()
    {
    	HWND hWnd=::CreateDialog(m_hInstance, (LPCTSTR)IDD, NULL, WinDlgPrce);
    	if (!IsWindow(hWnd))
    		return 0;
    	ShowWindow(hWnd, SW_SHOW);
    	UpdateWindow(hWnd);
    
    	MSG msg;
    	while (GetMessage(&msg,NULL,0,0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return m_hWnd;
    }
    
    INT_PTR CALLBACK CWinDialog::WinDlgPrce(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {		
    	TheApp->MYWinDlgPrce(hWnd, uMsg, wParam, lParam);
    	return 0;
    
            // 最开始是用下面这个直接返回,但是在子类继承时用MessageBox弹出对话框,不响应消息.
            //return TheApp->MYWinDlgPrce(hWnd, uMsg, wParam, lParam);
    }
    
    //前几天看了个视频,就是讲这个回调函数静态如何调成员函数
    //就是用这种另外写一个成员的回调函数,所有的消息函数都写在这里面
    //原来的回调函数只相当于傀儡.
    INT_PTR CALLBACK CWinDialog::MYWinDlgPrce(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	m_hWnd = hWnd;
    
    	switch (uMsg)
    	{
    
    	case WM_COMMAND:
    		OnCommand(wParam, lParam);
    		return 1;
    
    	case WM_INITDIALOG:
    	{	
    		OnInitDlg();
    		return 1;
    	}
    	case WM_CLOSE:
    		DestroyWindow(hWnd);
    		return 0;
    
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    
    	case WM_CONTEXTMENU:
    		OnRClick(wParam,lParam);
    		return 1;
    	
    	}
    	return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
    
    BOOL CWinDialog::OnInitDlg()
    {
    	return TRUE;
    }
    
    BOOL CWinDialog::OnRClick(WPARAM wParam, LPARAM lParam)
    {
    	return TRUE;
    }
    
    BOOL CWinDialog::OnCommand(WPARAM wParam, LPARAM lParam)
    {
    	return TRUE;
    }
    
    //子类的声明文件.h
    [CMyApp.h]
    #pragma once
    #include "CWinDialog.h"
    #include "resource.h"
    
    class CMyApp :
    	public CWinDialog
    {
    public:
    	CMyApp();
    	~CMyApp();
    protected:
    	HWND hList;
    private:
         int InsertColumn(HWND hList, UINT nCol, LPCTSTR ColName, WORD ColFmtDaty,UINT ColWith);
    	int InsertItem(HWND hList, int nItem, LPCTSTR lpszText);
    	BOOL SetItemText(HWND hList, int nItem, int nSubItem, LPCTSTR lpszText);
    public:
        //下面是对父类虚函数实例化 BOOL OnInitDlg(); //BOOL OnNotify(WPARAM wParam, LPARAM lParam); BOOL OnRClick(WPARAM wParam, LPARAM lParam); BOOL OnCommand(WPARAM wParam, LPARAM lParam); }; //子类的实现 [CMyApp.cpp] #include "CMyApp.h" #include <commctrl.h> //#include <minwindef.h> CMyApp::CMyApp() :CWinDialog::CWinDialog(IDD_DIALOG1) { hList = NULL; } CMyApp::~CMyApp() { } BOOL CMyApp::OnInitDlg() { hList = ::GetDlgItem(m_hWnd, IDC_LIST1); SendMessage(hList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT); RECT rt; ::GetClientRect(hList, &rt); int nListColWith = rt.right - 20; //设置列标题 InsertColumn(hList, 0, L"ID", LVCFMT_LEFT, (int)(0.2*nListColWith)); InsertColumn(hList, 1, L"Name", LVCFMT_LEFT, (int)(0.2*nListColWith)); InsertColumn(hList, 2, L"Sex", LVCFMT_LEFT, (int)(0.1*nListColWith)); InsertColumn(hList, 3, L"Age", LVCFMT_LEFT, (int)(0.1*nListColWith)); InsertColumn(hList, 4, L"Tel", LVCFMT_LEFT, (int)(0.4*nListColWith)); //插入项数据 InsertItem(hList, 0, L"001"); SetItemText(hList, 0, 1, L"赵红梅"); SetItemText(hList, 0, 2, L"女"); SetItemText(hList, 0, 3, L"40"); SetItemText(hList, 0, 4, L"132001"); InsertItem(hList, 1, L"002"); SetItemText(hList, 1, 1, L"叶伟"); SetItemText(hList, 1, 2, L"男"); SetItemText(hList, 1, 3, L"40"); SetItemText(hList, 1, 4, L"132002"); return TRUE; } int CMyApp::InsertColumn(HWND hList, UINT nCol, LPCTSTR ColName, WORD ColFmtDaty, UINT ColWith) { LVCOLUMN col = { 0 }; col.cchTextMax = 256; col.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_FMT; col.fmt = LVCFMT_LEFT; col.cx = ColWith; col.pszText =(LPTSTR) ColName; col.iSubItem = nCol; return (int)SendMessage(hList, LVM_INSERTCOLUMN, nCol, (LPARAM)&col); } int CMyApp::InsertItem(HWND hList, int nItem, LPCTSTR lpszText) { LVITEM item = { 0 }; item.mask = LVCF_TEXT | LVCF_FMT; item.iItem = nItem; item.iSubItem = 0; item.pszText = (LPTSTR)lpszText; return(int)SendMessage(hList, LVM_INSERTITEM, 0, (LPARAM)&item); } BOOL CMyApp::SetItemText(HWND hList, int nItem, int nSubItem, LPCTSTR lpszText) { LVITEM lvi = { 0 }; lvi.iSubItem = nSubItem; lvi.pszText = (LPTSTR)lpszText; return (BOOL)SendMessage(hList, LVM_SETITEMTEXT, nItem, (LPARAM)&lvi); } BOOL CMyApp::OnRClick(WPARAM wParam, LPARAM lParam) { if ((HWND)wParam == hList) { HMENU hMenu = LoadMenu(m_hInstance, LPCWSTR(IDR_MENU1)); HMENU hSubMenu = GetSubMenu(hMenu, 0); POINT pt; pt.y = HIWORD(lParam); pt.x = LOWORD(lParam); TrackPopupMenu(hSubMenu, TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_TOPALIGN, pt.x, pt.y,0, m_hWnd, NULL); DestroyMenu(hMenu); } return TRUE; } BOOL CMyApp::OnCommand(WPARAM wParam, LPARAM lParam) { switch (LOWORD(wParam)) { case ID_POPUMENU_ADD:  //菜单添加消息 { MessageBox(m_hWnd, L"添加", L"菜单项提示", MB_OK); return 1; } case I_POPUMENU_DEL:  //菜单删除消息
          break:

    //....其他菜单命令消息
    } return 0; }

      

    签名:GreenLeaf1976
  • 相关阅读:
    otter安装、使用
    windows下xampp安装rabbitmq的PHP扩展AMQP
    CentOS7下安装RabbitMQ
    CentOS7下开放端口
    CentOS7下安装Redis
    @b.windows.last.use
    Rspec基本语法
    ruby firefox23报错:waiting for evaluate.js load failed
    notepad++上配置ruby执行环境
    cucumber的hooks
  • 原文地址:https://www.cnblogs.com/greenleaf1976/p/12820375.html
Copyright © 2020-2023  润新知