-
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
-
-
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.
-
Thanks for the reply.
Is the line where I need to insert "BUTTON"?
Code:
WndClass.lpszClassName = sClassName;
-
Check out this tutorial. http://www.winprog.org/tutorial Should teach you everything you need to know.
-
Will do. Thanks for your help.