1
votes

I am trying to add menus to a window created in win32 API through VC++. This program generates two errors which I am not able to fix.

The code as following.

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);
}
2
There's a button in the question/answer text editor. It has ones and zeros on it and you can use it to format selected text as code.jscharf
What are the errors that you can't fix?dreamlax

2 Answers

1
votes

I don't know which errors you've got but I when I tried to compile your code I had only one error about type conversion

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

Also, I've noticed that you pass wrong second argument to DialogBox function. It expects a resource identifier of your dialog box converted to a LPCTSTR via MAKEINTRESOURCE. So you should add something like this

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

into your generic.rc file.

Hope this helps. Although it would be much better if you were more clear about your problem. And I suggest you study a bit MSDN windows programming section if you're about to do some GUI programming or at least let Visual Studio Wizard generate a simple win32 project for you and than study it.

1
votes
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);