We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Adding the CBS_OWNERDRAWVARIABLE after the combobox is created

mike_marquet
mike_marquet asked
on
Medium Priority
1,429 Views
Last Modified: 2013-11-20
How can I add the CBS_OWNERDRAWVARIABLE after the combobox control is created ?

I have try this :

void CExtendedComboBox::PreSubclassWindow()
 {
  ModifyStyle(0, CBS_OWNERDRAWVARIABLE | CBS_HASSTRING);

  CComboBox::PreSubclassWindow();
 }

But it has no effect.

Any idea ?
Comment
Watch Question

AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
Do you need to remove a different style setting that conflicts with the variabledraw?
mahesh1402IT Professional
Top Expert 2005

Commented:
You can use CBS_OWNERDRAWFIXED; it's just that you need to put an OnMeasureItem() handler in the parent window's class, since the WM_MEASUREITEM message is sent BEFORE the combo box is created.

MAHESH

Author

Commented:
I want to make a standalone class to put into a library, therefore, don't want to write code in the parent class.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
Just to be certain - is the PreSubclassWindow is being called.  Put a breakpoint in it and see if it is hit.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
Oh - as this is the **Pre** function is it actually attached to the combo at this point?  In other words is your code 'failing' because you aren't changing the style of the combo !
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
Give it a try in the SubclassWindow function instead (call the base function first, then ModifyStyle, also check with TRACE/Breakpoint that it gets called).

Author

Commented:
PreSubclassWindow is called but it seems to be to late to change this style.

SubclassWindow is never called !
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
PresubclassWindow - have you checked that there is a valid HWND at that point?

If there isn't then try SubclassDlgItem (there are two functions for subclassing! why - that I don't know)

Author

Commented:
I have a valid HWND in PreSubclassWindow
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
I've had a look at it here and done some searching.  It seems like there is no (easy) way to do it.

I would recommend that in the OnCreate and PreSubclassWindow functions you check the style, force a halt if it doesn't have the required styles.  
if(!(GetStyle() & CBS_OWNERDRAWVARIABLE))
{
  AfxMessageBox("Requires Owner Draw Variable style");
  ASSERT(false);
}

Author

Commented:
I have already written an ASSERT in the PreSubclassWindow, but would just try to find a way to hide it.
CERTIFIED EXPERT
Author of the Year 2009
Commented:
In this thread, respected EE Expert migel suggests that this is one of those styles that cannot be changed after creation:
     http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_11443919.html
and he described the steps to create a new control with the desired attributes and populate it from the contents of the existing one.

-- Dan

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT
Commented:
What I suggested in my previous comment is to provide a mechanism to warn any other developer that it won't work and why it wont work.
To change the style in the resource editor (prior to subclassing) is rapidly done.  A similar check in the OnCreate will also trap the case of a developer using Create to create a control dynamically.
The message box is to provide a warning.  The ASSERT will prevent the app from continuing in DEBUG mode.  Application development is usually performed in DEBUG mode.

Author

Commented:
When no way, I still stay with my ASSERT.
CERTIFIED EXPERT
Author of the Year 2009

Commented:
A interesting side note:  
VisualBasic programmers might be amused to hear that some "window attributes" cannot be changed easily at any time... because the VB libraries have been written to overcome such Win32 API limitations.  That huge VB runtime libary includes code to automatically and transparently destroy and recreate windows when necessary.  In C++, we are working a little bit "closer to the metal" :-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.