Mythic Fr0st
February 22nd, 2007, 06:23 PM
My code is now large to me (200 words or so), im used to having like 4k in PHP or other languages, but I can't read WINAPI very well yet, but anyway, I have a few questions...
~ Currently what I have (PS im trying to make a calculator)
Q's
Question 1: how do I print text on a window, I know how to do MessageBox(NULL, "HI", "Title", MB_ICONEXCLAMATION | MB_OK);
Question 2: how do I make a text box? for the user to enter a value into
Question 3: How do I make a MessageBox or other text, come up when you click a button? (Would you have some sort of thing where you can call a function? and that function happens?)
Question 4: How do I Change the colour of a Button, and message box?
Question 5: How do I MessageBox an integer? or char (or just "not" a string)IE
MessageBox(NULL, 25+452, "Sum" MB_ICONEXCLAMATION | MB_OK)
Question 6: How do I add a hotkey to a button? E.G you press 0, it clicks a button for you
my current code:
#include <windows.h>
#define N1 100
#define N2 100
#define N3 100
#define N4 100
#define N5 100
#define N6 100
#define N7 100
#define N8 100
#define N9 100
#define N0 100
#define NP 100
#define NM 100
#define ND 100
#define NPOW 100
#define NS 100
const char g_szClassName[] = "Calculator";
int Quit_Msg;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
Quit_Msg = MessageBox(hwnd, "Are you sure you really want to quit", "Quit", MB_YESNO);
if (Quit_Msg == IDYES)
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 0
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N0:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 1
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N1:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 2
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N2:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 3
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N3:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 4
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N4:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 5
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N5:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 6
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N6:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 7
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N7:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 8
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N8:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 9
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N9:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number +
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case NP:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number -
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case NS:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number /
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ND:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number *
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case NM:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number ^
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case NPOW:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//End Callback
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd, NM0, NM1, NM2, NM3, NM4, NM5, NM6, NM7, NM8, NM9, NMP, NMS, NMD, NMM, NMPOW;
MSG Msg;
//register window class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_DBLCLKS;
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) GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Reg has failed", "Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx
(
0, g_szClassName, "Calculator", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 250, 250, HWND_DESKTOP, NULL, hInstance, NULL
);
NM0 = CreateWindowEx
(
0, "Button", "0", WS_CHILD | WS_VISIBLE,
5, 5, 15, 15, hwnd, (HMENU)N0, hInstance, NULL
);
NM1 = CreateWindowEx
(
0, "Button", "1", WS_CHILD | WS_VISIBLE,
19, 5, 15, 15, hwnd, (HMENU)N1, hInstance, NULL
);
NM2 = CreateWindowEx
(
0, "Button", "2", WS_CHILD | WS_VISIBLE,
34, 5, 15, 15, hwnd, (HMENU)N2, hInstance, NULL
);
NM3 = CreateWindowEx
(
0, "Button", "3", WS_CHILD | WS_VISIBLE,
5, 19, 15, 15, hwnd, (HMENU)N3, hInstance, NULL
);
NM4 = CreateWindowEx
(
0, "Button", "4", WS_CHILD | WS_VISIBLE,
19, 19, 15, 15, hwnd, (HMENU)N4, hInstance, NULL
);
NM5 = CreateWindowEx
(
0, "Button", "5", WS_CHILD | WS_VISIBLE,
34, 19, 15, 15, hwnd, (HMENU)N5, hInstance, NULL
);
NM6 = CreateWindowEx
(
0, "Button", "6", WS_CHILD | WS_VISIBLE,
5, 34, 15, 15, hwnd, (HMENU)N6, hInstance, NULL
);
NM7 = CreateWindowEx
(
0, "Button", "7", WS_CHILD | WS_VISIBLE,
19, 34, 15, 15, hwnd, (HMENU)N7, hInstance, NULL
);
NM8 = CreateWindowEx
(
0, "Button", "8", WS_CHILD | WS_VISIBLE,
34, 34, 15, 15, hwnd, (HMENU)N8, hInstance, NULL
);
NM9 = CreateWindowEx
(
0, "Button", "9", WS_CHILD | WS_VISIBLE,
5, 49, 15, 15, hwnd, (HMENU)N9, hInstance, NULL
);
NMP = CreateWindowEx
(
0, "Button", "+", WS_CHILD | WS_VISIBLE,
19, 49, 15, 15, hwnd, (HMENU)NP, hInstance, NULL
);
NMS = CreateWindowEx
(
0, "Button", "-", WS_CHILD | WS_VISIBLE,
34, 49, 15, 15, hwnd, (HMENU)NS, hInstance, NULL
);
NMM = CreateWindowEx
(
0, "Button", "*", WS_CHILD | WS_VISIBLE,
5, 64, 15, 15, hwnd, (HMENU)NM, hInstance, NULL
);
NMD = CreateWindowEx
(
0, "Button", "/", WS_CHILD | WS_VISIBLE,
19, 64, 15, 15, hwnd, (HMENU)ND, hInstance, NULL
);
NMPOW = CreateWindowEx
(
0, "Button", "^", WS_CHILD | WS_VISIBLE,
34, 64, 15, 15, hwnd, (HMENU)NMPOW, hInstance, NULL
);
if (hwnd == NULL)
{
MessageBox(NULL, "Window Didnt Create", "Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
//msg loop
while (GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
~ Currently what I have (PS im trying to make a calculator)
Q's
Question 1: how do I print text on a window, I know how to do MessageBox(NULL, "HI", "Title", MB_ICONEXCLAMATION | MB_OK);
Question 2: how do I make a text box? for the user to enter a value into
Question 3: How do I make a MessageBox or other text, come up when you click a button? (Would you have some sort of thing where you can call a function? and that function happens?)
Question 4: How do I Change the colour of a Button, and message box?
Question 5: How do I MessageBox an integer? or char (or just "not" a string)IE
MessageBox(NULL, 25+452, "Sum" MB_ICONEXCLAMATION | MB_OK)
Question 6: How do I add a hotkey to a button? E.G you press 0, it clicks a button for you
my current code:
#include <windows.h>
#define N1 100
#define N2 100
#define N3 100
#define N4 100
#define N5 100
#define N6 100
#define N7 100
#define N8 100
#define N9 100
#define N0 100
#define NP 100
#define NM 100
#define ND 100
#define NPOW 100
#define NS 100
const char g_szClassName[] = "Calculator";
int Quit_Msg;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
Quit_Msg = MessageBox(hwnd, "Are you sure you really want to quit", "Quit", MB_YESNO);
if (Quit_Msg == IDYES)
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 0
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N0:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 1
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N1:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 2
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N2:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 3
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N3:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 4
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N4:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 5
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N5:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 6
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N6:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 7
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N7:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 8
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N8:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number 9
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case N9:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number +
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case NP:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number -
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case NS:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number /
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ND:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number *
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case NM:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//Number ^
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case NPOW:
{
PostQuitMessage(0);
}
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//End Callback
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd, NM0, NM1, NM2, NM3, NM4, NM5, NM6, NM7, NM8, NM9, NMP, NMS, NMD, NMM, NMPOW;
MSG Msg;
//register window class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_DBLCLKS;
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) GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Reg has failed", "Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx
(
0, g_szClassName, "Calculator", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 250, 250, HWND_DESKTOP, NULL, hInstance, NULL
);
NM0 = CreateWindowEx
(
0, "Button", "0", WS_CHILD | WS_VISIBLE,
5, 5, 15, 15, hwnd, (HMENU)N0, hInstance, NULL
);
NM1 = CreateWindowEx
(
0, "Button", "1", WS_CHILD | WS_VISIBLE,
19, 5, 15, 15, hwnd, (HMENU)N1, hInstance, NULL
);
NM2 = CreateWindowEx
(
0, "Button", "2", WS_CHILD | WS_VISIBLE,
34, 5, 15, 15, hwnd, (HMENU)N2, hInstance, NULL
);
NM3 = CreateWindowEx
(
0, "Button", "3", WS_CHILD | WS_VISIBLE,
5, 19, 15, 15, hwnd, (HMENU)N3, hInstance, NULL
);
NM4 = CreateWindowEx
(
0, "Button", "4", WS_CHILD | WS_VISIBLE,
19, 19, 15, 15, hwnd, (HMENU)N4, hInstance, NULL
);
NM5 = CreateWindowEx
(
0, "Button", "5", WS_CHILD | WS_VISIBLE,
34, 19, 15, 15, hwnd, (HMENU)N5, hInstance, NULL
);
NM6 = CreateWindowEx
(
0, "Button", "6", WS_CHILD | WS_VISIBLE,
5, 34, 15, 15, hwnd, (HMENU)N6, hInstance, NULL
);
NM7 = CreateWindowEx
(
0, "Button", "7", WS_CHILD | WS_VISIBLE,
19, 34, 15, 15, hwnd, (HMENU)N7, hInstance, NULL
);
NM8 = CreateWindowEx
(
0, "Button", "8", WS_CHILD | WS_VISIBLE,
34, 34, 15, 15, hwnd, (HMENU)N8, hInstance, NULL
);
NM9 = CreateWindowEx
(
0, "Button", "9", WS_CHILD | WS_VISIBLE,
5, 49, 15, 15, hwnd, (HMENU)N9, hInstance, NULL
);
NMP = CreateWindowEx
(
0, "Button", "+", WS_CHILD | WS_VISIBLE,
19, 49, 15, 15, hwnd, (HMENU)NP, hInstance, NULL
);
NMS = CreateWindowEx
(
0, "Button", "-", WS_CHILD | WS_VISIBLE,
34, 49, 15, 15, hwnd, (HMENU)NS, hInstance, NULL
);
NMM = CreateWindowEx
(
0, "Button", "*", WS_CHILD | WS_VISIBLE,
5, 64, 15, 15, hwnd, (HMENU)NM, hInstance, NULL
);
NMD = CreateWindowEx
(
0, "Button", "/", WS_CHILD | WS_VISIBLE,
19, 64, 15, 15, hwnd, (HMENU)ND, hInstance, NULL
);
NMPOW = CreateWindowEx
(
0, "Button", "^", WS_CHILD | WS_VISIBLE,
34, 64, 15, 15, hwnd, (HMENU)NMPOW, hInstance, NULL
);
if (hwnd == NULL)
{
MessageBox(NULL, "Window Didnt Create", "Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
//msg loop
while (GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}