2009-08-26 300 views
1

我想通過VC++將菜單添加到在win32 API中創建的窗口。這個程序會產生兩個我無法修復的錯誤。在VC++中通過Win32 API創建窗口(菜單創建)

代碼如下。

GENERIC.H

#define IDM_EXIT 100 
#define IDM_TEST 200 
#define IDM_ABOUT 300 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 
LRESULT CALLBACK About (HWND, UINT, WPARAM, LPARAM); 

GENERIC.RC

#include "windows.h" 
#include "generic.h" 
#include "winver.h" 
MYAPP ICON DISCARDABLE "GENERIC.ICO" 
MYAPP MENU DISCARDABLE 
BEGIN 
POPUP "&File" 
BEGIN 
MENUITEM "E&xit", IDM_EXIT 
END 
MENUITEM "&Test!", IDM_TEST 
POPUP "&Help" 
BEGIN 
MENUITEM "&About MyApp…", IDM_ABOUT 
END 
END 
1 VERSIONINFO 
FILEVERSION 3,3,0,0 
PRODUCTVERSION 3,3,0,0 
FILEFLAGSMASK 0x3fl 
#ifdef _DEBUG 
FILEFLAGS 0xbl 
#else 
FILEFLAGS 0xal 
#endif 
FILEOS 0X4L 
FILETYPE 0x1L 
FILESUBTYPE 0x0L 
BEGIN 
BLOCK "StringFileInfo" 
BEGIN 
BLOCK "040904B0" 
BEGIN 
VALUE "CompanyName", "Your Company\0" 
VALUE "FileDescription", "My Application\0" 
VALUE "FileVersion", "1.0\0" 
VALUE "InternalName", "MyApp\0" 
VALUE "LegalCopyright", "Copyright \251 Your Company. 
1995\0" 
VALUE "LegalTrademarks", "Microsoft\256 is a registered 
trademark of Microsoft 
Corporation. Windows (TM) is 
a trademark of Microsoft 
Corporation\0" 
VALUE "OriginalFilename", "\0" 
VALUE "ProductName", "MyApp\0" 
VALUE "ProductVersion", "1.0\0" 
END 
END 
BLOCK "VarFileInfo" 
BEGIN 
VALUE "Translation", 0x409, 1200 
END 
END 

GENERIC.C

#include <windows.h> 
#include "generic.h" 
HINSTANCE hInst; // current instance 
LPCTSTR lpszAppName = "MyApp"; 
LPCTSTR lpszTitle = "My Application"; 
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
        LPTSTR lpCmdLine, int nCmdShow) 
{ 
    MSG msg; 
    HWND hWnd; 
    WNDCLASSEX wc; 
    // In Windows 95 or Windows NT the hPrevInstance will always be NULL. 
    //................................................................... 
    wc.style = CS_HREDRAW | CS_VREDRAW; 
    wc.lpfnWndProc = (WNDPROC)WndProc; 
    wc.cbClsExtra = 0; 
    wc.cbWndExtra = 0; 
    wc.hInstance = hInstance; 
    wc.hIcon = LoadIcon (hInstance, lpszAppName); 
    wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
    wc.lpszMenuName = lpszAppName; 
    wc.lpszClassName = lpszAppName; 
    wc.cbSize = sizeof(WNDCLASSEX); 
    wc.hIconSm = LoadImage(hInstance, lpszAppName, 
     IMAGE_ICON, 16, 16, 
     LR_DEFAULTCOLOR); 
    if (!RegisterClassEx(&wc)) 
     return(FALSE); 
    hInst = hInstance; 
    hWnd = CreateWindow(lpszAppName, 
     lpszTitle, 
     WS_OVERLAPPEDWINDOW, 
     CW_USEDEFAULT, 0, 
     CW_USEDEFAULT, 0, 
     NULL, 
     NULL, 
     hInstance, 
     NULL 
     ); 
    if (!hWnd) 
     return(FALSE); 
    ShowWindow(hWnd, nCmdShow); 
    UpdateWindow(hWnd); 
    while(GetMessage(&msg, NULL, 0, 0)) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 
    return(msg.wParam); 
} 
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM 
         lParam) 
{ 
    switch(uMsg) 
    { 
    case WM_COMMAND : 
     switch(LOWORD(wParam)) 
     { 
     case IDM_TEST : 
      break; 
     case IDM_ABOUT : 
      DialogBox(hInst, "AboutBox", hWnd, (DLGPROC)About 
       ); 
      break; 
     case IDM_EXIT : 
      DestroyWindow(hWnd); 
      break; 
     } 
     break; 
    case WM_DESTROY : 
     PostQuitMessage(0); 
     break; 
    default : 
     return(DefWindowProc(hWnd, uMsg, wParam, lParam)); 
    } 
    return(0L); 
} 
LRESULT CALLBACK About(HWND hDlg, 
         UINT message, 
         WPARAM wParam, 
         LPARAM lParam) 
{ 
    switch (message) 
    { 
    case WM_INITDIALOG: 
     return (TRUE); 
    case WM_COMMAND: 
     if (LOWORD(wParam) == IDOK 
      || LOWORD(wParam) == IDCANCEL) 
     { 
      EndDialog(hDlg, TRUE); 
      return (TRUE); 
     } 
     break; 
    } 
    return (FALSE); 
} 
+0

問題/回答文本編輯器中有一個按鈕。它有1個和0個零部件,您可以使用它將選定的文本格式化爲代碼。 – jscharf 2009-08-26 06:59:37

+1

你無法修復的錯誤是什麼? – dreamlax 2009-08-26 07:13:57

回答

1

我不知道哪些錯誤ÿ ou've了,但我當我試圖編譯代碼我對類型轉換

wc.hIconSm = LoadImage(hInstance, lpszAppName, 
    IMAGE_ICON, 16, 16, 
    LR_DEFAULTCOLOR); 

也只有一個錯誤,我注意到,你傳遞錯誤的第二個參數DialogBox功能。它期望通過MAKEINTRESOURCE將對話框的資源標識符轉換爲LPCTSTR。所以你應該添加類似

IDD_ABOUTBOX DIALOG DISCARDABLE 34, 22, 217, 55 
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU 
CAPTION "About ..." 
FONT 8, "MS Sans Serif" 
BEGIN 
    LTEXT   "About me",1000,40,10,76,8 
    DEFPUSHBUTTON "OK",IDOK,176,6,32,14,WS_GROUP 
END 

到您的generic.rc文件中。

希望這會有所幫助。雖然如果你更清楚你的問題會好得多。我建議你學習一下MSDN windows programming section,如果你打算做一些GUI編程或者至少讓Visual Studio嚮導爲你生成一個簡單的win32項目,然後再研究它。

1
Generic.c 
----------- 
#define IDM_EXIT   100 
#define IDM_TEST   200 
#define IDM_ABOUT   300 
#define MYAPP1    400 
#define IDD_ABOUTBOX  500 
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); 
LRESULT CALLBACK About (HWND, UINT, WPARAM, LPARAM);