Compiler: DEV BloodShed C++ (latest release)

I am following this windows api tutorial and am at this step
http://www.winprog.org/tutorial/menus.html

//FILE: resource.h
// the below code is in a resource.h file
// as stated in the instructions.
#define IDR_MYMENU 101
#define IDI_MYICON 201
#define ID_FILE_EXIT 9001
#define ID_STUFF_GO 9002

//Next File in the API project:
//FILE: resource.rc

#include "resource.h"

IDR_MYMENU MENU
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "E&xit", ID_FILE_EXIT
    END

    POPUP "&Stuff"
    BEGIN
        MENUITEM "&Go", ID_STUFF_GO
        MENUITEM "G&o somewhere else", 0, GRAYED
    END
END

IDI_MYICON ICON "menu_one.ico"

// This is the main entry point for the application
// at the very bottom of this source code I list the error I get
// from the compiler.

#include <windows.h>
#include "resource.h"

const char g_szClassName[] = "myWindowClass";

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = MAKEINTRESOURCE(101);
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }   

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

//ERROR WHEN I COMPILE THIS CODE
// ERRORS: expected unqualified-id before numeric constant
// expected ',' or ';' before numeric constant
// and it points to IDR_MYMENU MENU as the problem ???


//THE ODD part is... is the second part of the tutorial
// states I can code the menu on the fly
// the below code works absolutely fine!
// WHY ??

#include <windows.h>

#define ID_FILE_EXIT 9001
#define ID_STUFF_GO 9002

const char g_szClassName[] = "myWindowClass";

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
               case WM_CREATE:
        {
            HMENU hMenu, hSubMenu;

            hMenu = CreateMenu();

            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");

            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO, "&Go");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Stuff");

            SetMenu(hwnd, hMenu);


            /*hIcon = LoadImage(NULL, "menu_two.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
            if(hIcon)
                SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
            else
                MessageBox(hwnd, "Could not load large icon!", "Error", MB_OK | MB_ICONERROR);

            hIconSm = LoadImage(NULL, "menu_two.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
            if(hIconSm)
                SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
            else
                MessageBox(hwnd, "Could not load small icon!", "Error", MB_OK | MB_ICONERROR);
        */
        }
        break;
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case ID_FILE_EXIT:

                break;
                case ID_STUFF_GO:

                break;
            }
        break;

        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }   

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

Recommended Answers

All 8 Replies

No idea why you get that error, I compiled your code and the resource, worked flawlessly.

Hi Cody!

I didn't run your 2nd program but was able to compile the first one you seem to be having troubles with. I used the Dev-C++ development suite. However, I had to comment out this line because I didn't have the icon file which you apparently have...

//IDI_MYICON ICON "menu_one.ico"

That would have been in your resource script which I named resource.rc for when I built your program. What development system are you using? I have a number of others, but just tried Dev-C++ because that's the quickest to put a little project together.

Here's another example for you of one of my learning programs...

//Main.c
#include <windows.h>
#include <commdlg.h>
#include <string.h>
#include "Form8.h"

typedef struct WindowsEventArguments
{
 HWND hWnd;
 WPARAM wParam;
 LPARAM lParam;
 HINSTANCE hIns;
}WndEventArgs,*lpWndEventArgs;


BOOL CALLBACK ModalDlgProc(HWND hwndDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
 char szName[64],szText[64];
 HWND hwndEdit;
 UINT nID;

 switch(message)
 {
  case WM_INITDIALOG:
    hwndEdit=GetDlgItem(hwndDlg,IDC_NAME);
    SetFocus(hwndEdit);
    return FALSE;
  case WM_COMMAND:
    nID = LOWORD(wParam);
    switch(nID)
    {
     case IDOK:
       hwndEdit=GetDlgItem(hwndDlg, IDC_NAME);
       GetWindowText(hwndEdit,szText,64);
       strcpy(szName,"Hello, ");
       strcat(szName,szText);
       strcat(szName,"!");
       MessageBox(hwndDlg,szName,"Form8",MB_OK);
     case IDCANCEL:
       EndDialog(hwndDlg, nID);
       break;
     default:
       break;
    }
    return TRUE;
  default:
    return FALSE;
 }
}


long WndProc_OnCreate(lpWndEventArgs Wea)
{
 HWND hTextBox,hButton;
 DWORD dwStyle;

 Wea->hIns=((LPCREATESTRUCT)Wea->lParam)->hInstance;
 dwStyle=WS_CHILD|WS_VISIBLE|WS_BORDER;
 hTextBox=CreateWindow("edit","",dwStyle,10,30,370,20,Wea->hWnd,(HMENU)IDC_EDITBOX1,Wea->hIns,0);
 hButton=CreateWindow("button","Exit",WS_CHILD|WS_VISIBLE,168,75,60,25,Wea->hWnd,(HMENU)IDC_BUTTON,Wea->hIns,0);

 return 0;
}


