954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do I change background color WINAPI

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
Newbie Poster
16 posts since Mar 2011
Reputation Points: 10
Solved Threads: 1
 

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
Frederick2
Posting Whiz
330 posts since Jul 2008
Reputation Points: 244
Solved Threads: 31
 

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.

Voidz
Newbie Poster
16 posts since Mar 2011
Reputation Points: 10
Solved Threads: 1
 

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;
}
Frederick2
Posting Whiz
330 posts since Jul 2008
Reputation Points: 244
Solved Threads: 31
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: