WinApi CreateDialog返回错误715

时间:2012-01-26 17:33:59

标签: c++ visual-studio-2010

我正在尝试将这个简单的Windows对话框生成,但是一旦启动它就会报告“错误x715”,这意味着hDialog函数中未正确生成int WINAPI WinMain()。它编译得很好。

我在Visual Studio 2010中工作,它是一个“Visual C ++ - > Empty Project”项目。

这是完整的main.cpp文件,它是项目中唯一的文件:

#include "windows.h"

#define DLG_MAIN 200 // ID for dialog
#define DLG_ICON 30000 // IDs for icons
#define DLG_ICON_S 30000
#define IDC_QUIT 1011 // ID for "quit"-button
#define IDC_INFO 2000 // ID for "info"-button
#define ID_TIMER 1 // ID for timer
#define IDC_STATIC -1 // ID for all labels
#define TIMER_INTERRUPT 500 // timer msg interval in msec 

HINSTANCE TheInstance = 0; // instance handle of this program

// Our main function
void lalala(HWND hwnd)
{
    /*Doesn't do anything yet.*/
}

// Windows passes messages to application windows to indicate "something"
// needs to be done
BOOL CALLBACK DialogProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_INITDIALOG:
        // set the time to generate a timer message (WM_TIMER)
        SetTimer(hwnd, ID_TIMER, TIMER_INTERRUPT, NULL);
        return TRUE;

        case WM_TIMER:
        // a timer msg was received so call our main function!
        lalala(hwnd);
        return TRUE;

        case WM_COMMAND:
        switch (LOWORD(wParam)) 
        {
            case IDC_INFO:
            // the info button on the window was pressed
            MessageBox(hwnd, "<show some info>", "The Jonas Brothers are way better than Nick Cave ever was.", MB_OK);
            return TRUE;

            case IDC_QUIT:
            // the quit button on the window was pressed
            PostQuitMessage(0);
            return TRUE;
        }
        return TRUE;

        case WM_DESTROY:
        // this app is about to be closed so kill the timer
        KillTimer(hwnd, ID_TIMER);
        PostQuitMessage(0);
        return TRUE;

        case WM_CLOSE:
        // destroy the window
        DestroyWindow (hwnd);
        return TRUE;
    }
return FALSE;
} 

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{
    TheInstance = hInst;
    HWND hDialog = 0;

    hDialog = CreateDialog (hInst, MAKEINTRESOURCE (DLG_MAIN), 0, (DLGPROC)DialogProc);
    if (!hDialog)
    {
        char buf [100];
        wsprintf (buf, "Error x%x", GetLastError ());
        MessageBox (0, buf, "CreateDialog", MB_ICONEXCLAMATION | MB_OK);
        return 1;
    }

    HICON hIcon = LoadIcon (TheInstance, MAKEINTRESOURCE (DLG_ICON));
    SendMessage (hDialog, WM_SETICON, WPARAM (TRUE), LPARAM (hIcon));
    hIcon = LoadIcon (TheInstance, MAKEINTRESOURCE (DLG_ICON_S));
    SendMessage (hDialog, WM_SETICON, WPARAM (FALSE), LPARAM (hIcon));

    MSG msg;
    int status;
    while ((status = GetMessage (&msg, 0, 0, 0)) != 0)
    {
        if (status == -1) return -1;

        if (!IsDialogMessage (hDialog, &msg))
        {
            TranslateMessage ( &msg );
            DispatchMessage ( &msg );
        }
    }

    return msg.wParam;
}

有人可以告诉我为什么它在hDialog = CreateDialog (hInst, MAKEINTRESOURCE (DLG_MAIN), 0, (DLGPROC)DialogProc);失败了吗?

1 个答案:

答案 0 :(得分:1)

错误0x715是ERROR_RESOURCE_NAME_NOT_FOUND,当您在资源部分找不到您提供的名称的对话框时,会得到错误。不要为每个资源声明宏,只需使用#include "resource.h"