- Hbrush cstadus: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
- {
- Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
- // Todo: change any attributes of the DC here
- If (nctlcolor = ctlcolor_static)
- {
- PDC-> settextcolor (RGB (0, 0, 0); // set it to the color of your background.
- PDC-> setbkmode (0); // transparent
- Return (hbrush): getstockobject (null_brush );
- }
- // Todo: return a different brush if the default is not desired
- Return HBr;
- }
Hbrush cstadus: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor) {hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor); // todo: change any attributes of the DC hereif (nctlcolor = ctlcolor_static) {PDC-> settextcolor (RGB (0, 0, 0 )); // set the color of your background to PDC-> setbkmode (0); // transparent return (hbrush): getstockobject (null_brush);} // todo: return a different brush if the default is not desiredreturn HBr ;}
The above Code makes the static control transparent, but when the content of the static control is changed through setwindowtext (), it is found that the text is overlapped, the more calls, the more serious the overlap is, the more black it is. After studying for a long time, I finally found a solution
Method 1: redrawwindow ()
Add this function after the code of the control that needs to change the text, as shown below:
- Getdlgitem (idc_static)-> setwindowtext ("your string ");
- Getdlgitem (idc_static)-> getparent ()-> redrawwindow ();
Getdlgitem (idc_static)-> setwindowtext ("your string"); getdlgitem (idc_static)-> getparent ()-> redrawwindow ();
This method works well, but sometimes the window is refreshed too frequently with a flash, and the effect is not very good. Fortunately, we have some tips and can use partial refresh.
Method 2: Partial refresh
You can customize a function as follows: void yourdlg: refreshcontrol (uint uctlid)
- {
- Crect RC;
- Getdlgitem (uctlid)-> getwindowrect (& rc );
- Screentoclient (& rc );
- Invalidaterect (RC );
- }
Void yourdlg: refreshcontrol (uint uctlid) {crect RC; getdlgitem (uctlid)-> getwindowrect (& rc); screentoclient (& rc); invalidaterect (RC );}
Call this function every time you change the control content. This method is recommended.