创建Windows模式拨号框 [英] Creating Windows modal dialox box

查看:59
本文介绍了创建Windows模式拨号框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在尝试使用visual C ++创建一个Windows对话框。

I am trying to create a Windows Dialog box using visual C++.

我设法创建一个简单的对话框包含一个小文本框的框。

I managed to create a simple dialog box with a small Textbox in it.

但我希望它是模态的。当我在该对话框外面点击时它不应该隐藏。

我现在卡住了。

But I wanted it to be modal. It should not hide when I clicked outside of that dialog box.
I am stuck at this point.

我在这里放置我的代码。

I am placing my piece of code here.

你可以告诉我在下面的代码中缺少什么来实现模态对话框吗?

Can you please tell what I am missing in the below code to achieve modal dialog box?

//---------------------------------------------------------------------------------------------------------------------
// inputDialogProc() - Callback procedure to initialize the input dialog box
//---------------------------------------------------------------------------------------------------------------------
INT_PTR CALLBACK inputDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {

	HWND hwndOwner;
	RECT rc, rcDlg, rcOwner;

	switch (uMsg) {
	case WM_INITDIALOG: {
							// Get the owner window and dialog box rectangles. 

							if ((hwndOwner = GetParent(hwndDlg)) == NULL)
							{
								hwndOwner = GetDesktopWindow();
							}

							GetWindowRect(hwndOwner, &rcOwner);
							GetWindowRect(hwndDlg, &rcDlg);
							CopyRect(&rc, &rcOwner);

							// Offset the owner and dialog box rectangles so that right and bottom 
							// values represent the width and height, and then offset the owner again 
							// to discard space taken up by the dialog box. 

							OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
							OffsetRect(&rc, -rc.left, -rc.top);
							OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);

							// The new position is the sum of half the remaining space and the owner's 
							// original position. 

							SetWindowPos(hwndDlg,
								HWND_TOP,
								rcOwner.left + (rc.right / 2),
								rcOwner.top + (rc.bottom / 2),
								0, 0,          // Ignores size arguments. 
								SWP_NOSIZE);

							if (GetDlgCtrlID((HWND)wParam) != GW_HWNDNEXT)
							{
								SetFocus(GetDlgItem(hwndDlg, GW_HWNDNEXT));
								return FALSE;
							}
							return TRUE;
	}
	case WM_COMMAND: {
						 WORD wNotifyCode = HIWORD(wParam);
						 WORD wID = LOWORD(wParam);
						 switch (wID) {

						 case IDOK:
							 SendMessageW(GetWindow(GetWindow(hwndDlg, GW_CHILD), GW_HWNDNEXT), WM_GETTEXT, sizeof(TheText), (LPARAM)&TheText);
							 SendMessage(hwndDlg, WM_CLOSE, 0, 0L);
							 return 1;
						 case IDCANCEL:
							 TheText[0] = 0;
							 SendMessage(hwndDlg, WM_CLOSE, 0, 0L);
							 return 1;
						 }
						 break;
	}
	case WM_CLOSE: {
					   EndDialog(hwndDlg, 0);
					   return 1;
	}
	}
	return 0;
}

