JohnCarmack
Programmer
Hi there, I have problem with subclassing, with painting. Try my code, it looks ok, but doesnt work. What I am doing bad?
I need paint some on my buttons.
#include <windows.h>
#include <stdlib.h>
#include <wingdi.h>
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
HWND handleButton[25];
BOOL RedButton[25];
WNDPROC PrevWindow[25] ;
char textt[255];
/* Declare Windows procedure */
LRESULT CALLBACK ButtonProcedure(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
void SetColor(HDC dc, int ID, bool RED)
{
HBRUSH NewBrush;
COLORREF CRED = RGB(255,0,0);
COLORREF CGREEN = RGB(0,255,0);
COLORREF DEF;
if (RED==TRUE)
DEF = CRED;
else
DEF = CGREEN;
itoa(ID+1,textt,10);
NewBrush = CreateSolidBrush(DEF);
SelectObject(dc, NewBrush);
Rectangle(dc,1,1,23,23);
DeleteObject(NewBrush);
SetBkMode(dc, TRANSPARENT);
TextOut(dc,8/strlen(textt),4,textt,strlen(textt));
};
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_PARENTDC; /* Catch double-clicks */
wincl.cbSize = sizeof(WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use light-gray as the background of the window */
wincl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
/* Register the window class, if fail quit the program */
if(!RegisterClassEx(&wincl)) return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx(
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Game", /* Title Text */
WS_OVERLAPPEDWINDOW/*|WS_CLIPCHILDREN| WS_CLIPSIBLINGS*/, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
300, /* The programs width */
315, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow(hwnd, nFunsterStil);
UpdateWindow(hwnd);
/* Run the message loop. It will run until GetMessage( ) returns 0 */
while(GetMessage(&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage( ) gave */
return messages.wParam;
}
LRESULT CALLBACK ButtonProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC dc;
int id = GetDlgCtrlID(hwnd);
switch (message)
{
case WM_LBUTTONDOWN:
RedButton[id]=~RedButton[id];
dc = GetDC(handleButton[id]);
SetColor(dc,id,RedButton[id]);
ReleaseDC(handleButton[id],dc);
break;
case WM_LBUTTONUP:
InvalidateRect(hwnd,NULL,TRUE);
break;
case WM_PAINT:
dc = GetDC(hwnd);
SetColor(dc,id,RedButton[id]);
ReleaseDC(hwnd,dc);
break;
}
return CallWindowProc(PrevWindow[id], hwnd, message, wParam, lParam);
}
/* This function is called by the Windows function DispatchMessage( ) */
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
for (int a=0; a<25; a++)
{
itoa(a+1,textt,10);
handleButton[a] = CreateWindow("button", textt, WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON , (a%5)*54+20, (a/5)*54+20, 25, 25, hwnd, (HMENU) a, (HINSTANCE) GetWindowLong (hwnd,GWL_HINSTANCE), NULL);
PrevWindow[a] = (WNDPROC) SetWindowLong(handleButton[a], GWL_WNDPROC,(LONG) ButtonProcedure) ;
}
break;
case WM_DESTROY:
PostQuitMessage(0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
I need paint some on my buttons.
#include <windows.h>
#include <stdlib.h>
#include <wingdi.h>
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
HWND handleButton[25];
BOOL RedButton[25];
WNDPROC PrevWindow[25] ;
char textt[255];
/* Declare Windows procedure */
LRESULT CALLBACK ButtonProcedure(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
void SetColor(HDC dc, int ID, bool RED)
{
HBRUSH NewBrush;
COLORREF CRED = RGB(255,0,0);
COLORREF CGREEN = RGB(0,255,0);
COLORREF DEF;
if (RED==TRUE)
DEF = CRED;
else
DEF = CGREEN;
itoa(ID+1,textt,10);
NewBrush = CreateSolidBrush(DEF);
SelectObject(dc, NewBrush);
Rectangle(dc,1,1,23,23);
DeleteObject(NewBrush);
SetBkMode(dc, TRANSPARENT);
TextOut(dc,8/strlen(textt),4,textt,strlen(textt));
};
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_PARENTDC; /* Catch double-clicks */
wincl.cbSize = sizeof(WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use light-gray as the background of the window */
wincl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
/* Register the window class, if fail quit the program */
if(!RegisterClassEx(&wincl)) return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx(
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Game", /* Title Text */
WS_OVERLAPPEDWINDOW/*|WS_CLIPCHILDREN| WS_CLIPSIBLINGS*/, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
300, /* The programs width */
315, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow(hwnd, nFunsterStil);
UpdateWindow(hwnd);
/* Run the message loop. It will run until GetMessage( ) returns 0 */
while(GetMessage(&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage( ) gave */
return messages.wParam;
}
LRESULT CALLBACK ButtonProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC dc;
int id = GetDlgCtrlID(hwnd);
switch (message)
{
case WM_LBUTTONDOWN:
RedButton[id]=~RedButton[id];
dc = GetDC(handleButton[id]);
SetColor(dc,id,RedButton[id]);
ReleaseDC(handleButton[id],dc);
break;
case WM_LBUTTONUP:
InvalidateRect(hwnd,NULL,TRUE);
break;
case WM_PAINT:
dc = GetDC(hwnd);
SetColor(dc,id,RedButton[id]);
ReleaseDC(hwnd,dc);
break;
}
return CallWindowProc(PrevWindow[id], hwnd, message, wParam, lParam);
}
/* This function is called by the Windows function DispatchMessage( ) */
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
for (int a=0; a<25; a++)
{
itoa(a+1,textt,10);
handleButton[a] = CreateWindow("button", textt, WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON , (a%5)*54+20, (a/5)*54+20, 25, 25, hwnd, (HMENU) a, (HINSTANCE) GetWindowLong (hwnd,GWL_HINSTANCE), NULL);
PrevWindow[a] = (WNDPROC) SetWindowLong(handleButton[a], GWL_WNDPROC,(LONG) ButtonProcedure) ;
}
break;
case WM_DESTROY:
PostQuitMessage(0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}