Redraw ComboBox [C #].

Source: Internet
Author: User

Redraw ComboBox [C #]

Bytes -------------------------------------------------------------------------------------

By chance, I found out that override
The onpaint of ComboBox will not be called. Therefore, ComboBox cannot be re-painted like a common control.

Bytes -------------------------------------------------------------------------------------

We can rewrite the Window Process (wndproc) to process messages.

Create a class that inherits from ComboBox,

Class customecombobox: ComboBox

 

 

 

 

[C-sharp]
View plaincopyprint?
  1. // Import API functions
  2. [System. runtime. interopservices. dllimport ("user32.dll")]
  3. Static extern intptr getwindowdc (intptr hwnd); // return the device environment of the window specified by the hwnd parameter.
  4. [System. runtime. interopservices. dllimport ("user32.dll")]
  5. Static extern int releasedc (intptr hwnd, intptr HDC); // function to release the device context (DC)
  6. Protected override void wndproc (ref message m)
  7. {
  8. Base. wndproc (ref m );
  9. // Wm_paint = 0xf; requires a window to redraw itself, that is, when the painting event
  10. // Wm_ctlcoloredit = 0x133; send this message to its parent window when an edit control is to be drawn;
  11. // In response to this message, the owner window can set the text and background color of the edit box by using the handle of the specified display device.
  12. // Windows message value table, see: http://hi.baidu.com/dooy/blog/item/0e770a24f70e3b2cd407421b.html
  13. If (M. MSG = 0xf | M. MSG = 0x133)
  14. {
  15. Intptr HDC = getwindowdc (M. hwnd );
  16. If (HDC. toint32 () = 0) // if the device context fails to be retrieved, the system returns
  17. {
  18. Return;
  19. }
  20. // Create a graphics image
  21. Graphics G = graphics. fromhdc (HDC );
  22. // Border
  23. Controlpaint. drawborder (G, new rectangle (0, 0, width, height), color. Red, buttonborderstyle. Solid );
  24. // Draw a solid line
  25. Controlpaint. drawborder (G, new rectangle (width-height, 0, height, height), color. Red, buttonborderstyle. Solid );
  26. // G. drawline (new pen (brushes. blue, 2), new pointf (this. width-this. height, 0), new pointf (this. width-this. height, this. height ));
  27. // Release the DC
  28. Releasedc (M. hwnd, HDC );
  29. }
  30. }

// Import the API function [system. runtime. interopservices. dllimport ("user32.dll")] Static extern intptr getwindowdc (intptr hwnd); // return the device environment of the window specified by the hwnd parameter. [System. runtime. interopservices. dllimport ("user32.dll")] Static extern int releasedc (intptr hwnd, intptr HDC); // function release device context environment (DC) protected override void wndproc (ref message m) {base. wndproc (ref m); // wm_paint = 0xf; requires a window to re-paint itself, that is, when the painting event // wm_ctlcoloredit = 0x133; the parent window to which the message is sent when an edit control is to be drawn; // The parent window to which the message is sent, the owner window can set the text and background color of the edit box by using a given handle to the relevant display device // Windows message value table, see: http://hi.baidu.com/dooy/blog/item/0e770a24f70e3b2cd407421b.html if (M. MSG = 0xf | M. MSG = 0x133) {intptr HDC = getwindowdc (M. hwnd); If (HDC. toint32 () = 0) // if the device context fails to be retrieved, {return;} // create a graphics peer image graphics G = graphics. fromhdc (HDC); // draw the controlpaint of the border. drawborder (G, new rectangle (0, 0, width, height), color. red, buttonborderstyle. solid); // draw a solid controlpaint. drawborder (G, new rectangle (width-height, 0, height, height), color. red, buttonborderstyle. solid); // G. drawline (new pen (brushes. blue, 2), new pointf (this. width-this. height, 0), new pointf (this. width-this. height, this. height); // release the DC releasedc (M. hwnd, HDC );}}

Note: This article references the http://topic.csdn.net/u/20070728/01/7e5ebf43-fce8-4db1-a05c-d44c612d1391.html

 

 

From: http://blog.csdn.net/sugar_tiger/article/details/4900641

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.