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

来源:互联网 发布:陌陌一键打招呼软件 编辑:程序博客网 时间:2024/06/06 08:52

对话框

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

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

代码:定义回调函数

// 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;}
原创粉丝点击