用C ++开发记事本

时间:2012-06-20 04:10:19

标签: c++ notepad

我正在尝试使用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'

阿西

1 个答案:

答案 0 :(得分:1)

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

您尚未从AboutDialog::Run返回值(您已定义了名为retval的变量,但尚未返回其值。)

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

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

您还没有包含任何代码来创建Run() MainWindow类的实例。您通常会在mainWinMain中执行该操作(您似乎都没有定义过这两种情况)。