Quercus Posted March 7, 2004 Posted March 7, 2004 I'm working on a custom ComboBox control that displays its data in a highly formatted maner. Letting the ComboBox draw the data itself won't do for my purposes. I've already used the OnDrawItem to show my data in the drop down portion just the way I want it. The problem is, once a selection is made, it shows up in the main control in the same old bland appearance. I need to override the normal control painting. The combo box rarely, if ever, calls the OnPaint method. So I've used the WndProc method to trap for WM_PAINT: protected override void WndProc(ref Message m) { const int WM_PAINT = 0xF; // Let base process the message first. base.WndProc (ref m); // Check what message was sent. if (m.Msg == WM_PAINT) { IntPtr hdc = GetDC(this.Handle); Graphics G = Graphics.FromHdc(hdc); // This will fill the whole thing with BlueViolet. Not what I really want // but will demonstrate if this is working or not. G.FillRectangle(System.Drawing.Brushes.BlueViolet, this.ClientRectangle); } } [/Code] What I get is a control that has Violet in all areas [b]except[/b] for the data region, which is what I want to paint. I.e. the border and down button areas are a solid violet, but the text area is unchanged. It would appear that either it is not letting me draw in the data area, or it is re-painting after I paint. Does anyone know how I can override the ComboBox's painting so that I can draw my data just the way I want? My only other thought is to place a TextBox/Label/RichTextBox over the text region of the control and draw to that, but I'd rather not do this if I don't have to. Thanks. Quote
Quercus Posted March 7, 2004 Author Posted March 7, 2004 ARRGH! I've been experimenting with placing a blank Control on top of my inherited ComboBox. The ComboBox still draws on top of my empty control! public MyCombo() { // This call is required by the Windows.Forms Form Designer. InitializeComponent(); // Place a blank control on top of this one for me to // draw on myself // Create new control. TextArea, declared earlier, is // just a blank control. Its only function is a slate that // I can draw to. textArea = new TextArea(); // Place it on the control. This method sizes it to fill // just the areas of the combo box I want it to. LocateTextArea(); // This should show an Aqua colored slate in front of the normal // text area of the combo box. textArea.BringToFront(); this.Controls.Add(textArea); textArea.BackColor = Colors.Aqua; textArea.Show(); } Even with this, the text area of the control show up as if it was alone. textArea is only visible as a few pixels here and there. Is there any way that I can paint on top of the combo box text area???? Thanks. Quote
*Gurus* divil Posted March 7, 2004 *Gurus* Posted March 7, 2004 Drawing combo boxes is a pain. The first thing I notice about your code is that you're not releasing the DC you got, a very bad thing not to do. Also I would circumvent that problem entirely by using Graphics.FromHwnd instead of FromHdc. You should not try to paint on top of the text box area as this will have unpredictable results - if you want to owner draw the combo's border you should paint only the bits that aren't the text box (you can use the system metrics to get the areas to draw). If all you want is for your ownerdrawn items to show up in the combobox, then you should just use the DropDownList style and it will do it automatically. Also there will be no textbox. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Quercus Posted March 8, 2004 Author Posted March 8, 2004 Thanks Devil. I got a lot out of your DesignTime support tuturials and have used it elsewhere in this project. Problem is, DropDownList does work, but that's not what I want. I want the user to be able to edit the text or enter his/her own. And because of the way I have formatted the data here, I will probably need to implement this functionality manually as well. So it brings be back to square one. I still need to be able to either directly draw to the text area of the control, or I need to be able to place a different control on top of the combo box. Any thoughts on how I can do these? Quote
*Gurus* divil Posted March 8, 2004 *Gurus* Posted March 8, 2004 You could use GetWindow to get the handle of the child textbox window. You would have to do this whenever the parent combobox's handle was recreated, i.e. by overriding OnHandleCreated. You'd then have to subclass this handle for the WM_PAINT message by create a class that inherits from NativeWindow. Remember to unbind this class in the parent combobox's OnHandleDestroyed method. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
fadi Posted March 8, 2004 Posted March 8, 2004 forget about creating ur combo box, there is an xp look combo named Sandbar search for it on the web Quote
*Gurus* divil Posted March 8, 2004 *Gurus* Posted March 8, 2004 Or click on the link in my signature... Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Quercus Posted March 8, 2004 Author Posted March 8, 2004 Sandbar is not the direction I'm going in, but thanks anyway. As far as the subclassing approach you are talking about, I'll have to look into this a bit. Got any links that will start me in the right direction? Got any techniques I can use to force another control on top of the inherited combo box? Thanks. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.