Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey!

I want to change the background color of a static.

C++
mainhwnd = CreateWindowEx(blah,blah,blah ordinary window settings!, blah);
texthwnd = CreateWindowEx(0, "STATIC", "RAM: 99%",
                          WS_CHILD | WS_VISIBLE | SS_SIMPLE,
                          10, 10, 100, 100, mainhwnd, 0, 0, 0);


"texthwnd" is the HWND i want to change color of.

It's an ordinary "STATIC" window :)

Please help me!
Posted
Comments
Philippe Mori 17-Sep-11 18:29pm    
Is there any reason that you program at Win32 API level if you are new to programming. For example, WinForms programming with C# is 10 time easier to do.
ChrilleFkr 18-Sep-11 4:06am    
I like C++ for it's syntaxes and stuff. The way it looks :)

Here is your example:
C++
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>

#pragma comment (lib,"User32.lib")
#pragma comment (lib,"Gdi32.lib")

HWND      texthwnd = 0;
COLORREF  textcol = RGB(0,0,0);

void OnCreate(HWND hParent)
{
  texthwnd = CreateWindowEx(0, __T("STATIC"), __T("RAM: 99%"),
                            WS_CHILD | WS_VISIBLE | SS_SIMPLE,
                            1010100100, hParent, 000);
  SetTimer(hParent,100,100,0);
}

LRESULT OnCtlColorStatic(HDC hdc,HWND hCtl)
{
  if(hCtl==texthwnd)
  {
    SetTextColor(hdc,textcol);
    return (LRESULT)GetStockObject(HOLLOW_BRUSH);
  }
  return 0;
}

void OnTimer(HWND hwnd,int idTimer)
{
  if(100==idTimer)
  {
    switch(textcol)
    {
      case RGB(0,0,0)  : textcol = RGB(255,0,0); break;
      case RGB(255,0,0): textcol = RGB(0,255,0); break;
      case RGB(0,255,0): textcol = RGB(0,0,255); break;
      default          : textcol = RGB(0,0,0); break;
    }
    InvalidateRect(hwnd,0,1);
  }
}

static LRESULT FAR PASCAL __wndproc(HWND h,unsigned int m,WPARAM w,LPARAM l)
{
  LRESULT  r;
  switch(m)
  {
    case WM_CTLCOLORSTATIC:
      if(r=OnCtlColorStatic((HDC)w,(HWND)l)) return r;
    break;

    case WM_CREATE:
      OnCreate(h);
    break;
    case WM_TIMER:
      OnTimer(h,w);
    break;

    case WM_CLOSE:
      DestroyWindow(h);
    break;

    case WM_DESTROY:
      PostQuitMessage(0);
    break;
  }
  return DefWindowProc(h,m,w,l);
}

int FAR PASCAL _tWinMain(HINSTANCE h,HINSTANCE p,LPTSTR c,int sw)
{
  WNDCLASS  wc = { 0,__wndproc,0,0,h,0,LoadCursor(0,IDC_ARROW),(HBRUSH)(COLOR_WINDOW+1),0,__T("mywnd") };
  HWND      hwnd;

  if(RegisterClass(&wc))
  {
    hwnd = CreateWindowEx(  0, wc.lpszClassName, __T("Hello world"),
                            WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0000);
    if(IsWindow(hwnd))
    {
      ShowWindow(hwnd,sw);
      for(MSG m;GetMessage(&m,0,0,0);)
      {
        TranslateMessage(&m);
        DispatchMessage(&m);
      }
    }
  }
  return 0;
}

Good luck.
 
Share this answer
 
Comments
ChrilleFkr 18-Sep-11 14:09pm    
Thaaanks alloooooootttt!!!!!!!!!!! :D
mbue 18-Sep-11 14:14pm    
its a pleasure. ;)
Handle the WM_CTLCOLORSTATIC[^] in the static's parent window (See the example in the posted link).
 
Share this answer
 
Comments
ChrilleFkr 17-Sep-11 17:05pm    
I'm sorry but i can't get it to work? I've put the example code into my code but it doesent change any color at all!
I'm new to programming and i dont understand what the example script does so it would be awesome with some explaination :)
Philippe Mori 17-Sep-11 18:27pm    
The documentation seems clear to me...

Are you using MFC or using plain Win32 API? By the way, this is old technology. Many things are complex to do according to today standard and it was done that way more that 20 years ago when the computer were having at most a few MB of memory and resources were very limited.

It is far easier to program using .NET and C#.
ChrilleFkr 18-Sep-11 3:39am    
I use C++ beacuse I think it looks smooth and it just felt like the language which where right for me.
I am using plain Win32 API and i dont need more than a few MB of memory for my project! :)

Well tell me what i should do then? I still prefer in C++, a code that creates a text, changes it's color and text in runtime?
 
Share this answer
 
Comments
ChrilleFkr 18-Sep-11 4:05am    
How? :O I dont know how i should use any of the information. Can you give me an example code? :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900