// JP opened flex table

Click to See Complete Forum and Search --> : Questions and a question about a hotkey for button


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;

}

Ali Imran
February 22nd, 2007, 07:41 PM
PHP is much more easier, it is true winapi code is not that easy to read especially when you are a beginner.

That many questions :eek: hooooof, lol never mind. Let's go through one by one

Question 1: how do I print text on a window, I know how to do MessageBox(NULL, "HI", "Title", MB_ICONEXCLAMATION | MB_OK);

Printing a text is different than setting a value to a control or a window. You want to display directly on a window/Dialog's surface ?

to set text:
SetWindowText(hwnd, "My text");

to print text:
HDC dc = GetDC(hwnd);
char *myTextVar = "My Text";
TextOut(dc,x_pos,y_pos,myTextVar,strlen(myTextVar));
ReleaseDC(hwnd,dc);


Question 2: how do I make a text box? for the user to enter a value into

You are using function CreateWindowEx already and passing classname "Button" as second argument of this function, simply pass "EDIT" instead and you have a textbox.


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?)

You mean track down button click ? Sorry, question a bit confusing.


Question 4: How do I Change the colour of a Button, and message box?
in your window procedure:

switch(msg) {
case WM_CTLCOLORBTN:
return (long)CreateSolidBrush(RGB(0,0,0xFF));
//RGB(0,0,0xFF) is color
.......
}


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)


char text[1024];
wsprintf(text,"%d", 25+452);
MessageBox(NULL, text, "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

Please read help for RegisterHotKey function HERE (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/registerhotkey.asp)


I hope it helps.

regards

Mythic Fr0st
February 22nd, 2007, 11:41 PM
Thanks, it helped alot however, could you perhaps give some examples...

such as

Adding ALL my cases into 1 switch statement so instead of

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);
}

that is 1 switch statement but I have 20 more, the only thing changing in each one is case N(NUMBER)

E.G case N1, N2, N3, N4, N5, how can I fit
more than just 1 case into that statement (can you give example of 2 or 3)

if you dont understand just tell me, ill rephrase

Also, How do I change the Icon that appears over my "buttons" (and text box),

and how do I declare a global string variable and store the value clicked from the button

E.G, (this isnt right but maybe give you an idea of what I need)

If clicked Button equal to "NM0" then
Set String = String + 0

if clicked button equal to NM1 then
Set String = String + 1

also... can you give an example of how to colour things, with one of my switch statements...

can you also give me an example of that hotkey thing, I couldn't understand very well the function thing, but i'll bookmark that site for later reference

~ Thanks

//JP added flex table