码迷,mamicode.com
首页 > Windows程序 > 详细

win32 API编程之对话框

时间:2021-04-09 12:54:33      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:close   load   init   win   back   pwd   quit   菜单   rbac   

一、简介

二、分类

三、对话框与控件的区别

四、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;
}

 

 

 

win32 API编程之对话框

标签:close   load   init   win   back   pwd   quit   菜单   rbac   

原文地址:https://www.cnblogs.com/mango1997/p/14632306.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!