//Dialog box creation
LRESULT createInputDialogBox(HINSTANCE hinst, HWND hwndOwner, LPWSTR lpszTitle, LPWSTR lpszMessage, LPWSTR lpszInitial) {
	HGLOBAL hgbl;
	LPDLGTEMPLATE lpdt;
	LPDLGITEMTEMPLATE lpdit;
	LPWORD lpw;
	LPWSTR lpwsz;
	LRESULT ret;
	int nchar;

	hgbl = GlobalAlloc(GMEM_ZEROINIT, 2048);
	lpdt = (LPDLGTEMPLATE)GlobalLock(hgbl);

	// Define a dialog box.

	lpdt->style = WS_POPUP | WS_BORDER | WS_SYSMENU | DS_MODALFRAME | WS_CAPTION | DS_SETFOREGROUND;
	lpdt->cdit = 4;         // Number of controls
	lpdt->x = 10;  lpdt->y = 10;
	lpdt->cx = 110; lpdt->cy = 50;

	lpw = (LPWORD)(lpdt + 1);
	*lpw++ = 0;             // No menu
	*lpw++ = 0;             // Predefined dialog box class (by default)

	for (lpwsz = (LPWSTR)lpw; *lpwsz++ = *lpszTitle++;);
	lpw = (LPWORD)lpwsz;

	//-----------------------
	// Define a static text control.
	//-----------------------
	lpw = lpwAlign(lpw);    // Align DLGITEMTEMPLATE on DWORD boundary
	lpdit = (LPDLGITEMTEMPLATE)lpw;
	lpdit->x = 5; lpdit->y = 5;
	lpdit->cx = 90; lpdit->cy = 10;
	lpdit->id = ID_TEXT;    // Text identifier
	lpdit->style = WS_CHILD | WS_VISIBLE | SS_LEFT;

	lpw = (LPWORD)(lpdit + 1);
	*lpw++ = 0xFFFF;
	*lpw++ = 0x0082;        // Static class

	for (lpwsz = (LPWSTR)lpw; *lpwsz++ = *lpszMessage++;);
	lpw = (LPWORD)lpwsz;
	*lpw++ = 0;             // No creation data

	//-----------------------
	// Define a Edit field.
	//-----------------------
	lpw = lpwAlign(lpw);    // Align DLGITEMTEMPLATE on DWORD boundary
	lpdit = (LPDLGITEMTEMPLATE)lpw;
	lpdit->x = 5; lpdit->y = 15;
	lpdit->cx = 90; lpdit->cy = 10;
	lpdit->id = ID_HELP;    // Help button identifier
	lpdit->style = WS_CHILD | WS_VISIBLE;

	lpw = (LPWORD)(lpdit + 1);
	*lpw++ = 0xFFFF;
	*lpw++ = 0x0081;        // Button class atom

	for (lpwsz = (LPWSTR)lpw; *lpwsz++ = *lpszInitial++;);
	lpw = (LPWORD)lpwsz;
	*lpw++ = 0;             // No creation data

	//-----------------------
	// Define an OK button.
	//-----------------------
	lpw = lpwAlign(lpw);    // Align DLGITEMTEMPLATE on DWORD boundary
	lpdit = (LPDLGITEMTEMPLATE)lpw;
	lpdit->x = 10; lpdit->y = 30;
	lpdit->cx = 40; lpdit->cy = 15;
	lpdit->id = IDOK;       // OK button identifier
	lpdit->style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON;

	lpw = (LPWORD)(lpdit + 1);
	*lpw++ = 0xFFFF;
	*lpw++ = 0x0080;        // Button class

	lpwsz = (LPWSTR)lpw;
	nchar = 1 + MultiByteToWideChar(CP_ACP, 0, "OK", -1, lpwsz, 50);
	lpw += nchar;
	*lpw++ = 0;             // No creation data

	//-----------------------
	// Define a Cancel button.
	//-----------------------
	lpw = lpwAlign(lpw);    // Align DLGITEMTEMPLATE on DWORD boundary
	lpdit = (LPDLGITEMTEMPLATE)lpw;
	lpdit->x = 60; lpdit->y = 30;
	lpdit->cx = 40; lpdit->cy = 15;
	lpdit->id = IDCANCEL;       // Cancel button identifier
	lpdit->style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON;

	lpw = (LPWORD)(lpdit + 1);
	*lpw++ = 0xFFFF;
	*lpw++ = 0x0080;        // Button class

	lpwsz = (LPWSTR)lpw;
	nchar = 1 + MultiByteToWideChar(CP_ACP, 0, "Cancel", -1, lpwsz, 50);
	lpw += nchar;
	*lpw++ = 0;             // No creation data

	GlobalUnlock(hgbl);
	ret = DialogBoxIndirect(hinst, (LPDLGTEMPLATE)hgbl, hwndOwner, (DLGPROC)inputDialogProc);

	GlobalFree(hgbl);
	return ret;
}

提前谢谢!

推荐答案

您是否尝试过 HWND_TOPMOST 而不是 HWND_TOP


这篇关于创建Windows模式拨号框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