About Transparent (redrawing) of CEdit controls

Source: Internet
Author: User
Tags set background

Excerpt from: http://www.jcwcn.com/html/VC/10_19_51_12.htm

The main problem of making a transparent edit control is the output of the character, and there are several times when the output is refreshed in edit, one is when the keyboard or mouse message is received, and the WM_PAINT message is received. It is not all redrawn at the time of the refresh, so it is not feasible to handle WM_PAINT messages in the inherited edit class. But the edit control always knows how to refresh, so just send a message to the control so that it can refresh itself. By using Spy + + to know the need to refresh a few times, one is the key when the content changes, the other is the choice of changes, the former edit control will receive the GetCtlCode and KeyUp messages, The latter receives GetCtlCode and capturechange messages or KeyUp messages, so RedrawWindow is called in GetCtlCode to force edit to flush the entire control. In RedrawWindow, you can make the control repaint the background by using the parameter rdw_erase, which is called ONERASEBKGND (cdc* PDC), where the background is redrawn. A special case is to hold down the left mouse button and drag the mouse back and forth, when the choice to change, receive the message is MouseMove, in order to correct the response also to process the message, but in each MouseMove refresh display of the overhead is too large, and inevitably flicker sense, So only when the left mouse button is pressed to refresh the display.
The approximate code is the following, mainly inherited a CEdit object Ctpedit, when used can be dynamically created, or the use of subclass method. I'm using the latter.

[CPP]View PlainCopyprint?
  1. Class Ctestdlg: Public CDialog
  2. {
  3. ......
  4. Declaring a CTPEDIT member variable
  5. Private
  6. Ctpedit M_tpedit;
  7. };
  8. Subclass the Edit Control in a dialog box template in OnInitDialog
  9. BOOL Ctestdlg::oninitdialog ()
  10. {
  11. CDialog::OnInitDialog ();
  12. M_tpedit.   SubclassDlgItem (Idc_edit, this);
  13. return TRUE;
  14. }
  15. Set the transparency of the background in OnCtlColor, to change the color of the edit control font is also here
  16. Hbrush Ctestdlg::onctlcolor (cdc* PDC, cwnd* pWnd, UINT nCtlColor)
  17. {
  18. Hbrush HBR = Cdialog::onctlcolor (PDC, PWnd, nCtlColor);
  19. if ((nCtlColor = = Ctlcolor_edit) && (Pwnd->getdlgctrlid () ==idc_edit))
  20. {
  21. Pdc->setbkmode (TRANSPARENT); //Set background transparent, so that when the output character is
  22. is the so-called hollow word, instead of having a white background
  23. Pdc->settextcolor (RGB (255,0,0)); //Change the color of the font
  24. Return Hbrush (Getstockobject (Hollow_brush));
  25. }
  26. return HBR;
  27. }
  28. Ctpedit Object
  29. #ifndef _ccolor_edit_h_
  30. #define _ccolor_edit_h_
  31. Class Ctpedit: Public CEdit
  32. {
  33. Public
  34. M_mousedown is used to record whether the left mouse button is pressed
  35. BOOL M_mousedown;
  36. Protected
  37. Respond to the following message
  38. {{afx_msg (Ctpedit)
  39. afx_msg BOOL onerasebkgnd (cdc* PDC);
  40. afx_msg void OnMouseMove (UINT nflags, CPoint point);
  41. afx_msg void OnLButtonDown (UINT nflags, CPoint point);
  42. afx_msg void OnLButtonUp (UINT nflags, CPoint point);
  43. afx_msg UINT OnGetDlgCode ();
  44. }}afx_msg
  45. Declare_message_map ()
  46. };
  47. {{Afx_insert_location}}
  48. #endif
  49. The message response function for Ctpedit is as follows
  50. #include "stdafx.h"
  51. #include "ColorEdit.h"
  52. Drawing background picture
  53. BOOL ctpedit::onerasebkgnd (cdc* PDC)
  54. {
  55. Get the edit control's outer frame, which is the background area
  56. RECT Updatarect;
  57. GetClientRect (&updatarect);
  58. Drawing background, I'm drawing a yellow rectangle.
  59. CBrush Newbrush;
  60. Newbrush.createsolidbrush (RGB (255,255,200));
  61. CBrush * Oldbrush = Pdc->selectobject (&newbrush);
  62. Pdc->rectangle (&updatarect);
  63. Pdc->selectobject (Oldbrush);
  64. return TRUE;
  65. }
  66. Force edit control to erase background, write characters
  67. UINT Ctpedit::ongetdlgcode ()
  68. {RedrawWindow (NULL, Null,rdw_invalidate | Rdw_erase);
  69. return Cedit::ongetdlgcode ();
  70. }
  71. Record whether the left mouse button is pressed
  72. void Ctpedit::onlbuttondown (UINT nflags, CPoint point)
  73. {
  74. M_mousedown = TRUE;
  75. SetCapture ();
  76. Cedit::onlbuttondown (nflags, point);
  77. }
  78. void Ctpedit::onlbuttonup (UINT nflags, CPoint point)
  79. {
  80. if (M_mousedown)
  81. ReleaseCapture ();
  82. M_mousedown = FALSE;
  83. Cedit::onlbuttonup (nflags, point);
  84. }
  85. If you left-click and drag the mouse, the display will refresh
  86. void Ctpedit::onmousemove (UINT nflags, CPoint point)
  87. {
  88. if (M_mousedown)
  89. RedrawWindow (NULL, Null,rdw_invalidate | Rdw_erase);
  90. Cedit::onmousemove (nflags, point);
  91. }
  92. Initializing member variables
  93. Ctpedit::ctpedit ()
  94. {
  95. M_mousedown=false;
  96. }
http://blog.csdn.net/witch_soya/article/details/6889970

About Transparent (redrawing) of CEdit controls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.