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?
- // Import API functions
- [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 to release the device context (DC)
- Protected override void wndproc (ref message m)
- {
- Base. wndproc (ref m );
- // Wm_paint = 0xf; requires a window to redraw itself, that is, when the painting event
- // Wm_ctlcoloredit = 0x133; send this message to its parent window when an edit control is to be drawn;
- // 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.
- // 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, the system returns
- {
- Return;
- }
- // Create a graphics image
- Graphics G = graphics. fromhdc (HDC );
- // Border
- Controlpaint. drawborder (G, new rectangle (0, 0, width, height), color. Red, buttonborderstyle. Solid );
- // Draw a solid line
- 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 );
- }
- }
// 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