Solved

Windows Messages

Posted on 1998-04-22
9
387 Views
Last Modified: 2013-12-03
I'm having nothing but bad luck from Windows Messages.  Specifically, I am trying to add items to a combo box.  Here is the line I am using to add the item:

long err = SendMessage(hWnd, CB_ADDSTRING, MAKEWPARAM(0, IDC_COMBO), (LPARAM)Item);

I immediatly call GetLastError after this line, and get a value of 0x58d (Control ID not found.)  I have specified the Control ID in the Hiword of the wParam, so I don't understand why I get this error.  And yes, I did specify the same control ID when creating the window.  I have also tried the line like this:

long err = SendMessage(hWnd, CB_ADDSTRING, 0, (LPARAM)Item);

and get the same result.
0
Comment
Question by:dirtdart
  • 4
  • 4
9 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
Comment Utility
You need hWnd to be the HWND of the combo control itself

Note that the CB_ADDSTRING docco says

CB_ADDSTRING
wParam = 0;                         // not used; must be zero
lParam = (LPARAM) (LPCTSTR) lpsz;    // address of string to add
 
Also, I would use ComboBox_AddString or ComboBox_AddItemData instead (it is a little nice to code that way)

0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
#include <windowsx.h> to do what Roger suggested.
0
 
LVL 5

Author Comment

by:dirtdart
Comment Utility
I'm going to go ahead and grade your answer because you put forth the effort to answer.  However, that wasn't the problem.  hWnd is the hWnd of the control.  I realized the problem right after I left work.  I had created the control with the wrong control class specified (that's what happens sometimes when you cut and paste code).  If I change it to the correct class, I believe it will work as it should.
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
Well, at least you've got it fixed.  And you know that you don't need to include the control id in the message.  And you know that you can use ComboBox_AddItemData or AddString instead to make your code look neater (it worries about the SendMessage syntax for you).

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 5

Author Comment

by:dirtdart
Comment Utility
Actually, I'm creating an AddString method for my combobox.  I'm trying to stay completly away from MFC, because i hate using it.  But it only makes sense to wrap objects you are going to use more than once (combobox, textbox, etc).  I had figured that the control ID didn't need to be included in the SendMessage, but since it had said Control ID not found, I thought I'd give it a shot like that.  It wouldn't be the first time an API was obscure.
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
ComboBox_AddString is NOTHING to do with MFC .. it is part of the API - its is implemented as a Macro in <windowsx.h>.

Most standard SendMessage calls have API macro wrappers that make them easier to call.  That way you don't need to remember what lparam and wparam etc to use .. the macro builds them approriately for each message.

If there is such a macro for a particular message, then it is ALWAYS best to use the macro instead of the direct SendMessage because:
* you are less likely to make mistakes
* your code will still work even if the way a particular message is send is altered in a later version of Win32
* it is much easier to read.


0
 
LVL 5

Author Comment

by:dirtdart
Comment Utility
Ok.  I'll buy that.  Any chance you can tell me what the macro is to change the height of the stupid thing?  According to my error messages, everything worked beautifully, but I wouldn't know, because when I drop it down, it's only about one pixel high. AAARRRRGGGGGGGG!!!!!
0
 
LVL 5

Author Comment

by:dirtdart
Comment Utility
Never mind.  Just found it.  Duh!

0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
Are you using the dialog resource editor in VC?

If so, click on the drop down arrow of the combo box from within the editor. That will show you the size of the dropdown part.  Drag it to the deried size.

You can change the size (I think) with message, but getting it right in the dialog template is better.

Is this another question?  If so, then you might be better off posting another question (with whatever points you feel is suitable) so that other experts can get a look in, people looking at the PAQ can find this answer, and (coincidentally) I can get a couple more points :-)


0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now