突出显示Win32编辑控件以指示它是现在填充的控件

时间:2022-02-27 00:02:29

I have a Win32 GUI application that has several edit controls (plain old "EDIT" classname).

我有一个Win32 GUI应用程序,它有几个编辑控件(普通的旧“EDIT”类名)。

The logic is that the user is to fill the edit box selected by the application. To make it clearer which one is to be filled now I want to somehow highlight the "current" edit box. Then, when the user has done input and asked the application to proceed the edit box would have to become "usual" again.

逻辑是用户要填充应用程序选择的编辑框。为了更清楚地说明要填写哪一个,我想以某种方式突出显示“当前”编辑框。然后,当用户完成输入并要求应用程序继续时,编辑框必须再次变为“通常”。

The ideal way would be to paint its background with a color of choice. How could I achieve this or similar selection - maybe I could substitute the brush used to paint the control temporarily? If it's not possible with edit control what replacement controls available in Windows starting with Win2k are out there?

理想的方法是用选择的颜色绘制背景。我怎么能实现这个或类似的选择 - 也许我可以替换用于临时绘制控件的画笔?如果使用编辑控件无法在Windows中启用哪些替换控件,那么从Win2k开始?

1 个解决方案

#1


You can handle the WM_CTLCOLOREDIT notification in the parent window for the edit controls. The notification is sent when the edit control is about to be drawn. So in general, you would use RedrawWindow or something to force a redraw, then handle the inevitable control colour notification. In this, you set the fore and back colour for the device context which is passed in with the notification:

您可以在父窗口中处理编辑控件的WM_CTLCOLOREDIT通知。在即将绘制编辑控件时发送通知。所以一般来说,你会使用RedrawWindow或其他东西强制重绘,然后处理不可避免的控件颜色通知。在此,您可以为通知传入的设备上下文设置前后颜色:

LRESULT OnControlColorEdit(HWND hwnd, DWORD msg, WPARAM wParam, LPARAM lParam)
{
   if( !toHighlight ) {
       return DefWindowProc( hwnd, msg, wParam, lParam );
   }
   HDC dc = reinterpret_cast<HDC>(wParam);
   ::SetBkColor(dc, whatever);
   ::SetTextColor(dc, whatever);
   HBRUSH brush = // create a solid brush of necessary color - should cache it and destroy when no longer needed
   return reinterpret_cast<LRESULT>( brush );
}

#1


You can handle the WM_CTLCOLOREDIT notification in the parent window for the edit controls. The notification is sent when the edit control is about to be drawn. So in general, you would use RedrawWindow or something to force a redraw, then handle the inevitable control colour notification. In this, you set the fore and back colour for the device context which is passed in with the notification:

您可以在父窗口中处理编辑控件的WM_CTLCOLOREDIT通知。在即将绘制编辑控件时发送通知。所以一般来说,你会使用RedrawWindow或其他东西强制重绘,然后处理不可避免的控件颜色通知。在此,您可以为通知传入的设备上下文设置前后颜色:

LRESULT OnControlColorEdit(HWND hwnd, DWORD msg, WPARAM wParam, LPARAM lParam)
{
   if( !toHighlight ) {
       return DefWindowProc( hwnd, msg, wParam, lParam );
   }
   HDC dc = reinterpret_cast<HDC>(wParam);
   ::SetBkColor(dc, whatever);
   ::SetTextColor(dc, whatever);
   HBRUSH brush = // create a solid brush of necessary color - should cache it and destroy when no longer needed
   return reinterpret_cast<LRESULT>( brush );
}