控件的背景色(winapi) [英] background color of a control (winapi)
问题描述
这就是我在说的:程序窗口
我知道,这个话题已经出版了好几次了,但是我找不到任何好的解决方案.
我的问题是
1 .:-闪烁的控件,用于显示时间和直方图(静态,不是双缓冲).
2 .:-我无法将控件(标签,轨迹栏好友,直方图bkgnd)的背景从白色更改为LTGRAY_BRUSH.
这是一些代码:
I know, this topic was several times published, but I can't find any good solution.
My problem is
1.: -flickering controls showing time and histogram (static, not double bufferred).
2.: -I can't change background of controls (labels, trackbar buddies, histogram bkgnd) from white to LTGRAY_BRUSH.
And here's some code:
case WM_PAINT:
PaintProcedure( hdcClock );
SetBkColor(hdcWindow, RGB(255,220,180));
TextOut(hdcWindow,260,10,TEXT(" -- CAMERA WINDOW -- "),21);
break;
... ...
void PaintProcedure( HDC hdc )
{
img = cam->getFrame();
cam->ShowImage(img, hdcCam, 1, 1, img->width, img->height, 0, 0);
InvalidateRect( hClock, &rClock, 1 );
RedrawWindow(hClock,&rClock,0,RDW_UPDATENOW);
char sTime[256];
SYSTEMTIME time;
HFONT hFont;
SIZE size;
...
...
BeginPath (hdc) ;
SetBkMode( hdc, TRANSPARENT/*OPAQUE*/ );
TextOut( hdc,1,1,sTime, strlen( sTime ) );
EndPath (hdc) ;
SelectObject (hdc, CreateHatchBrush (HS_DIAGCROSS, RGB (0, 0, 255))) ;
SetBkColor (hdc, RGB (255, 0, 0)) ;
SetBkMode (hdc, OPAQUE) ;
StrokeAndFillPath (hdc) ;
DeleteObject (SelectObject (hdc, GetStockObject (LTGRAY_BRUSH)));
SelectObject (hdc, GetStockObject (SYSTEM_FONT)) ;
DeleteObject (hFont) ;
cam->ShowImage(cam->drawIntensityHistogram(),hdcHistogram,1,1,
rHistogram.right-rHistogram.left,rHistogram.bottom-rHistogram.top,0,0);
InvalidateRect( hwnd, &r, 1 );
RedrawWindow(hwnd,&r,0,RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW);
}
添加此代码:
case WM_CTLCOLORSTATIC:
case WM_CTLCOLORDLG:
case WM_CTLCOLORBTN:
return (LRESULT)GetStockObject(LTGRAY_BRUSH);
导致仅绘制轨迹栏而没有框架. 我试图使用计时器来使控件无效并重画控件,但这无济于事. 现在我不知道如何解决它.有人可以帮我吗?
causes that only trackbars are drawn without a frame. I tried to use timers to invalidate and redraw controls but it doesn't help. Now I have no idea how to fix it. Could someone help me, please?
推荐答案
使用静态控件的源代码:
Source code using a static control:
LONG WINAPI WndProc(HWND, UINT, WPARAM,LPARAM);
HWND hWndStatic;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{ HWND hwnd;
MSG msg;
WNDCLASS w;
memset(&w,0,sizeof(WNDCLASS));
w.style = CS_HREDRAW | CS_VREDRAW;
w.lpfnWndProc = WndProc;
w.hInstance = hInstance;
w.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
w.lpszClassName = L"My Class";
w.hCursor = LoadCursor(NULL, IDC_ARROW);
RegisterClass(&w);
hwnd = CreateWindow(L"My Class", L"My title", WS_OVERLAPPEDWINDOW, 300, 200, 800, 600, NULL, NULL, hInstance, NULL);
hWndStatic = CreateWindowEx(0, L"Static", NULL, WS_CHILD| WS_VISIBLE |SS_LEFT, 10, 130, 200, 20, hwnd, NULL, hInstance, NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{ TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
UINT_PTR uTimerId;
LOGBRUSH lbrBk;
LONG WINAPI WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
{
switch (Message)
{ case WM_CREATE:
{ LRESULT lRes = DefWindowProc(hwnd, Message, wparam, lparam);
HBRUSH hbr = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
GetObject(hbr, sizeof(lbrBk), &lbrBk);
uTimerId = SetTimer(hwnd, 123, 1000, NULL);
return lRes;
}
case WM_CTLCOLORSTATIC:
if ((HWND)lparam == hWndStatic)
{ SetBkColor((HDC)wparam, lbrBk.lbColor);
return (BOOL) GetStockObject(LTGRAY_BRUSH);
}
return FALSE;
case WM_TIMER:
{ TCHAR szTime[128];
HDC hdc = GetDC(hwnd);
_tstrtime_s(szTime, _countof(szTime));
SetWindowText(hWndStatic, szTime);
ReleaseDC(hwnd, hdc);
break;
}
case WM_DESTROY:
KillTimer(hwnd, uTimerId);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, Message, wparam, lparam);
}
return 0;
}
这篇关于控件的背景色(winapi)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!