Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Window Help

Status
Not open for further replies.

amalthea

Programmer
May 30, 2002
7
US
Hello,
I was wondering if someone could help me with creating a standard window with a menu for a program I am writing. Thanks,
Amalthea
 
#include <windows.h>

// f prototype
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

--------- END OF m_ain.h START OF m_ain.cpp ----------

#include &quot;m_ain.h&quot;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
HWND hWnd;
MSG Msg;
WNDCLASSEX wcex = { sizeof(WNDCLASSEX), CS_CLASSDC, WindowProc, 0L, 0L, hInstance, NULL, NULL, NULL, NULL, &quot;GameClass&quot;, NULL };
// reg class exit on err
if(!RegisterClassEx(&wcex))
return FALSE;
hWnd = CreateWindow(&quot;GameClass&quot;, &quot;the title&quot;, WS_OVERLAPPEDWINDOW, 0, 0, 400, 400, NULL, NULL, hInstance, NULL);
// return if err on CreateWindow
if(hWnd == NULL)
return FALSE;
// show the window
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
// Clear out message structure
ZeroMemory(&Msg, sizeof(MSG));
// loop until msg = exit
while(Msg.message != WM_QUIT) {
// peek into queue to see if there's message waiting
if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
} else {
// time-crusial stuff
}
}
// unreg class
UnregisterClass(&quot;GameClass&quot;, hInstance);
// exit
return 0;
}
// MSG procedure
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg) {
case WM_DESTROY:
PostQuitMessage(0); // close app
break;
// handle other msg
default: return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
} ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
u could also use vc++6 template ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Thanks so much for your reply... If I wish to use this window class or the visual C++ template, how do I then interface it with whatever code I have written?
 
Depends on what the code does and how you want to inteface with it. Usually, programs respond to user events so you need to extend the message handling switch statement to handle different events. Menu item clicks for example get handled by WM_COMMAND message:

switch(uMsg) {
case WM_DESTROY:
PostQuitMessage(0); // close app
break;

case WM_COMMAND:
switch (LOWORD(wParam))
{
case CM_SOME_MENUITEM_YOU_DEFINE:
YourFunctionCall();
break;
case CM_YOUR_MENU_EXIT_OPTION:
DestroyWindow(hWnd);
break;
}
break;

default: return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}

In this example you would first need to create a menu, which means you need to create a resource in VC++ which you add to your project. The menu needs to have items which have ids corresponding to the values above. i.e. CM_SOME_MENUITEM_YOU_DEFINE is the id of one of the items in your menu.

Alternately if you want a GUI of some kind (say with buttons) then you need to create it, either as a dialog resource you embed into the client area of the main window or manually by creating each button with CreateWindow(). Then, messages from each button are handled, again within the WM_COMMAND message, using the high word of the wParam to get the button-clicked notification and the low word to get the id of the button:

case WM_COMMAND:
switch(HIWORD(wParam))
{
case BN_CLICKED:
switch(LOWORD(wParam))
{
case IDC_SOMEBTN:
SomeFunction();
break;

case IDC_SOMEOTHERBTN:
SomeOtherFunction();
break;
}
break;
}
break;

This is all a pretty manual approach, which doesn't bother me, but a lot of people use MFC which wraps a lot of this kind of stuff into more manageable C++ classes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top