本文介绍了在C ++中设置静态背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿!

我想更改静态背景的颜色.

Hey!

I want to change the background color of a static.

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"是我要更改其颜色的HWND.

这是一个普通的静态"窗口:)

请帮帮我!



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

It''s an ordinary "STATIC" window :)

Please help me!

推荐答案

#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.




这篇关于在C ++中设置静态背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:54