我正在开发一个 Win32 GUI 项目,其中有一个主窗口,在里面显示 3 个按钮和 1 个带有 BITMAP 的标签。像这样: 主窗口
我声明了一个标签数组,并为每个标签附加了一个位图,我是这样做的:
/*Loading the bitmaps, creating the backgrounds and the buttons*/
for(i = 0; i <3; i++){
background_bitmaps[i] = (HBITMAP)LoadImage(NULL, background_strings[i], IMAGE_BITMAP, 275, 183, LR_LOADFROMFILE);
background_labels[i] = CreateWindow("STATIC", NULL, WS_CHILD | WS_VISIBLE | SS_BITMAP, 25, 50, 275, 183, hwnd, NULL, hThisInstance, NULL);
tab_buttons[i] = CreateWindow("BUTTON", buttons_strings[i] ,WS_CHILD | WS_VISIBLE, 10 + (50*i), 10, 40, 30, hwnd, NULL, hThisInstance, NULL);
SendMessageW(background_labels[i], STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)background_bitmaps[i]);
}
背景必须根据单击的按钮以及名为 value 的变量的值而变化,该变量的值可以是 1、2 或 4。
然后我像这样处理按钮的事件:
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
switch (message){
value = 0;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
case WM_COMMAND:
for(i = 0; i<3; i++)
if((HWND)lParam == tab_buttons[i]){
value = 1<<i;
setVisibleBackground(i);
}
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
setVisibleBacground 的声明是这样的:
/*Functions declarations*/
void setVisibleBackground(int _value){
for(i = 0; i<3; i++){
bool result = (_value == i)?TRUE:FALSE;
ShowWindow(background_labels[i], result);
}
}
到目前为止,一切正常,但现在我需要按下第三个按钮以显示第三个背景,然后绘制一个形状,例如背景上方的矩形或椭圆。
我试试这个:
case WM_PAINT:
hDC = BeginPaint(hwnd, &Ps);
EndPaint(hwnd, &Ps);
if(value == 4){
hDC = BeginPaint(background_labels[2], &Ps);
hPen = CreatePen(PS_NULL, 1, RGB(0, 200, 0));
SelectObject(hDC, hPen);
BrushOn = CreateSolidBrush(RGB(31, 127, 0));
SelectObject(hDC, BrushOn);
Ellipse(hDC, 160, 140, 210, 190);
EndPaint(background_labels[2], &Ps);
}
break;
单击第三个按钮时它的值将等于 4,我尝试像这样更改 setVisibleBackground 函数:
/*Functions declarations*/
void setVisibleBackground(int index){
for(i = 0; i<3; i++){
bool result = (index == i)?TRUE:FALSE;
ShowWindow(background_labels[i], result);
}
if( index == 2){
InvalidateRect(background_labels[2], NULL, FALSE);
UpdateWindow(hwnd);
}
}
但是这段代码只有在我按下第一个和第二个按钮时才有效,背景发生了变化,但是当我按下第三个按钮时它失败了,首先它绘制了圆圈,然后它显示了圆圈上方的背景,就像在这个 图像中一样
任何帮助将不胜感激