Dev C++ tutorial.

Programming, for all ages and all languages.
Post Reply
Oliver

Dev C++ tutorial.

Post by Oliver »

Where can I find a tutorial for Dev C++????
Tim

Re:Dev C++ tutorial.

Post by Tim »

Here :). I'll write one.

Part 1: make a menu resource. I assume that, since you're using DevC++, you're writing resource files in text format. If you have a graphical editor, use that and skip this part.

In your resource.h file:

Code: Select all

#define IDR_MENU                        101
#define ID_FILE_NEW                     40001
#define ID_FILE_OPEN                    40002
#define ID_FILE_SAVE                    40003
#define ID_FILE_SAVEAS                  40004
#define ID_FILE_EXIT                    40005
#define ID_HELP_ABOUT                   40006
In your .RC file:

Code: Select all

#include "resource.h"

IDR_MENU MENU DISCARDABLE 
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "&New\tCtrl+N",                ID_FILE_NEW
        MENUITEM "&Open...\tCtrl+O",            ID_FILE_OPEN
        MENUITEM "&Save\tCtrl+S",               ID_FILE_SAVE
        MENUITEM "Save &As...",                 ID_FILE_SAVEAS
        MENUITEM SEPARATOR
        MENUITEM "E&xit",                       ID_FILE_EXIT
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&About",                      ID_HELP_ABOUT
    END
END
This code should now compile with your resource compiler, which, I believe, is called windres. Alternatively you can use the rc program included in the Platform SDK.

Part 2: load the menu in your program. #include "resource.h" in your source file.

Three possible ways to use the menu:
1) Set WNDCLASS.lpszMenuName = MAKEINTRESOURCE(IDR_MENU) before you call RegisterClass.
2) Call LoadMenu and pass a menu handle to CreateWindow.
3) Call LoadMenu and SetMenu after the window has been created (e.g. in WM_CREATE).

I'd say (1) was the easiest way. With (2) and (3), don't forget to call DestroyMenu on the menu handle after your program exits.

Part 3: handling menu commands. Menu commands get passed to the window with the WM_COMMAND message. LOWORD(wParam) gives you the command ID; this will be one of the ID_FILE_NEW, ID_FILE_OPEN etc. constants. If you create a toolbar as well, you can reuse the same IDs and you won't need to handle the menu and toolbar separately.

Extra tips:
-- Don't forget to set WNDCLASS.hInstance if you're using option (1) above
-- To display a line in the status bar for each command, handle WM_MENUSELECT. Define a string in your string table for each command ID (re-using the same IDs). In your WM_MENUSELECT handler, load the appropriate string using LoadString and display it on your status bar.
-- Menu bar IDs (and resource IDs in general) can be either strings or numbers. Here I've used a number, because resource.h has a #define line for IDR_MENU. The resource compiler sees this as a number. If I didn't put in a #define for IDR_MENU then the resource compiler would see the name as a string. There's an important difference here: for numerical IDs (including names defined in resource.h) you have to use MAKEINTRESOURCE; for string IDs you use a normal quoted string name. In the example above, using "IDR_MENU" wouldn't work, but MAKEINTRESOURCE(IDR_MENU) does.

Hope I've answered any questions; if I've misunderstood your question then I've just written a load of stuff for no reason :).
Oliver

Re:Dev C++ tutorial.

Post by Oliver »

yah you answered it , so now I have the menubar code, now I need the whole tutorial of Dev C++
Post Reply