忆杰的博客

忆杰的博客

CreateDialogParam非模态对话框

恩, 非模态对话框有时候还是有些应用的.

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

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

BOOL InitDialog(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	HWND hWndDlg = NULL;
	MSG msg;

	hWndDlg = CreateDialogParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DialogProc, NULL);
	if(hWndDlg == NULL)
	{
		MessageBox(NULL, "创建对话框失败.", "", MB_OK);
		return 0;
	}

	RECT rtDlg;
	GetWindowRect(hWndDlg, &rtDlg);

	int nScreenX = GetSystemMetrics(SM_CXSCREEN);
	int nScreenY = GetSystemMetrics(SM_CYSCREEN);

	SetWindowPos(hWndDlg, 
				HWND_TOP, 
				nScreenX / 2 - rtDlg.right / 2,
				nScreenY / 2 - rtDlg.bottom / 2,
				0,
				0,
				SWP_NOSIZE | SWP_SHOWWINDOW); //SWP_NOSIZE)

	while(GetMessage(&msg, NULL, NULL, NULL))
	{
		if(msg.message == WM_KEYDOWN)
		{
			SendMessage(hWndDlg, msg.message, msg.wParam, msg.lParam);
		}
		else if(!IsDialogMessage(hWndDlg, &msg))// 如果消息没有被处理, 返回值为0
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}

BOOL InitDialog(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
/*
		响应WM_INITDIALOG消息时,如果函数调用SetFocus设置对话
	框中控制中的一个焦点, 则对话框应用程序应该返回零值,否则对
	话框应用程序应该返回非零值在,这种情况下系统对能够有焦点的
	对话框中的第一个控制设置焦点。 
*/
	return TRUE;
}

BOOL CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_INITDIALOG:
		return InitDialog(hWnd, uMsg, wParam, lParam);

	case WM_KEYDOWN:
		break;				

	case WM_COMMAND:
		if(LOWORD(wParam) == IDOK)
		{
		//	MessageBox(hWnd, "确定", "", MB_OK);
		}
		else if(LOWORD(wParam) == IDCANCEL)
		{
			DestroyWindow(hWnd);	
		}
		return TRUE;
	case WM_CLOSE:
		DestroyWindow(hWnd);
		return TRUE;

	case WM_DESTROY:
		PostQuitMessage(0);
		return TRUE;
	}

	return FALSE;	// 如果函数不处理消息,则对话框应用程序应该返回零值。
}

发表评论


Warning: Undefined variable $user_ID in /www/wwwroot/joenchen.com/wp-content/themes/agan/comments.php on line 66

您必须登录 才能进行评论。