-1

我正在尝试用 C++ 开发一个带有菜单的记事本。代码如下。

#include <windows.h>
#define IDI_APP_ICON    1
#define IDD_ABOUT   100
#define IDC_STATIC  101
#define IDM_MAINMENU    200
#define IDM_FILE_NEW    201
#define IDM_FILE_OPEN   203
#define IDM_FILE_SAVE   204
#define IDM_FILE_EXIT   205
#define IDM_HELP_ABOUT  206

class MainWindow
{
public:
MainWindow(HINSTANCE hInstance);
~MainWindow();
static LRESULT CALLBACK MainWndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM       lParam);

static void OnCommand(HWND hwnd, int id, HWND hCtl, UINT codeNotify);
bool Run(int nCmdShow);

private:
WNDCLASSEX m_wndClass;
static HINSTANCE m_hInstance;
HWND m_hwnd;
static char m_szClassName[];
};

class AboutDialog
{
public:
AboutDialog();
~AboutDialog();
static BOOL CALLBACK DialogProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int Run(HINSTANCE hInstance, HWND hParent);

private:
HWND m_hwnd;
};

AboutDialog::AboutDialog()
{
}

AboutDialog::~AboutDialog()
{
}

// Function: Run
// Returns: Result of the DialogBox
int AboutDialog::Run(HINSTANCE hInstance, HWND hParent)

{
int retval = DialogBox(
    hInstance,
    MAKEINTRESOURCE(IDD_ABOUT), // Dialog template
    hParent, // Pointer to parent hwnd
    DialogProc);

}
BOOL CALLBACK
AboutDialog::DialogProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
int retVal = false;
switch(msg)
{
case WM_INITDIALOG:
    retVal = true;
    break;
case WM_COMMAND:
    if(LOWORD(wParam)== IDOK)
        EndDialog(hwnd, TRUE);
    break;
case WM_CLOSE:
    EndDialog(hwnd, TRUE);
    break;
}
return retVal;
}


char MainWindow::m_szClassName[] = "DrawLite";
HINSTANCE MainWindow::m_hInstance = NULL;

MainWindow::MainWindow(HINSTANCE hInstance)
{
m_hInstance = hInstance; // Save Instance handle

m_wndClass.cbSize = sizeof(WNDCLASSEX); // Must always be sizeof(WNDCLASSEX)
m_wndClass.style = CS_DBLCLKS; // Class styles
m_wndClass.lpfnWndProc = MainWndProc; // Pointer to callback procedure
m_wndClass.cbClsExtra = 0; 
m_wndClass.cbWndExtra = 0; 
m_wndClass.hInstance = hInstance;
m_wndClass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APP_ICON));
m_wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); 
m_wndClass.hbrBackground = (HBRUSH) (COLOR_WINDOW);
m_wndClass.lpszMenuName = MAKEINTRESOURCE(IDM_MAINMENU);
m_wndClass.lpszClassName = m_szClassName; 
m_wndClass.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APP_ICON));
}


MainWindow::~MainWindow()
{
}


LRESULT CALLBACK MainWindow::MainWndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM  lParam)
{
switch (msg)
{
case WM_DESTROY:
    PostQuitMessage (0);
    break;
case WM_COMMAND:
//HANDLE_WM_COMMAND(hwnd, wParam, lParam, OnCommand);
    break;
default:
    return DefWindowProc (hwnd, msg, wParam, lParam);
}

return 0;
}


void MainWindow::OnCommand(HWND hwnd, int id, HWND hCtl, UINT codeNotify)
{
switch(id)
{
case IDM_FILE_EXIT:
    PostQuitMessage(0);
    break;
case IDM_HELP_ABOUT:
    AboutDialog* dlg = new AboutDialog();
    dlg->Run(m_hInstance, hwnd);
    delete dlg; dlg = NULL;
    break;
}
}

bool MainWindow::Run(int nCmdShow)
{
if(!RegisterClassEx(&m_wndClass))
    return false;
m_hwnd = CreateWindowEx(
        0,
        m_szClassName,
        "Draw Lite",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        500,
        400,
        NULL,
        NULL,
        m_hInstance,
        NULL
        );
if(!m_hwnd)
    return false;
ShowWindow(m_hwnd, nCmdShow);
return true;
}
int WINAPI
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;

MainWindow *winMain = new MainWindow(hInst);
if(!winMain->Run(nCmdShow))
{
    delete winMain;
    return 1; // error
}

// Run the message loop. It will run until GetMessage() returns 0
while (GetMessage (&msg, NULL, 0, 0))
{
    // Translate virtual-key messages into character messages
    TranslateMessage(&msg);
    // Send message to WindowProcedure
    DispatchMessage(&msg);
}

delete winMain;

return msg.wParam;
}

我有一个链接器错误,不知道如何修复它,因为我在这个领域很新。我想知道是否有人可以帮助我。谢谢,

这是我得到的错误

[Linker error] undefined reference to _ZN11AboutDialog10DialogProcEP6HWND__jjl@16'

这也是[Linker error] undefined reference to _ZN11AboutDialog10DialogProcEP6HWND__jjl@16'

阿西夫

4

1 回答 1

2

虽然您在这里没有任何大错误,但您有一些较小的错误。正如链接器所说,您已经声明但尚未定义AboutDialog::DialogProc.

您还没有返回值AboutDialog::Run(您已经定义了一个名为 的变量retval,但还没有返回它的值)。

你真的需要修复你的缩进。

您没有主消息循环(可能应该在丢失的main/中WinMain)?

您还没有包含任何要创建的代码和类Run()的实例MainWindow。您通常会在mainor中执行此操作WinMain(您似乎都没有定义)。

于 2012-06-20T04:33:48.480 回答