Windows核心编程学习一:使用DialogBoxParam显示模式对话框

注:源码为学习《Windows核心编程》的一些尝试,非原创。若能有助于一二访客,幸甚。



1.DialogBoxParam

This function creates a modal dialog box from a dialog box template resource. Before displaying the dialog box, the function passes an application-defined value to the dialog box procedure as thelParam parameter of the WM_INITDIALOG message. An application can use this value to initialize dialog box controls.

 
   
int DialogBoxParam( 
  HINSTANCE hInstance, 
  LPCTSTR lpTemplateName, 
  HWND hWndParent, 
  DLGPROC lpDialogFunc, 
  PARAM dwInitParam
); 

2.最简单的尝试

/*******************************************************************************
 * File:	FirstTry.cpp
 * Author:	[email protected]
 * Time:	2013-04-14
 * 描述:	主要尝试使用DialogBoxParam函数显式一个对话框
 *******************************************************************************/

#include 
#include 
#include 
#include "Resource.h"


// 对话框过程函数
INT_PTR WINAPI Dlg_Proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	return FALSE;
}


int WINAPI _tWinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int)
{
	DialogBoxParam(hinstExe, MAKEINTRESOURCE(IDD_FIRSTTRY), NULL, Dlg_Proc, _ttoi(pszCmdLine));
	return 0;
}
运行结果:


但会发现,对话框不能被关闭。

3.对话框过程函数

/*******************************************************************************
 * File:	FirstTry.cpp
 * Author:	[email protected]
 * Time:	2013-04-14
 * 描述:	主要尝试使用DialogBoxParam函数显式一个对话框
 *******************************************************************************/

#include 
#include 
#include 
#include "Resource.h"


/* SetDlgMsgResult This macro maps to the SetWindowLong function.
 * SetWindowLong changes an attribute of the specified window, also sets a 32-bit (LONG) 
 * value at the specified offset into the extra window memory of a window.
 * The normal HANDLE_MSG macro in WindowsX.h does not work properly for dialog
 * boxes because DlgProc returns a BOOL instead of an LRESULT (likeWndProcs). 
 * This chHANDLE_DLGMSG macro corrects the problem.
 */
#define chHANDLE_DLGMSG(hWnd, message, fn)                 \
   case (message): return (SetDlgMsgResult(hWnd, uMsg,     \
      HANDLE_##message((hWnd), (wParam), (lParam), (fn))))


void Dlg_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
	switch (id) {
		case IDCANCEL:
			EndDialog(hwnd, id);
			break;
	}
}


// 对话框过程函数
INT_PTR WINAPI Dlg_Proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg) {
		chHANDLE_DLGMSG(hwnd, WM_COMMAND,	 Dlg_OnCommand);
	}

	return FALSE;
}


int WINAPI _tWinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int)
{
	DialogBoxParam(hinstExe, MAKEINTRESOURCE(IDD_FIRSTTRY), NULL, Dlg_Proc, _ttoi(pszCmdLine));
	return 0;
}

其中chHANDLE_DLGMSG是原书作者对HANDLE_MSG的改进。

你可能感兴趣的:(Windows核心编程,笔记)