• win32 API编程之对话框


    一、简介

    二、分类

    三、对话框与控件的区别

    四、MessageBox函数

    五、创建模态对话框

    六、创建非模态对话框

    七、创建对话框主窗口程序

    一、简介

    二、分类

    三、对话框与控件的区别

    四、MessageBox函数

    1)函数详解

    2)代码实例

    #include<windows.h> 
    
    
    //自定义的窗口过程
    LRESULT  CALLBACK    MyWindowProc(HWND hWnd, UINT Msg,
        WPARAM wParam, LPARAM lParam)
    {
        switch (Msg)
        {
    
        case WM_DESTROY:
            PostQuitMessage(0);//可以使GetMessage返回0    
            return 0;
    
        case WM_LBUTTONDOWN:
        {
    
            //返回哪个按钮被点击
         int  ret=    MessageBox(hWnd, L"这是一个模态对话框!", L"提示",
                MB_YESNO|MB_ICONEXCLAMATION |  MB_DEFBUTTON2); 
         /*
     #define IDOK                1
    #define IDCANCEL            2
    #define IDABORT             3
    #define IDRETRY             4
    #define IDIGNORE            5
    #define IDYES               6
    #define IDNO                7
    #if(WINVER >= 0x0400)
    #define IDCLOSE         8
    #define IDHELP          9
         */
         bool  b = (ret == IDYES);
    
        }break;
         
        }
    
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    
    
    int  WINAPI   WinMain(HINSTANCE  hInstance, HINSTANCE  hPrevInstance,
        LPSTR lpCmdLine, int  nShowCmd)
    {
    
        //1.注册窗口类
        WNDCLASS  wnd;
        wnd.cbClsExtra = 0;
        wnd.cbWndExtra = 0;
        wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色
        wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标
        wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标
                                                    //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息
        wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数
        wnd.lpszClassName = L"MrHuang";//窗口类名
        wnd.lpszMenuName = NULL;//菜单资源名称
        wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式
        wnd.hInstance = hInstance;//实例句柄
        RegisterClass(&wnd);
    
    
        //创建窗口(返回之前发送WM_CREATE)
        HWND    hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序",
            WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL);
    
        //显示窗口
        ShowWindow(hWnd, nShowCmd);
        //更新窗口(发送WM_PAINT)
        UpdateWindow(hWnd);
    
        //消息循环(收到WM_QUIT消息退出)
        MSG  msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);//翻译消息
            DispatchMessage(&msg);//分发消息到窗口过程
        }
    }

    五、创建模态对话框

    1)DialogBox函数

    2)DialogProc函数

    3)WM_INITDIALOG消息

    4)WM_CTLCOLORDLG消息

    5)EndDialog函数

    6)代码实例

    #include<windows.h> 
    #include"resource.h"
    
    HBRUSH  bsh=NULL;
    
    //自定义的对话框过程
    INT_PTR  CALLBACK    MyDlgProc(HWND hDlg, UINT Msg,
        WPARAM wParam, LPARAM lParam)
    {
        switch (Msg)
        {
        case  WM_INITDIALOG://在对话框显示之前立即发送
        {
    
            HWND hEdit = CreateWindow(L"EdiT",
                L"这是一个编辑框控件哈哈哈",
                WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_NOHIDESEL,
                30, 100, 200, 100, hDlg, (HMENU)1111,
                GetModuleHandle(NULL), NULL);
    
            return  TRUE;//处理了消息
        }
    
        case  WM_CTLCOLORDLG :  //回忆前面学习的WM_CTLCOLORBTN
        {
            //创建画刷
            bsh=  CreateSolidBrush(RGB(255, 0, 0));
            return  (INT_PTR)bsh;
        }
    
    
        case  WM_COMMAND://菜单点击,按钮点击的消息
        {
            switch ( LOWORD(wParam))
            {
            case IDOK:
                //销毁对话框
                EndDialog(hDlg, 666 ); //这里的666将作为DialogBox的返回值
                DeleteObject(bsh);
                break;
            case IDCANCEL:
                EndDialog(hDlg, 999);
                DeleteObject(bsh);
                break;
            }
    
            return  TRUE;
        }
         
        }
            
        return FALSE;//没处理此消息
    }
    
    
    //自定义的窗口过程
    LRESULT  CALLBACK    MyWindowProc(HWND hWnd, UINT Msg,
        WPARAM wParam, LPARAM lParam)
    {
        switch (Msg)
        {
    
        case WM_DESTROY:
            PostQuitMessage(0);//可以使GetMessage返回0    
            return 0;
             
        case WM_LBUTTONDOWN:
        {
            //根据对话框资源创建模态对话框 
            int ret= DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), hWnd, MyDlgProc);
            ret = ret; 
        }break;
    
        }
    
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    
    
    int  WINAPI   WinMain(HINSTANCE  hInstance, HINSTANCE  hPrevInstance,
        LPSTR lpCmdLine, int  nShowCmd)
    {
    
        //1.注册窗口类
        WNDCLASS  wnd;
        wnd.cbClsExtra = 0;
        wnd.cbWndExtra = 0;
        wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色
        wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标
        wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标
                                                    //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息
        wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数
        wnd.lpszClassName = L"MrHuang";//窗口类名
        wnd.lpszMenuName = NULL;//菜单资源名称
        wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式
        wnd.hInstance = hInstance;//实例句柄
        RegisterClass(&wnd);
    
    
        //创建窗口(返回之前发送WM_CREATE)
        HWND    hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序",
            WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL);
    
        //显示窗口
        ShowWindow(hWnd, nShowCmd);
        //更新窗口(发送WM_PAINT)
        UpdateWindow(hWnd);
    
        //消息循环(收到WM_QUIT消息退出)
        MSG  msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);//翻译消息
            DispatchMessage(&msg);//分发消息到窗口过程
        }
    }

    六、创建非模态对话框

    1)CreateDialog函数

    2)IsDialogMessage函数

    3)代码实例

    #include<windows.h> 
    #include"resource.h"
    
    HBRUSH  bsh = NULL;
    HWND   hDlg = NULL;
    
    //自定义的对话框过程
    INT_PTR  CALLBACK    MyDlgProc(HWND hDlg, UINT Msg,
        WPARAM wParam, LPARAM lParam)
    {
        switch (Msg)
        {
        case  WM_INITDIALOG://在对话框显示之前立即发送
        {
    
            HWND hEdit = CreateWindow(L"EdiT",
                L"这是一个编辑框控件哈哈哈",
                WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_NOHIDESEL,
                30, 100, 200, 100, hDlg, (HMENU)1111,
                GetModuleHandle(NULL), NULL);
    
            return  TRUE;//处理了消息
        }
    
        case  WM_CTLCOLORDLG:  //回忆前面学习的WM_CTLCOLORBTN
        {
            //创建画刷
            bsh = CreateSolidBrush(RGB(255, 0, 0));
            return  (INT_PTR)bsh;
        }
    
    
        case  WM_COMMAND://菜单点击,按钮点击的消息
        {
            switch (LOWORD(wParam))
            {
            case IDOK:
            case IDCANCEL: 
                //销毁对话框
                DestroyWindow(hDlg);
                DeleteObject(bsh);
                break; 
            }
    
            return  TRUE;
        }
    
        }
    
        return FALSE;//没处理此消息
    }
    
    
    //自定义的窗口过程
    LRESULT  CALLBACK    MyWindowProc(HWND hWnd, UINT Msg,
        WPARAM wParam, LPARAM lParam)
    {
        switch (Msg)
        {
    
        case WM_DESTROY:
            PostQuitMessage(0);//可以使GetMessage返回0    
            return 0;
    
        case WM_LBUTTONDOWN:
        {
         //创建非模态对话框,返回窗口句柄 (DialogBox返回的是INT_PTR)
            hDlg =CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1)
                , hWnd, MyDlgProc);
            //显示非模态对话框
            ShowWindow(hDlg, SW_SHOW);
    
    
        }break;
    
        }
    
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    
    
    int  WINAPI   WinMain(HINSTANCE  hInstance, HINSTANCE  hPrevInstance,
        LPSTR lpCmdLine, int  nShowCmd)
    {
    
        //1.注册窗口类
        WNDCLASS  wnd;
        wnd.cbClsExtra = 0;
        wnd.cbWndExtra = 0;
        wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色
        wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标
        wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标
                                                    //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息
        wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数
        wnd.lpszClassName = L"MrHuang";//窗口类名
        wnd.lpszMenuName = NULL;//菜单资源名称
        wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式
        wnd.hInstance = hInstance;//实例句柄
        RegisterClass(&wnd);
    
    
        //创建窗口(返回之前发送WM_CREATE)
        HWND    hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序",
            WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL);
    
        //显示窗口
        ShowWindow(hWnd, nShowCmd);
        //更新窗口(发送WM_PAINT)
        UpdateWindow(hWnd);
    
        //消息循环(收到WM_QUIT消息退出)
        MSG  msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            if (IsDialogMessage(hDlg, &msg)) //对话框处理过
            {
                continue;
            }
            TranslateMessage(&msg);//翻译消息
            DispatchMessage(&msg);//分发消息到窗口过程
        }
    }

    七、创建对话框主窗口程序

    1)创建对话框为主窗口的步骤

    2)设置对话框图标

    3)代码实例

    #include<windows.h> 
    #include"resource.h"
    
    //自定义的对话框窗口过程
    INT_PTR   CALLBACK   MyDlgProc(HWND  hDlg, UINT  Msg, WPARAM  wParam, LPARAM  lParam)
    {
    
        switch (Msg)
        { 
    
        case WM_INITDIALOG:
        {
            //设置对话框标题
            SetWindowText(hDlg, L"用户登录");
            //设置对话框图标
            HICON  icon = LoadIcon(GetModuleHandle(NULL),
                MAKEINTRESOURCE(IDI_ICON1));
            SendMessage(hDlg, WM_SETICON, ICON_SMALL,
                (LPARAM)icon);
            DestroyIcon(icon);
    
    
            return  TRUE;
        }
    
        case    WM_COMMAND:
        {
            UINT  id = LOWORD(wParam);
    
            switch (id)
            {
            case ID_BTN_LOGIN :
            {
                TCHAR  name[64] = { 0 };
                GetDlgItemText(hDlg, IDC_EDIT_NAME, name, 64);
                TCHAR  pwd[64] = { 0 };
                GetDlgItemText(hDlg, IDC_EDIT_PWD, pwd, 64);
                TCHAR  str[256] = { 0 };
                wsprintf(str, L"用户名:%s  密码:%s", name, pwd);
                MessageBox(hDlg, str, L"提示", MB_OK);
            } 
                break;
    
            case IDCANCEL:
                DestroyWindow(hDlg);//销毁非模态对话框
                PostQuitMessage(0);//WM_QUIT退出消息循环
                break;
            }
    
            return TRUE;
        }
    
        default:
            break;
        }
    
        return  FALSE;
    }
    
    
    int  WINAPI   WinMain(HINSTANCE  hInstance, HINSTANCE  hPrevInstance,
        LPSTR lpCmdLine, int  nShowCmd)
    {
        //创建非模态对话框
        HWND  hDlg=CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG1),
            GetDesktopWindow(), MyDlgProc);
        //显示对话框
        ShowWindow(hDlg, SW_SHOW);
    
    
        //消息循环
        MSG  msg;
        while (GetMessage(&msg, NULL,  0,0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return   TRUE;
    }

     

     

  • 相关阅读:
    session 和 aplication 相关总结
    asp.net网站发布时碰到的一些问题
    考前防脑瘫防挂分预防针
    【游记】NOIP2021 白给记
    SELECT list is not in GROUP BY clause and contains nonaggregated
    StringUtils.isEmpty()
    gitLab生成SSH私钥后上传代码及获取代码
    tomcat配置https请求访问
    Mybatis:映射文件概述 & 增删改查
    Mybatis:核心文件概述
  • 原文地址:https://www.cnblogs.com/mango1997/p/14632306.html
Copyright © 2020-2023  润新知