void WndProc_OnCommand_OnOpen(lpWndEventArgs Wea)
{
 static char szFilter[]="C Files (*.C),CPP Files (*.cpp)\0*.c;*.cpp\0\0";
 static char szTitleName[_MAX_FNAME+_MAX_EXT];
 static szFileName[_MAX_PATH];
 char lpszBuffer[128];
 OPENFILENAME ofn;
 
 GetCurrentDirectory(128,lpszBuffer);
 memset(&ofn,'\0',sizeof(OPENFILENAME));
 ofn.lStructSize=sizeof(OPENFILENAME);
 ofn.lpstrFilter = szFilter;
 ofn.nMaxFile=_MAX_PATH;
 ofn.nMaxFileTitle=_MAX_FNAME+_MAX_EXT;
 ofn.lpstrInitialDir = lpszBuffer;
 ofn.lpstrDefExt = "Sdb";
 ofn.hInstance=Wea->hIns;
 ofn.hwndOwner = Wea->hWnd;
 ofn.Flags=OFN_HIDEREADONLY | OFN_CREATEPROMPT;
 ofn.lpstrFile=szFileName;
 ofn.lpstrFileTitle=szTitleName;
 GetOpenFileName(&ofn);
 SetWindowText(GetDlgItem(Wea->hWnd,IDC_EDITBOX1),ofn.lpstrFile);

 return;
}


void WndProc_OnCommand_OnSave(lpWndEventArgs Wea)
{
 MessageBox(Wea->hWnd,"You Chose File >> Save","Form8",MB_OK);
}


void WndProc_OnCommand_OnExit(lpWndEventArgs Wea)
{
 SendMessage(Wea->hWnd,WM_DESTROY,0,0L);
}


void WndProc_OnCommand_OnHelp(lpWndEventArgs Wea)
{
 MessageBox(Wea->hWnd,"There Is No Help.","Form8",MB_OK);
}


void WndProc_OnCommand_OnHelpAbout(lpWndEventArgs Wea)
{
 DialogBox(Wea->hIns,MAKEINTRESOURCE(IDD_ABOUT),Wea->hWnd,ModalDlgProc);
}


void WndProc_OnCommand_OnButton_Click(lpWndEventArgs Wea)
{
 MessageBox(Wea->hWnd,"You Clicked The Button And Want To Exit!","Form8",MB_OK);
 SendMessage(Wea->hWnd,WM_DESTROY,0,0L);
}


long WndProc_OnCommand(lpWndEventArgs Wea)
{
 switch (LOWORD(Wea->wParam))
 {
  case IDM_OPEN:
    WndProc_OnCommand_OnOpen(Wea);
    break;
  case IDM_SAVE:
    WndProc_OnCommand_OnSave(Wea);
    break;
  case IDM_EXIT:
    WndProc_OnCommand_OnExit(Wea);
    break;
  case IDM_HELP:
    WndProc_OnCommand_OnHelp(Wea);
    break;
  case IDM_ABOUT:
    WndProc_OnCommand_OnHelpAbout(Wea);
    break;
  case IDC_BUTTON:
    WndProc_OnCommand_OnButton_Click(Wea);
    break;
 }

 return 0;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
 static WndEventArgs wea;

 switch (iMsg)
 {
  case WM_CREATE:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
    return WndProc_OnCreate(&wea);
  case WM_COMMAND:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
    return WndProc_OnCommand(&wea);
  case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
 }

 return DefWindowProc(hwnd,iMsg,wParam,lParam);
}

