MFC学习笔记3 Windows编程基础--DialogBox、回调、消息、控件

版权声明:(谢厂节的博客)博主文章绝大部分非原创,转载望留链接。 https://blog.csdn.net/xundh/article/details/78602319

对话框

在资源里新建对话框:
这里写图片描述

新建控件:
这里写图片描述

代码:定义回调函数

// test3.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"

BOOL CALLBACK MainProc(
                       HWND hwndDlg,
                       UINT uMsg,
                       WPARAM wParam,
                       LPARAM lParam)
{
    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    // TODO: Place code here.
    DialogBox(hInstance,(LPCSTR)IDD_DIALOG1, NULL,MainProc);
    return 0;
}

说明:
MainProc是消息回调函数,参数:

  • hwndDlg dialogbox的句柄
  • uMsg 消息类型
  • wParam 数据参数
  • lParam 第2个数据参数

代码: sprintf 在回调函数里输出参数值
sprintf 给字符串赋值

#include "stdio.h"
BOOL CALLBACK MainProc(
                       HWND hwndDlg,
                       UINT uMsg,
                       WPARAM wParam,
                       LPARAM lParam)
{
    char s[256];
    sprintf(s,"uMsg=%d,wParam=%d,lParam=%d \n", uMsg,wParam,lParam);
    OutputDebugString(s);

    return FALSE;
}

这里写图片描述

示例:点击按钮事件

BOOL CALLBACK MainProc(
                       HWND hwndDlg,
                       UINT uMsg,
                       WPARAM wParam,
                       LPARAM lParam)
{
    char s[256];
    sprintf(s,"uMsg=0x%x,wParam=%d,lParam=%d \n", uMsg,wParam,lParam);
    OutputDebugString(s);
    if(WM_COMMAND==uMsg){
        if(LOWORD(wParam)==IDCANCEL){
            EndDialog(hwndDlg,IDCANCEL);
        }else if(LOWORD(wParam)==IDOK){
            MessageBox(hwndDlg,"click ok","title",0);
        }
    }
    return FALSE;
}

示例:计算结果,控件取值赋值


BOOL CALLBACK MainProc(
                       HWND hwndDlg,
                       UINT uMsg,
                       WPARAM wParam,
                       LPARAM lParam)
{
    char s[256];
    sprintf(s,"uMsg=0x%x,wParam=%d,lParam=%d \n", uMsg,wParam,lParam);
    OutputDebugString(s);
    if(WM_COMMAND==uMsg){
        if(LOWORD(wParam)==IDCANCEL){
            EndDialog(hwndDlg,IDCANCEL);
        }else if(LOWORD(wParam)==IDOK){
            int nLeft = GetDlgItemInt(hwndDlg,IDC_LEFT,NULL,TRUE);
            int nRight = GetDlgItemInt(hwndDlg,IDC_RIGHT,NULL,TRUE);
            SetDlgItemInt(hwndDlg,IDC_RESULT,nLeft+nRight,TRUE);
        }
    }
    return FALSE;
}
阅读更多
换一批

没有更多推荐了,返回首页