您的位置:首页 > 编程语言

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

2017-11-22 13:27 627 查看

对话框

在资源里新建对话框:



新建控件:



代码:定义回调函数

// 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: