2012-06-20 16 views
-1

我想用C++开发一个带有菜单的记事本。代码如下。在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 [email protected]' 

错误,这也[Linker error] undefined reference to [email protected]'

阿西夫

+3

你应该说错误是什么。 –

+2

请发布链接器错误,编译命令等,如果没有它,我们无法帮助您。 – Linuxios

+0

这是错误消息[链接器错误]未定义的引用'_ZN11AboutDialog10DialogProcEP6HWND__jjl @ 16',这也是[链接器错误]未定义的引用'_ZN11AboutDialog10DialogProcEP6HWND__jjl @ 16' –

回答

1

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

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

您确实需要修复缩进。

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

您还没有包含任何代码来创建和Run()MainWindow类的实例。你通常会在mainWinMain(你似乎都没有定义过)中这样做。

+0

我已经添加了Winmain。它编译好没有任何错误消息,但只有一个空白窗口。请指导在何处添加有关dailog的代码。谢谢, –

+0

您还需要一个资源文件来为初学者定义菜单和对话框模板。 –