-
respond to button
i'm trying to create a window that writes something different on the click of a button, but the problem is the window doesn't do anything.
Code:
#include <windows.h>
LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
HINSTANCE hInstGlobal;
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
hInstGlobal = hInstance;
WNDCLASSEX WndCls;
static char szAppName[] = "Kazalio Studios";
MSG Msg;
WndCls.cbSize = sizeof(WndCls);
WndCls.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
WndCls.lpfnWndProc = WindProcedure;
WndCls.cbClsExtra = 0;
WndCls.cbWndExtra = 0;
WndCls.hInstance = hInstance;
WndCls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndCls.hCursor = LoadCursor(NULL, IDC_ARROW);
WndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndCls.lpszMenuName = NULL;
WndCls.lpszClassName = szAppName;
WndCls.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
RegisterClassEx(&WndCls);
CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
szAppName, "Dog Simulator 2",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 600, 400,
NULL, NULL, hInstance, NULL);
while( GetMessage(&Msg, NULL, 0, 0) )
{
TranslateMessage(&Msg);
DispatchMessage( &Msg);
}
return static_cast<int>(Msg.wParam);
}
LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg,
WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT Ps;
HFONT font;
switch(Msg)
{
case WM_CREATE:
HWND hContinue, hExit;
hContinue = CreateWindow ( "Button", "Continue",
WS_CHILD | WS_VISIBLE |
BS_PUSHBUTTON,
10, 320, 140, 20,
hWnd, (HMENU) 1,
hInstGlobal, NULL );
hExit = CreateWindow ( "Button", "Exit",
WS_CHILD | WS_VISIBLE |
BS_PUSHBUTTON,
437, 320, 140, 20,
hWnd, (HMENU) 2,
hInstGlobal, NULL);
return 0;
case WM_COMMAND:
if (HIWORD(wParam) == BN_CLICKED)
{
if(LOWORD(wParam) == 1)
{
hDC = BeginPaint(hWnd, &Ps);
font = CreateFont(90,0,0, 0,
FW_NORMAL, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_ROMAN,
"Adolescence");
SelectObject(hDC, font);
TextOut(hDC, 22, 100, "Dog Simulator 3", 15);
DeleteObject(font);
EndPaint(hWnd, &Ps);
}
if(LOWORD(wParam) == 2)
{
SendMessage (GetParent((HWND)lParam),
WM_DESTROY, 0, 0);
}
}
return 0;
case WM_PAINT:
hDC = BeginPaint(hWnd, &Ps);
font = CreateFont(90,0,0, 0,
FW_NORMAL, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_ROMAN,
"Adolescence");
SelectObject(hDC, font);
TextOut(hDC, 22, 0, "Dog Simulator 2", 15);
DeleteObject(font);
EndPaint(hWnd, &Ps);
break;
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return 0;
}
-
-
Ummmmmmmm, not meaning to be picky..........
I hope you got all these errors.....
If you want the variables in the callback to hold their values make them global or static. In C WIN32 I use an array/linked list of structures to hold these values.
This is old..
while(GetMessage(&Msg, NULL, 0, 0))
Should now be
while(GetMessage(&Msg, NULL, 0, 0)>0)// -ve return is now an error
Don't call BeginPaint() except within the WM_PAINT handler, use this to call for a paint
Code:
RECT Rect;
GetClientRect(hWnd,&Rect); //get area to paint
InvalidateRect(hWnd,&Rect,FALSE); //send paint msg, no background erase
UpdateWindow(hWnd); //post directly to app msg que not OS msg que
If not using a .NET version of MSVC (as .NET GDI objects are temp by default)
A GDI object will/may not delete if selected into a DC. The DC may also not delete. These are memory leaks, bad ones.
Test the handles for NULL before creating new objects in any code that may get called more than once.
Try..
Code:
static HFONT hFont=NULL;
//create
if(!hFont)
//create font
//when you select a GDI object into a DC the current GDI of that type is the return value
HFONT hOldFont = SelectObject(hDC, hFont);// catch current font
//use DC
SelectObject(hDC,hOldFont);// return DC to original state
if(!DeleteObject(hFont))// clean up
//error call GetLastError()
else //good
hFont=NULL;//reset handle
Use FillRect() to erase any current text on the DC