【发布时间】:2014-09-15 19:58:53
【问题描述】:
在我的 Win32 应用程序中,我注册了一个名为“AX”的自定义类。
.rc 文件中定义的以下对话框资源按预期工作:
IDD_DIALOG1 DIALOGEX 0, 0, 457, 219
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
CAPTION "Dialog"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
CONTROL "{8856F961-340A-11D0-A96B-00C04FD705A2}", IDC_EXPLORER1, "AX", WS_CHILD | WS_VISIBLE, 0, 0, 500, 400
END
但是,就我而言,我不能使用任何 .rc 文件。基本上,我需要在内存中构建 DLGTEMPLATE 并调用 DialogBoxIndirect。这是我的相关代码:
lpdt->style = DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_SYSMENU /*| DS_NOFAILCREATE*/;
lpdt->cdit = 1; // Number of controls
lpdt->x = 10; lpdt->y = 10;
lpdt->cx = 500; lpdt->cy = 400;
LPWORD lpw = (LPWORD)(lpdt + 1);
*lpw++ = 0; // No menu
*lpw++ = 0; // Predefined dialog box class (by default)
LPWSTR lpwsz = (LPWSTR)lpw;
int nchar = 1 + MultiByteToWideChar(CP_ACP, 0, "My Dialog", -1, lpwsz, 50);
lpw += nchar;
////-----------------------
//// Define the WebBrowser Control.
////-----------------------
lpw = lpwAlign(lpw);
LPDLGITEMTEMPLATE lpdit = (LPDLGITEMTEMPLATE)lpw;
lpdit->x = 0; lpdit->y = 0;
lpdit->cx = 300; lpdit->cy = 300;
lpdit->id = IDC_EXPLORER1;
lpdit->style = WS_CHILD | WS_VISIBLE;
// Class for the activeXControl
lpw = (LPWORD)(lpdit + 1);
*lpw++ = 0xFFFF;
*lpw++ = atom; // Here, atom was obtained from RegisterClassEx() for class name "AX"
lpw = (LPWORD) (lpdit + 1);
lpwsz = (LPWSTR) lpw;
nchar = 1 + MultiByteToWideChar (CP_ACP, 0, "{8856F961-340A-11D0-A96B-00C04FD705A2}", -1, lpwsz, 2048);
lpw += nchar;
*lpw++ = 0; // no creation data
GlobalUnlock(hgbl);
LRESULT ret = DialogBoxIndirect(hinst, (LPDLGTEMPLATE)hgbl, hwndOwner, DialogProc);
GlobalFree(hgbl);
if (ret == -1) {
int err = ::GetLastError();
ReportError(err);
}
DialogBoxIndirect 的返回码是 -1。但是,GetLastError() 返回 0。奇怪。
如果我在对话框样式中指定 DS_NOFAILCREATE,则会创建对话框。但是,我需要的控件丢失了。
请帮助我找出我的代码中的问题。问候。
【问题讨论】:
-
你正确初始化COM了吗?