在使用WS_EX_TRANSPARENT绘制的C Win32 API STATIC控件中更新文本 [英] Updating text in a C Win32 API STATIC control drawn with WS_EX_TRANSPARENT

查看:687
本文介绍了在使用WS_EX_TRANSPARENT绘制的C Win32 API STATIC控件中更新文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些静态标签和按钮的窗口。
我使所有的LABELS透明背景,所以我可以让背景红色说。
在CALLBACK我处理WM_CTLCOLORSTATIC消息,确定控件的ID与GetDlgCtrlID()然后:

  SetBkMode((HDC)wParam,TRANSPARENT); //使STATIC控制Bkgd透明
return(INT_PTR)(HBRUSH)GetStockObject(NULL_BRUSH);

到目前为止很好。



在用户交互后,我需要更改文本,所以我发出一个SetDlgItemText()消息和新文本是绘制。问题是旧的文本不会被删除,新的文本绘制在它的顶部。



今天读了一些,似乎问题是控件父形式)负责绘制背景。这意味着当您更改标签文本时,控件会重新绘制新文本,但表单不会自动重绘背景。



这是我如何强制重新绘制标签控件的矩形区域的形式(最好没有子类化任何东西)?



ADDED:



我试过下面的:

  HWND hctrl; 
hctrl = GetDlgItem(hwnd,ControlID);
RedrawWindow(hctrl,0,0,
RDW_UPDATENOW || RDW_ALLCHILDREN || RDW_FRAME || RDW_INVALIDATE || RDW_ERASE || RDW_INTERNALPAINT); // RDW_UPDATENOW

和:



不处理WM_PAINT消息,只有:

  case WM_CTLCOLORSTATIC:
SetBkMode((HDC)wParam,TRANSPARENT );
return(INT_PTR)(HBRUSH)GetStockObject(NULL_BRUSH);




int库:: SetControlTxt(int ControlID,string sText)//对话框
{
int RetVal;

RetVal = SetDlgItemText(hwnd,ControlID,sText.c_str());
RECT rect;
HWND hctrl;
hctrl = GetDlgItem(hwnd,ControlID);
GetClientRect(hctrl,& rect);
MapWindowPoints(hctrl,hwnd,(POINT *)& rect,2);
InvalidateRect(hwnd,& rect,TRUE);

return RetVal;
}

马克,谢谢这个作品。



 使用InvalidateRect控制方法。 RECT rect; 
GetClientRect(hctrl,& rect);
InvalidateRect(hctrl,& rect,TRUE);
MapWindowPoints(hctrl,hwnd,(POINT *)& rect,2);
RedrawWindow(hwnd,& rect,NULL,RDW_ERASE | RDW_INVALIDATE);


I have window with some STATIC labels and BUTTONs on it. I make all the LABELS transparent background so I can make the background RED say. In the CALLBACK i process the WM_CTLCOLORSTATIC message, determine the ID of the control with GetDlgCtrlID() and then:

SetBkMode((HDC)wParam, TRANSPARENT); // Make STATIC control Bkgd transparent
return (INT_PTR)(HBRUSH)GetStockObject(NULL_BRUSH);

So far so good. Form is drawn, background is RED and label text is drawn on top.

After user interaction I need to change the text, so I issue a SetDlgItemText() message and the new text is draw. The problem is the old text is not erased, and the new text is drawn on top of it.

Having read somewhat today, it seems the problem is the controls parent (the form) is responsible for drawing the background. This means that when you change the label text, the control redraws the new text, BUT the form doesn't automatically redraw the background.

THe question is HOW do I force the form to redraw the rectangle area of the label control (preferably without subclassing anything)?

ADDED:

I have tried the following:

HWND hctrl;
hctrl = GetDlgItem(hwnd, ControlID);
RedrawWindow( hctrl, 0, 0, 
RDW_UPDATENOW || RDW_ALLCHILDREN || RDW_FRAME || RDW_INVALIDATE || RDW_ERASE || RDW_INTERNALPAINT ); // RDW_UPDATENOW 

and:

I am not handling the WM_PAINT message at all, only:

case WM_CTLCOLORSTATIC:
 SetBkMode((HDC)wParam, TRANSPARENT); 
 return (INT_PTR)(HBRUSH)GetStockObject(NULL_BRUSH);




int Library::SetControlTxt( int ControlID, string sText  ) // Dialog Out
{ 
 int RetVal;

  RetVal = SetDlgItemText( hwnd, ControlID, sText.c_str() ); 
  RECT rect;
  HWND hctrl;
  hctrl = GetDlgItem(hwnd, ControlID);
  GetClientRect(hctrl, &rect);
  MapWindowPoints(hctrl, hwnd, (POINT *)&rect, 2);
  InvalidateRect(hwnd, &rect, TRUE);

       return RetVal;
} 

Mark, Thank you this works.

解决方案

Use InvalidateRect on the rectangle occupied by the control.

RECT rect;
GetClientRect(hctrl, &rect);
InvalidateRect(hctrl, &rect, TRUE);
MapWindowPoints(hctrl, hwnd, (POINT *) &rect, 2);
RedrawWindow(hwnd, &rect, NULL, RDW_ERASE | RDW_INVALIDATE);

这篇关于在使用WS_EX_TRANSPARENT绘制的C Win32 API STATIC控件中更新文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