Thread: Using Resource Files

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    5

    Buttons in C++

    I have a window with some basic menus. I am looking to add buttons to it. Would someone kindly be able to give some simple steps or point me in the direction of a straight forward tutorial?

    In case your interested:

    Here's the code that is used for the window:

    Header File:
    Code:
    #define ID_FILE_NEW           100
    #define ID_FILE_OPEN          101
    #define ID_FILE_SAVE          102
    #define ID_FILE_EXIT          103
    #define ID_DO_SOMETHING       104
    #define ID_DO_SOMETHING_ELSE  105
    #define ID_HELP_ABOUT         106
    Resource File:
    Code:
    #include "tutorial.h"
    
    ID_MENU MENU DISCARDABLE 
    BEGIN
        POPUP "&File"
        BEGIN
            MENUITEM "&New",                        ID_FILE_NEW
            MENUITEM "&Open",                       ID_FILE_OPEN
            MENUITEM "&Save",                       ID_FILE_SAVE
            MENUITEM SEPARATOR
            MENUITEM "E&xit",                       ID_FILE_EXIT
        END
        POPUP "&View"
        BEGIN
            MENUITEM "&Instructions for use",       ID_DO_SOMETHING
            MENUITEM "Sample Video",                ID_DO_SOMETHING_ELSE
        END
        POPUP "&Help"
        BEGIN
            MENUITEM "&About GST",                  ID_HELP_ABOUT
        END
    END
    Code:
    #include <windows.h>
    #include "tutorial.h"
    #include <fstream.h>
    #include <dos.h>
    //#include <graphics.h>
    #include <conio.h>
    #include <process.h>
    #include <stdio.h>
    #include <string.h>
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    static char sClassName[]  = "MyClass";
    static HINSTANCE zhInstance = NULL;
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int 
    
    nCmdShow) 
    {
           WNDCLASSEX WndClass;
           HWND hwnd;
           MSG Msg;
    
           zhInstance = hInstance;
    
           WndClass.cbSize        = sizeof(WNDCLASSEX);
           WndClass.style         = NULL;
           WndClass.lpfnWndProc   = WndProc;
           WndClass.cbClsExtra    = 0;
           WndClass.cbWndExtra    = 0;
           WndClass.hInstance     = zhInstance;
           WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
           WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
           WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
           WndClass.lpszMenuName  = "ID_MENU";
           WndClass.lpszClassName = sClassName;
           WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
           if(!RegisterClassEx(&WndClass)) {
                   MessageBox(0, "Error Registering Class!", "Error!", MB_ICONSTOP | MB_OK);
    	
    		//MessageBox (NULL, "Semaphore System Running!" , "Graphical Semaphore 
    
    Tutor (GST)", 0);
           return 0;
    }
    
    hwnd = CreateWindowEx(WS_EX_STATICEDGE, 
    					  sClassName, 
    					  "Graphical Semaphore Tutor (GST)", 
    					  WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
    						CW_USEDEFAULT,
    						750, 750, NULL, NULL, 
    						zhInstance, NULL);
    
           if(hwnd == NULL) {
                   MessageBox(0, "Error Creating Window!", "Error!", MB_ICONSTOP | MB_OK);
                   return 0;
           }
    
           ShowWindow(hwnd, nCmdShow);
           UpdateWindow(hwnd);
    
           while(GetMessage(&Msg, NULL, 0, 0)) {
                   TranslateMessage(&Msg);
                   DispatchMessage(&Msg);
           }
    
           return Msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
           switch(Message) {
                case WM_CLOSE:
                           DestroyWindow(hwnd);
                           break;
                case WM_DESTROY:
                           PostQuitMessage(0);
                           break;
    
    			case WM_COMMAND:
                           switch(LOWORD(wParam)) {
                                   case ID_FILE_NEW:
                                           MessageBox(hwnd, "You Clicked New File", "New File", 
    
    0);
    										//ofstream 
    
    myproj;
    										//myproj.
    
    open("Semaphore1.exe");	
                                           break;
                                   case ID_FILE_OPEN:
                                           MessageBox(hwnd, "Selecting File to Open", "File 
    
    Open", 0);
    
    									   break;
                                   case ID_FILE_SAVE:
                                           MessageBox(hwnd, "Saving", "File Save", 0);
                                           break;
                                   case ID_FILE_EXIT:
                                           PostQuitMessage(0);
                                   case ID_DO_SOMETHING:
                                           MessageBox(hwnd, "1. Select line of code from left 
    
    pane\n2. Click button to insert into right pane\n3. System checks if you are correct", "
    
    Instructions", 0);
                                           break;
                                   case ID_DO_SOMETHING_ELSE:
                                           MessageBox(hwnd, "Looking for sample video", "View 
    
    Example", 0);
                                   		   break;
                                   case ID_HELP_ABOUT:
                                           MessageBox(hwnd, "Graphical Semaphore Tutor (GST) 1
    
    .0 (C) 2005 PH", "About", 0);
    									   break;
                }
                break;
    
                default:
                           return DefWindowProc(hwnd, Message, wParam, lParam);
           }
    
           return 0;
    }
    Thanks in advance,

    Peter

    PS- Quick Question:

    Is there a limit to how many of these you can use in a program?
    I got this message when I tried using two resources. Might have been another and may not be linked but I'm sure.

    http://img222.echo.cx/img222/8393/message3bs.jpg
    Last edited by heylin; 05-01-2005 at 02:56 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Moved to windows board.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Like you normally create windows, but instead of registering a window class name you use the text "BUTTON". I believe you have to state WS_CHILD as a flag too, and set the parent argument to the window the button should be in.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    5
    Thanks for the reply.

    Is the line where I need to insert "BUTTON"?

    Code:
    WndClass.lpszClassName = sClassName;

  5. #5
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Check out this tutorial. http://www.winprog.org/tutorial Should teach you everything you need to know.

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    5
    Will do. Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  2. Resource Files....
    By gcn_zelda in forum Windows Programming
    Replies: 2
    Last Post: 12-13-2003, 03:02 PM
  3. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  4. Multiple Resource files
    By doodlak in forum C++ Programming
    Replies: 0
    Last Post: 07-04-2002, 06:13 PM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM