Hi, I have an array of hWnds, and I need to change each one of their background colors. They're all without a border(titlebar, and such). I've searched Google, and IxQuick, and I need an answer please. Thanks so much for the help.
Voidz 0 Newbie Poster
Frederick2 189 Posting Whiz
That's not terribly hard. All you need to do is create a BRUSH of the color you want and select it into the device context in your WM_PAINT handler. I don't think I have a C/C++ example handy, but here is a PowerBASIC example of a WM_PAINT handler where I paint the whole window bright red, and draw "Hello, World!" in massive white text on it...
Function OnPaint(wea As WndEventArgs) As Long
Local hDC,hBrush,hTmp,hFont As Dword
Local lpPaint As PAINTSTRUCT
Local strText As String
Local rc As RECT
hDC = BeginPaint(wea.hWnd, lpPaint)
hBrush=CreateSolidBrush(&h000000FF)
hTmp=SelectObject(hDC,hBrush)
Call GetClientRect(wea.hWnd,rc)
Call FillRect(hDC,rc,hBrush)
Call DeleteObject(SelectObject(hdc,hTmp))
Call SetTextColor(hDC,&H00FFFFFF)
hFont=CreateFont(40,0,0,0,%FW_BOLD,0,0,0,0,0,0,2,0,"SYSTEM_FIXED_FONT")
hTmp=SelectObject(hDC,hFont)
Call SetBkMode(hDC,%TRANSPARENT)
strText = "Hello, World!"
Call TextOut(hDC,52,90,ByVal StrPtr(strText),Len(strText))
Call DeleteObject(SelectObject(hdc,hTmp))
Call EndPaint(wea.hWnd,lpPaint)
OnPaint=0
End Function
Voidz 0 Newbie Poster
That's not terribly hard. All you need to do is create a BRUSH of the color you want and select it into the device context in your WM_PAINT handler. I don't think I have a C/C++ example handy, but here is a PowerBASIC example of a WM_PAINT handler where I paint the whole window bright red, and draw "Hello, World!" in massive white text on it...
Function OnPaint(wea As WndEventArgs) As Long Local hDC,hBrush,hTmp,hFont As Dword Local lpPaint As PAINTSTRUCT Local strText As String Local rc As RECT hDC = BeginPaint(wea.hWnd, lpPaint) hBrush=CreateSolidBrush(&h000000FF) hTmp=SelectObject(hDC,hBrush) Call GetClientRect(wea.hWnd,rc) Call FillRect(hDC,rc,hBrush) Call DeleteObject(SelectObject(hdc,hTmp)) Call SetTextColor(hDC,&H00FFFFFF) hFont=CreateFont(40,0,0,0,%FW_BOLD,0,0,0,0,0,0,2,0,"SYSTEM_FIXED_FONT") hTmp=SelectObject(hDC,hFont) Call SetBkMode(hDC,%TRANSPARENT) strText = "Hello, World!" Call TextOut(hDC,52,90,ByVal StrPtr(strText),Len(strText)) Call DeleteObject(SelectObject(hdc,hTmp)) Call EndPaint(wea.hWnd,lpPaint) OnPaint=0 End Function
Thanks, this will help.
Frederick2 189 Posting Whiz
Just for the heck of it, I translated my PowerBASIC code to C++. Here's the whole program (which contains the fnWndProc_OnPaint() handler which paints the screen red and prints "Hello, World!" in a gigantic font). Its a compilable program...
//WinTypes.h
#ifndef WINTYPES_H
#define WINTYPES_H
#define dim(x) (sizeof(x) / sizeof(x[0]))
typedef struct WindowsEventArguments
{
HWND hWnd;
WPARAM wParam;
LPARAM lParam;
HINSTANCE hIns;
}WndEventArgs, *lpWndEventArgs;
long fnWndProc_OnPaint (lpWndEventArgs Wea);
long fnWndProc_OnClose (lpWndEventArgs Wea);
struct EVENTHANDLER
{
unsigned int Code;
long (*fnPtr)(lpWndEventArgs);
};
const EVENTHANDLER EventHandler[]=
{
{WM_PAINT, fnWndProc_OnPaint},
{WM_CLOSE, fnWndProc_OnClose}
};
#endif
//Main.cpp
#define UNICODE
#define _UNICODE
#include <windows.h>
#include <tchar.h>
#include "WinTypes.h"
long fnWndProc_OnPaint(lpWndEventArgs Wea)
{
TCHAR szBuffer[]=_T("Hello, World!");
HFONT hFont,hTmpFnt;
HBRUSH hBrush,hTmpBr;
PAINTSTRUCT ps;
RECT rc;
HDC hDC;
hDC=BeginPaint(Wea->hWnd,&ps);
hBrush=CreateSolidBrush(0x000000FF);
hTmpBr=(HBRUSH)SelectObject(hDC,hBrush);
GetClientRect(Wea->hWnd,&rc);
FillRect(hDC,&rc,hBrush);
DeleteObject(SelectObject(hDC,hTmpBr));
SetTextColor(hDC,0x00FFFFFF);
hFont=CreateFont(80,0,0,0,FW_BOLD,0,0,0,0,0,0,2,0,_T("SYSTEM_FIXED_FONT"));
hTmpFnt=(HFONT)SelectObject(hDC,hFont);
SetBkMode(hDC,TRANSPARENT);
DrawText(hDC,szBuffer,13,&rc,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
DeleteObject(SelectObject(hDC,hTmpFnt));
EndPaint(Wea->hWnd,&ps);
return 0;
}
long fnWndProc_OnClose(lpWndEventArgs Wea)
{
DestroyWindow(Wea->hWnd);
PostQuitMessage(0);
return 0;
}
LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
WndEventArgs Wea;
for(unsigned int i=0; i<dim(EventHandler); i++)
{
if(EventHandler[i].Code==msg)
{
Wea.hWnd=hwnd, Wea.lParam=lParam, Wea.wParam=wParam;
return (*EventHandler[i].fnPtr)(&Wea);
}
}
return (DefWindowProc(hwnd,msg,wParam,lParam));
}
int WINAPI WinMain(HINSTANCE hIns,HINSTANCE hPrevIns,LPSTR lpszArgument,int iShow)
{
TCHAR szClassName[]=_T("Hello");
WNDCLASSEX wc;
MSG messages;
HWND hWnd;
wc.lpszClassName=szClassName; wc.lpfnWndProc=fnWndProc;
wc.cbSize=sizeof (WNDCLASSEX); wc.style=CS_VREDRAW|CS_HREDRAW;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wc.hInstance=hIns;
wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION); wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wc.cbWndExtra=12;
wc.lpszMenuName=NULL; wc.cbClsExtra=0;
RegisterClassEx(&wc);
hWnd=CreateWindow(szClassName,szClassName,WS_OVERLAPPEDWINDOW,150,150,500,450,HWND_DESKTOP,0,hIns,0);
ShowWindow(hWnd,iShow);
UpdateWindow(hWnd);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.