Custom Combo Box re-draw problem

G

Guest

I’m having problems with a custom Combo box. The main problem is that I want
to modify the look of the combo which includes the size of the button. To do
this I have inherited from the standard combo set it as a UserPaint and
provided my own paint. This along with the standard Owner Draw for the list
items works fine for the DropDownList version, the problems start with the
versions with the edit box which are where the problems are.

So to get hold of the edit box so I can modify it I have subclassed it using
a NativeWindow class via a DllImported ComboBoxInfo method to get the edit
box Windows handle. After this I can resize the Edit box to fit (as I
resized the button) and to set the font to that of the main Combo box using
other DllImport methods (SendMessage, SetWindowPos etc). The problem is in
the edit box’s painting of its background… it doesn’t. If you delete text
the cursor moved but the text stays there and if you coved the combo box with
another Window and then switch back, the background if not repainted and the
covering Window graphics remain.

Anyone got any ideas? I do have a sample solution is anyone is interested.
 
Y

Yan-Hong Huang[MSFT]

Hello Duncan,

If it is convenient for you, could you please upload a simple sample to
demo it? Please list the detailed repro steps and we will help look into it
for you.

Thanks very much.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://msdn.microsoft.com/subscriptions/managednewsgroups/

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi

Yes, is there another location to upload files to? Sorry if that's a daft
question, but I'm a newbie here.

Thanks

Duncan
 
J

Jeffrey Tan[MSFT]

Hi Duncan,

Thanks for your feedback.

You can use Outlook Express to attach the sample demo project in a
reply(Use the "Insert" menu in Outlook Express reply window).

However, before this, I have some questions regarding your issue.

Based on my understanding, you are doing owner-draw with combobox in .Net.
Can you show me what does the this sentense mean: "modify the look of the
combo which includes the size of the button"? Is the "button" the dropdown
arrow of combobox?

I am not sure why you have to use NativeWindow class to do subclassing. To
do owner-draw with combobx, we only need to handle MeasuareItem and
DrawItem events. For a sample, please refer to the article below:
"Owner Draw ComboBox"
http://www.csharphelp.com/archives2/archive316.html
"Create Owner-Draw List and Combo Boxes with .NET (cont'd)"
http://www.devx.com/dotnet/Article/8014/0/page/3

If you want to draw on Edit portion of combobox, please refer to the
article below:
"Adding an Icon or Control to a TextBox or ComboBox"
http://www.vbaccelerator.com/home/NET/Code/Controls/ListBox_and_ComboBox/Tex
tBox_Icon/article.asp

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Hi Jeffrey

An answer from my colleague who is actually working on the control, wth
regard to the 2 articles you referred to:

Actually I want to do both. Redrawing in the listbox works fine. Using a
technique similar to the vbacceleratro article (they use NativeWindow) I am
getting the textbox in order to adjust the size and font but in my case the
textbox does not repaint it's background properly.


He is putting together a simple demo.

Unfortunately we are not able to use Outlook Express (or any other news
reader) as NNTP is blocked on our network so I have to use the web-based
reader. I can't see any way to upload a demo using that. Is there someone I
can email it to?

Duncan
 
J

Jeffrey Tan[MSFT]

Hi Duncan,

Thanks for your feedback.

If you can create a simple sample project, you can send it to me at:
(e-mail address removed)(remove "online."). I will give a review. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Hi Jeffrey

Hopefully you have received the example code from my colleague, Simon Smith.

Regards

Duncan
 
J

Jeffrey Tan[MSFT]

Hi Duncan,

Thanks for your feedback.

Yes, I have received the sample project and given it a review. It seems
that your current problem is how to set the edit portion of combobox
background color. Actually, Edit control will send a WM_CTLCOLOREDIT
notification message to its parent window(combobox in our scenario) when
the control is about to be drawn. By responding to this message, the parent
window can use the specified device context handle to set the text and
background colors of the edit control.

Below is the code snippet I added to the MyCombo class, after adding this
code snippet, it works well on my side now:

int WM_CTLCOLOREDIT = 0x133;
IntPtr hBrush = IntPtr.Zero;

[DllImport("gdi32.dll", EntryPoint = "CreateSolidBrush", CharSet =
CharSet.Auto, SetLastError = true, ExactSpelling = true)]
private static extern IntPtr CreateSolidBrush(int crColor);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true,
ExactSpelling = true)]
public static extern int SetBkColor(IntPtr hDC, int clr);

protected override void WndProc(ref Message m)
{
if (m.Msg == WM_CTLCOLOREDIT)
{
IntPtr hdcEdit = m.WParam;
IntPtr hEdit = m.LParam;
if (hEdit != IntPtr.Zero && hdcEdit != IntPtr.Zero)
{
hBrush= CreateSolidBrush(ColorToCOLORREF(Color.Blue));
SetBkColor(hdcEdit, ColorToCOLORREF(Color.Blue));
m.Result = hBrush;
return;
}
}
base.WndProc(ref m);
}

public static int ColorToCOLORREF(Color color)
{
return ((color.R | (color.G << 8)) | (color.B << 0x10));
}

Hope it helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Thanks for that.

I have just tried your idea in my project and that sorts out the problem
nicely.
 
J

Jeffrey Tan[MSFT]

I am glad it can help you. If you need further help, please feel free to
post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top