int WINAPI WinMain(HINSTANCE hIns,HINSTANCE hPrIns,PSTR szCmdLn,int iCmdShow)
{
 char szAppName[]="Form8";
 WNDCLASSEX wndclass;
 HWND hwnd;
 MSG msg;

 wndclass.cbSize=sizeof(wndclass);
 wndclass.style=CS_HREDRAW|CS_VREDRAW;
 wndclass.lpfnWndProc=WndProc;
 wndclass.cbClsExtra=0;
 wndclass.cbWndExtra=0;
 wndclass.hInstance=hIns;
 wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
 wndclass.hCursor=LoadCursor(NULL,IDC_ARROW) ;
 wndclass.hbrBackground=(HBRUSH)(COLOR_BTNFACE+1);
 wndclass.lpszMenuName=MAKEINTRESOURCE(IDR_MAIN_MENU);
 wndclass.lpszClassName=szAppName;
 wndclass.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
 RegisterClassEx(&wndclass);
 hwnd=CreateWindow(szAppName,"Form8",WS_OVERLAPPEDWINDOW,200,120,400,175,NULL,0,hIns,NULL);
 ShowWindow(hwnd,iCmdShow);
 UpdateWindow(hwnd);
 while(GetMessage(&msg,NULL,0,0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }

 return msg.wParam;
}
//Form8.h
/*Form8.h header file*/
#define IDC_STATIC      -1
#define IDR_MAIN_MENU   1240
#define IDM_OPEN        1250
#define IDM_SAVE        1252
#define IDM_EXIT        1254
#define IDM_HELP        1260
#define IDM_ABOUT       1262
#define IDC_NAME        1300
#define IDD_ABOUT       1400
#define IDC_EDITBOX1    1500
#define IDC_BUTTON      1510
/* Form8.rc */
#include <windows.h>
#include "Form8.h"

IDR_MAIN_MENU MENU
{
 POPUP "&File"
 {
  MENUITEM "&Open...",          IDM_OPEN
  MENUITEM "&Save...",          IDM_SAVE
  MENUITEM SEPARATOR
  MENUITEM "E&xit",             IDM_EXIT
 }
 POPUP "&Help"
 {
  MENUITEM "&Help...",          IDM_HELP
  MENUITEM "&About Form8...",   IDM_ABOUT
 }
}

IDD_ABOUT DIALOG DISCARDABLE 30,60,180,36
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Identify Your Sorry Self!"
FONT 8, "MS Sans Serif"
BEGIN
  DEFPUSHBUTTON  "OK",IDOK,120,20,40,14
  PUSHBUTTON     "Cancel",IDCANCEL,75,20,40,14
  LTEXT          "Enter Your Name",IDC_STATIC,7,4,60,8
  EDITTEXT       IDC_NAME,65,4,100,12,ES_AUTOHSCROLL
END

I see you are using Dev-C++ too. I missed that at first. I'm with William. Don't know what the problem is. Maybe try putting the project together again.

I see you are using Dev-C++ too. I missed that at first. I'm with William. Don't know what the problem is. Maybe try putting the project together again.

Yeah I am definitely gonna resort to this every now and then.
I am currently following the entire winAPI programming tutorial from www.winprog.org/tutorial

I am using DEV-C++, and am limited to just only using it alone due to having an aircard, and slow bandwidth it would take to long to download any other compiler. I also feel raw code style programming is probably best for me as well instead of using resource editors that are provided in VStudio.

Thanks allot, and I look forward to you helping me. I may pm you when needed if you dont mind.

I see you are using Dev-C++ too. I missed that at first. I'm with William. Don't know what the problem is. Maybe try putting the project together again.

oh yeah forgot to mention after putting the project together again it worked. For some reason or another even having everything the in the right place it wasnt working. DEV kept creating some default API.o file and it just wouldnt compile and run. Did the project over again.. voila!!!

I also feel raw code style programming is probably best for me as well instead of using resource editors that are provided in VStudio.

Absolutely! Using the Windows Dialog Engine to learn Windows programming is IMNSHO a big mistake.

Recently I posted some beginning Api Sdk style code plus explanations here...

http://www.jose.it-berater.org/smfforum/index.php?topic=3389.0

Program Example 37 starts GUI stuff. Before that is general C/C++ stuff.

You know, with Dev-CPP and all other development suites, you can't just code Win32 Api Sdk code in a blank code window and hit the compile button. You have to go through the various setup screens accessable from the file menu >> Create New Project option. When you choose the option to create a GUI program then the compiler options will be set correctly for you. I'm wondering if that is/was your problem. Anyway, glad its working for you now.

I also feel raw code style programming is probably best for me as well instead of using resource editors that are provided in VStudio.

If you want to learn how to program resource files, you're more than welcome to, but at the end of the day it isn't really necessary. I've only ever looked inside my resource files a couple of times when doing some very specific things, but otherwise it's a good idea to let Visual Studio handle them.

It's all a matter of personal coding preference I guess, feel free to do what you like :icon_lol:

Well the problem here is I am working with minimalization.
Slow connection to the internet because I live way out in the country LOL, so I cant download a large compiler like MSVStudio. So I only use Bloodshed DEV-C++ despite it's limitations and lack of a resource editor that MSVS provides it forces me to code everything by hand, but all in the same I am at a very fundamental level of WINAPI programming. Ive just finally after the years made my mind to get out of console level personal hobby projects to learn winAPI in hopes it helps me financially becoming a programmer. Since your more advanced I thank you for helping me because I really need your experience to accellerate my learning, but this way you know my limitations. I will be very active on this forum until I complete this entire winAPI tutorial and can entirely make my own API with comfort. I'm over whelmed, but focused and driven. So Im gonna get this down :)
I am also going to go back to college and major in programming so I'm self studying to try and get a head start.

I have another post where I am running into a problem with the Dialog portion of the tutorial.. please if you could help me find where I am going wrong. Bloodshed I have noticed can be buggy at times.


If you want to learn how to program resource files, you're more than welcome to, but at the end of the day it isn't really necessary. I've only ever looked inside my resource files a couple of times when doing some very specific things, but otherwise it's a good idea to let Visual Studio handle them.

It's all a matter of personal coding preference I guess, feel free to do what you like :icon_lol:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.