CreateDialog返回0但获得ERROR_SUCCESS

时间:2014-12-23 02:22:29

标签: c winapi visual-c++ user-interface

以下代码将0返回wnd,但在CreateDialog错误为ERROR_SUCCESS之后。对话框没有显示,我不明白这是怎么回事。这是一个控制台项目,如果相关,则在vs2013中创建对话框。

#include <windows.h>
#include "resource.h"

int main(){
    HWND wnd = CreateDialog(NULL, MAKEINTRESOURCE(IDD_DIALOG1), NULL, NULL);
    ShowWindow(wnd, SW_SHOWDEFAULT);
    UpdateWindow(wnd);
    MSG msg;
    while(GetMessage(&msg, NULL, 0, 0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

这必定是一些非常愚蠢的错误,但我看不到它。

2 个答案:

答案 0 :(得分:5)

首先需要初始化丰富的编辑库 - 如果未加载库,则不会注册该控件,并且对话框创建将失败。

请参阅http://msdn.microsoft.com/en-us/library/windows/desktop/hh298375(v=vs.85).aspx有关致电LoadLibrary的说明。您需要在创建对话框之前执行此操作。

或者设置DS_NOFAILCREATE样式应该允许创建对话框,尽管不会显示丰富的编辑控件。

答案 1 :(得分:0)

有同样的问题,我的 Dialog 不会出现,实际上是缺少 RichText2.0 控件加载。

这是我修复它的方式,它应该支持任何 Windows 版本。

将此包含添加到您的项目中

#include <richedit.h>       // To use richedit control

//Load RichText4.1 or 2.0/3.0 or 1.0 (last resort)  Control first. (3 different DLL's for full support).

//
//   FUNCTION: IsWinXPSp1Min()
//
//   PURPOSE: Return TRUE if operating sytem is Windows XP SP1 or later. 
//   Windows XP SP1 is the minimum system required to use a richedit v4.1 but only 
//   when UNICODE is defined.
//
BOOL IsWinXPSp1Min()
{
    OSVERSIONINFO osvi = { sizeof(osvi) };
    if (!GetVersionEx(&osvi))
    {
        return FALSE;
    }

    // Determine if system is Windows XP minimum
    if (osvi.dwMajorVersion >= 5 && osvi.dwMinorVersion >= 1)
    {
        // Now check if system is specifically WinXP and, if so, what service pack 
        // version is installed.
        if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
        {
            // The following test assumes that the szCSDVersion member of the 
            // OSVERSIONINFO struct's format will always be a string like 
            // "Service Pack x", where 'x' is a number >= 1. This is fine for SP1 
            // and SP2 but future service packs may have a different string 
            // descriptor.
            TCHAR* pszCSDVersion = L"Service Pack 1";
            if (osvi.szCSDVersion < pszCSDVersion)
            {
                return FALSE;
            }
        }
        return TRUE;
    }
    return FALSE;
}

//
//   FUNCTION: GetRichEditClassName()
//
//   PURPOSE: Load the proper version of RichEdit and return the class name.
//
PCWSTR GetRichEditClassName()
{
    HINSTANCE hLib;

    // Try to load latest version of rich edit control. Since v4.1 is available 
    // only as an UNICODE control on a minimum of Windows XP with service pack 1 
    // installed, use preprocessor conditional to ensure that an attempt to load 
    // Msftedit.dll is only made if UNICODE is defined.

#if defined UNICODE
    if (IsWinXPSp1Min())
    {
        // Try to get richedit v4.1, explicitly use wide character string as this 
        // is UNICODE only.
        hLib = LoadLibrary(L"msftedit.dll");
        if (hLib)
        {
            return MSFTEDIT_CLASS;
        }
    }
#endif

    // Cannot get latest version (v4.1) so try to get earlier one

    // Rich Edit Version 2.0/3.0
    hLib = LoadLibrary(L"riched20.dll");
    if (hLib)
    {
        // Version 2.0/3.0 is good
        return RICHEDIT_CLASS;
    }

    // Rich Edit Version 1.0
    hLib = LoadLibrary(L"riched32.dll");
    if (hLib)
    {
        // Version 1.0 is good
        return L"RichEdit";
    }

    // Cannot get any versions of RichEdit control (error)
    return L"";
}

resource.h 文件

#define IDC_RICHEDIT        3990

创建实际的 RichEdit 控件

// //   FUNCTION: OnInitRichEditDialog(HWND, HWND, LPARAM) // //   PURPOSE: Process the WM_INITDIALOG message // BOOL OnInitRichEditDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam) {
    RECT rc = { 20, 20, 160, 250 };

    HWND hRichEdit = CreateWindowEx(WS_EX_CLIENTEDGE, GetRichEditClassName(), 
        L"RichEdit Control", 
        ES_MULTILINE | ES_AUTOHSCROLL | WS_HSCROLL | ES_AUTOVSCROLL | 
        WS_VSCROLL | WS_CHILD | WS_VISIBLE, 
        rc.left, rc.top, rc.right, rc.bottom, 
        hWnd, reinterpret_cast<HMENU>(IDC_RICHEDIT), g_hInst, 0);
    if (hRichEdit)
    {
        SendMessage(hRichEdit, WM_SETFONT, reinterpret_cast<WPARAM>(g_hFont), TRUE);
    }

    return TRUE; }