[Win32API] ComboBox Not Selectable Item

Pages: 12
title says everything, how to? plain win32 API

somthing like

Section Name - not selectable
Section item 1 - selectable
Section item 2 - selectable
http://www.cplusplus.com/forum/windows/ Win32, MFC, ATL, C++/CLI, .NET ...
don't post if u can't help, this is beginners forum, everything a beginner is doing belongs here
ne555 is trying to tell you that there's a forum right here for Windows and that you should post there. Even if you are a beginner, your question is about programming for Windows and not some simple C or C++ concept, which is what the Beginners forum is for.

You can move your questions yourself to the Windows forum by editing the post.

As for your question in particular: You may be trying to create a combobox that probably needs to tell the user "--Select One--", right? If so maybe making the option unselectable is not the way to go. Maybe you can take a simpler approach of just validating the choices in the dialog box to see if a valid option was selected, and if it is not the case simply inform the user about the fact.
Why would you put something in a combo box that cant be selected?
@webJose I've posted some windows stuff in windows section several times, never got a response

I want to create unselectable item to create "sections"

for example combobox will contain:

Ranged // unselectable

Melee // unselectable

i've saw in several APPs that when u place mouse over those unselectable items u simply can't select them... it won't highlight them and u can't click on them
Last edited on
Personally I've never seen any. Can you give concrete examples to see if we can check them out?

Since you seem to be unwilling to follow alternative methods to accomplish your task, I can only think about subclassing the combobox control to try to gain control over what can and cannot be selected. I think that maybe subclassing the underlying listbox is the way to go. Process WM_NCHITTEST. If it is HTCLIENT and happens to be on top of the first item, see if you can get your desired result by returning HTBORDER instead.
well the first app that came in my mind is Microsoft Word and Font selecting...

u have there Recent Fonts, Theme Fonts and All Fonts sections that are unselectable

edit: + I have no idea what you said except subclassing... (which I've never tried before and have no idea what exactly it is but found some tuts on msdn)
Last edited on
I've done this a few times where the top item in the comboBox is the instruction telling you to select something.

I just ignore the input or associate it with a blank or default object when that Invalid/Instruction item is used.
Ah, ok. I think those are owner-drawn comboboxes. See http://msdn.microsoft.com/en-us/library/windows/desktop/hh404152(v=vs.85).aspx for a sample of this, but note that I don't think the sample is disabling any items. I think that part would still require subclassing of the underlying listbox to deactivate mouse hovering effects + selection.

I still don't get that subclassing part, WHAT is subclassing? and how does it works? i'm not asking for code, i'm asking for explaination on what it is :P

@Stewbond well I need to disable that mouse hovering highlighting and selection as webjose said.. :P
Subclassing is the process of deviating the flow of window messages to a new window procedure. This is done for a variety of reasons, but the chief reason is the alteration of some stock behavior. In your case you want to alter the behavior of the stock combobox by disallowing the selection of one of the items.

A window procedure's entry point's address is stored with the individual window (all controls are windows) and can be retrieved or overwritten using GetWindowLongPtr() and SetWindowLongPtr(). The process of subclassing consists of a few simple steps:

1. Retrieve the current window procedure's address using GetWindowLongPtr(). Store this value for future use.
2. Set a new window procedure by using SetWindowLongPtr().

That's it. Now the window is subclassed and all window messages (notifications as they are called in MSDN) are diverted to the new window procedure, which is one you write.

Inside your new window procedure you only write code for the window messages you want/need in order to accomplish your task. All other messages are redirected to the original window procedure (whose address was stored in step 1 above) using CallWindowProc().

That's subclassing in a nutshell. In order to grasp this concept you must be familiar with the traditional paradigm for Windows Programming, which is a message-driven paradigm.
I don't really think I get it.. can't I just do the same thing in first window proc as in second window proc? why do I have to divert messages to second window proc?
All messages must be processed. Will you re-write every behavior of the combobox? The answer is NO. You are only interested in re-write the behavior of a very specific thing, not everything. By everything I mean everything: Making the list visible, calculating scrollbars, adding visual mouse-hovering effects, drawing the combobox, sending notifications to the parent window, etc. etc. etc. If this actually had to be done, then Subclassing wouldn't exist as it would provide nothing useful.

So you process the messages related to the behavior you want to change and then simply redirect all others to the original window procedure.
okay, but when I select something in combo box for example, why would I derivate it to another windowproc and handle it there? it makes no sense O_o

or if there's any way to remove highlighting by message, why do I have to create another procedure and not do it in original proc?
Subclassing is not a trivial topic. You seem to lack the mastery required to understand it. Your next move should be to study how Windows works and how windows receives messages. Once you grasp the concept well, you can start studying subclassing. I cannot possibly explain subclassing in a few forum posts so it is best if you look up tutorials and then just clear any doubts after reading here in the forum.

To try to answer your question (although I'm unsure if you'll understand without studying): You cannot do it in the original window procedure because you did not write the original procedure for the standard combobox. That window procedure was written by Microsoft personnel and it is stored in user32.dll (most likely). You MUST write a new window procedure for your combobox in order to alter its stock behavior. This is the core of the subclassing technique. Is this any more clear?
┬┐why don't just use 2 combo-box, where the contents of the second are generated dynamically according to the first choice?
@webJose wait, you mean procedure for window such as button, combobox, listbox etc? not procedure for main window? if so it makes sense then... I guess...

@ne555 cuz something like that wouldn't be really effective :P but I guess I'll have to do it like that..
Yes, if you subclass the combobox you'll be replacing the Microsoft-provided window procedure for the combobox class with one of your own making. Your new window procedure for the combobox will only process those messages required to achieve your goal and then you'll redirect all other messages to the original window procedure for the combobox class so you don't have to basically re-create the combobox in its entirety.
Pages: 12