0
点赞
收藏
分享

微信扫一扫

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

对话框

在资源里新建对话框:
MFC学习笔记3 Windows编程基础--DialogBox、回调、消息、控件_git

新建控件:
MFC学习笔记3 Windows编程基础--DialogBox、回调、消息、控件_函数_02

代码:定义回调函数

// 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;
}

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

示例:点击按钮事件

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


举报

相关推荐

0 条评论