Solved

[Q] TComboBox & ListHandle

Posted on 1998-07-31
9
345 Views
Last Modified: 2010-04-04
Hello there,
 
I'm trying to get handle of the listbox portion of combobox control...
I'm expecting ListHandle property to be a correct thing but it always
returns 0 no matter what style my combobox is.
I tried GetWindow() with various values of relationship parameter. But I
could not get it to work properly anyway. Anybody can help?
 
Thank you!
0
Comment
Question by:_art_
9 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 1359290
Hey, How are ya?

I'm not exactly sure what you are looking for? Would exaplin a little more, what handle do you need...???? :)

Regards,
Viktor Ivanov
0
 
LVL 4

Expert Comment

by:BoRiS
ID: 1359291
_art_

here is a snippet not very neat and quite untidy, but t was written very quickly...

This will retrieve the handle of a child of the combobox which is the listhandle as far as I understand it...Correct me if I'm wrong

procedure TForm1.Button1Click(Sender: TObject);
var
Lst: HWND;
 Combo: TCustomComboBox;
begin
  Combo := TCustomComboBox.Create(Form1);
   Combo.Parent := Self;
    Combo.Left := 12;
     Combo.Top := 10;
      Combo.Width := 100;
       Combo.Height := 25;
         Lst := GetWindow(Combo.Handle, GW_CHILD);
       if Lst <= 0 then
        Showmessage('No handle caught')
        else
         Showmessage('You have the handle');

end;

Later
BoRiS
0
 

Author Comment

by:_art_
ID: 1359292
viktornet: doin' fine. check out my new Delphi site at
www.torry.ru/dpfl/ (other are welcome as well).
BoRiS: Yep, thats exactly how Inprise catches ListHandle
and it is ALWAYS 0 ("No handle caught")!!
viktornet: Once again I need handle of the dropped list that
is part of combobox control and is listbox itself.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1359293
I think what Boris told ya is right...

GetWindow(Combo.Handle, GW_CHILD);

Regards,
Viktor Ivanov
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:_art_
ID: 1359294
yep except that it always returns 0. :-)
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1359295
It's not that...did you try his example, when the combo is created it returns other than zero...and shows you the message that the handle is really there..Anyway, I'm not sure what's going on with that ... :)

Regards,
Viktor Ivanov
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1359296
Hi everybody...
I don't think it's useful to get the handle of the list attached to the combo. If you want to manipulate the items and stuff, you better use some combo-related-smAPI.

Look at Win32 help on value "Combo Box Reference" and so on.
Search for messages like CB_ADDSTRING, CB_GETCURSEL and so on. All you want to do with the combo-list you can do through CB_ messages.
0
 
LVL 4

Accepted Solution

by:
erajoj earned 100 total points
ID: 1359297
Hi,
You're in for a surprise...
The combo listbox is NOT owned by the combobox but by the desktop unless "Style := csSimple;"! :-(
That's the reason you can't retrieve a handle for it as BoRiS suggested. The only other child a combobox has is the editbox in styles; csDropDown, csSimple.
Because the Combobox List window is not a child of the ComboBox window, there is no simple way to get its handle. Furthermore, because the Combo's list box is a private class registered by USER its class cannot be accessed either.

A way to get the handle is to replace the combobox subclass proc.
Here's some code on how to do it (in C++, mea culpa):

LRESULT CALLBACK NewComboProc (HWND hWnd,   UINT message,   WPARAM
   wParam, LPARAM lParam ); // prototype for the combo box subclass proc

HANDLE hInst;                     // Current app instance
BOOL bFirst;                      // a flag

// Dialog procedure for the dialog containing the combo box.

BOOL __export CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam,
            LPARAM lParam)
{
    FARPROC lpfnNewComboProc;

    switch (message)
    {
    case WM_INITDIALOG:

   bFirst = TRUE;       // set flag here - see below for usage

   // subclass the combo box

   lpfnOldComboProc = (FARPROC ) SetWindowLong (
               GetDlgItem ( hDlg, IDC_COMBO1 ),
               GWL_WNDPROC,
               (LONG)NewComboProc );
   break;

     case WM_DESTROY:
   (FARPROC ) SetWindowLong (    GetDlgItem ( hDlg, IDC_COMBO1 ),
               GWL_WNDPROC,
               (LONG)lpfnOldComboProc );
   break;
     default:
        break;
     }

     return FALSE;

} // end dialog proc



// Combobox subclass proc.

LRESULT CALLBACK NewComboProc (HWND hWnd,   UINT message,   WPARAM
            wParam, LPARAM lParam );

{
   static HWND hwndList;
   static RECT rectList;

#ifdef WIN16
   if (  WM_CTLCOLOR  == message)  // combo controls are to be painted.
#else
   if ( WM_CTLCOLORLISTBOX == message ) // 32 bits has new message.
#endif
   {
      // is this message for the list box control in the combo?
#ifdef WIN16
      if ( CTLCOLOR_LISTBOX==HIWORD (lParam )   ) // need only for 16 bits
      {
#endif
         // Do only the very first time, get the list
         // box handle and the list box rectangle.
         // Note the use of GetWindowRect, as the parent
         // of the list box is the desktop window

         if ( bFirst )
         {
#ifdef WIN16
            hwndList = LOWORD (lParam );
#else
            hwndList = (HWND) lParam ;     // HWND is 32 bits
#endif
            GetWindowRect ( hwndList, &rectList );
                       bFirst = FALSE;
         }

         // Resize listbox window cx by 50 ( use your size here )

         MoveWindow ( hwndList, rectList.left, rectList.top,
         ( rectList.right - rectList.left + 50 ),
         rectList.bottom - rectList.top, TRUE );
#ifdef WIN16
      }
#endif
   }

   // Call original combo box procedure to handle other combo messages.

   return CallWindowProc ( lpfnOldComboProc, hWnd, message,
wParam, lParam ); }

Hope this helps.

/// John
0
 

Author Comment

by:_art_
ID: 1359298
erajoj: Excellent! :-)

I really am surprised because actually even after subclassing the control that receiving messages is still ComboBox. :-)
Anyhow my problem is solved and everything works great. By the way in Delphi it's easier than in code you provided. I had not subclass ComboBox. It was enough to override WndProc() method which declared in TCustomComboBox and then:

procedure TSomeComboBox.WndProc(var Message: TMessage);
begin
  inherited WndProc(Message);

  try
    case Message.Msg of
      WM_CTLCOLORLISTBOX:
          HListBox := Message.lParam;
    end;
  except
    Application.HandleException(Self);
  end;
end;

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

23 Experts available now in Live!

Get 1:1 Help Now