代码之家  ›  专栏  ›  技术社区  ›  Ravi shankar

通过VC中的win32 api(菜单创建)打开窗口++

  •  1
  • Ravi shankar  · 技术社区  · 15 年前

    我正在尝试通过VC++将菜单添加到在win32 api中创建的窗口中。这个程序生成了两个我无法修复的错误。

    代码如下。

    泛型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);
    

    通用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
    

    通用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);
    }
    
    2 回复  |  直到 12 年前
        1
  •  1
  •   Serge    15 年前

    我不知道你有哪些错误,但当我试图编译你的代码时,我只有一个关于类型转换的错误。

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

    另外,我注意到你把错误的第二个论点传给了 DialogBox 功能。它期望通过将对话框的资源标识符转换为lpctstr MAKEINTRESOURCE . 所以你应该加上这样的东西

    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项目,然后再研究它。

        2
  •  1
  •   Ruben Rivero    12 年前
    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);