Hi,
How to change the Background of window creted by us
can I use RBG to have my own color combination.How could I
Regards
Printable View
Hi,
How to change the Background of window creted by us
can I use RBG to have my own color combination.How could I
Regards
You can use this when you initially register your window class.
CreateSolidBrush(RGB(255,255,255))
or
SetClassLong(hwnd,GCL_HBRBACKGROUND,CreateSolidBru sh(RGB(255,255,255)))
after the window has been registered
Thnx.
Just remember that brushes that aren't stocked need to be deleted with DeleteObject. :)
Good advice in most circumstance, however, not always applicable...per MSQuote:
Originally Posted by Joelito
Quote:
The system automatically deletes class background brushes when the class is unregistered by using UnregisterClass. An application should not delete these brushes.
UnregisterClass!
Good point ;)
since i use theCretesolidbrush while difining the WNDCLASS
do i need to delete brush using DeleteObject()
If you call unregisterclass at the end of your program, which is good practice, then no, you should not delete it. It will get deleted by unregisterclass.
However, and I can't find a clear answer on this, if you don't call Unregisterclass, I am not sure if you need to delete the background brush because even when you don't call unregisterclass, windows still unregisters your class when the program ends.
use GetStockObject() and not worry about it......
ie
GetStockObject(WHITE_BRUSH)
i dont mean to intrude but now that we have the brush, how do you change the background color and text?
and then, how do you change it to something else, say change it back to the original colors?
What if we want a nice green felt color as our background for our blackjack game. WHITE_BRUSH is not going to cut it. None of the 16 stock brushes will.Quote:
Originally Posted by novacain
I modified the example from winprog.net to demonstrate changing background colors. As far as text goes, you can use textout() for simple text outputs. But depending on your needs it can get pretty involed as there are a lot of sizing issues that must be considered. Especially if you are doing multiple lines and differnt fonts per line.Quote:
Originally Posted by willc0de4food
Code:#include <windows.h>
const char g_szClassName[] = "myWindowClass";
HBRUSH oldBrush = 0;
HBRUSH newBrush = 0;
bool isChanged = false;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_LBUTTONDOWN:
{
if (!isChanged)
{
newBrush = CreateSolidBrush(RGB(0,255,0)); // green
oldBrush = (HBRUSH)SetClassLong(hwnd,GCL_HBRBACKGROUND, (LONG)newBrush);
InvalidateRect(hwnd,0,TRUE);
isChanged = true;
}
}
break;
case WM_RBUTTONDOWN:
{
if (isChanged)
{
SetClassLong(hwnd,GCL_HBRBACKGROUND,(LONG)oldBrush);
DeleteObject (newBrush);
InvalidateRect(hwnd,0,TRUE);
isChanged = false;
}
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
if (isChanged)
{
SetClassLong(hwnd,GCL_HBRBACKGROUND,(LONG)oldBrush);
DeleteObject (newBrush);
isChanged = false;
}
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
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)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}