Link to home
Avatar of b_vaneck
b_vaneck

asked on

Titlebar height / selecting an item in TCheckListBox

Hi,

I have a checklistbox in which you can either click on the check next to an item so select it, or you just click on the item and it will select the item clicked too. This all works with retrieving the current position of the mouse and then calculating which item was clicked. This involves knowing the height of the titlebar and menubar.

In Win95/NT/98/2000 this was very easy, just a fixed height of 44 pixels for the height of the titlebar and menubar. But in WinXP the titlebar is just 4 pixels higher. So now in some cases it will check the item below the item I want to check....

Here's the code:

procedure TMainFrm.opdrSondChkLbxClickCheck(Sender: TObject);
var
  mousept : TPoint;
  index : Integer;
const
  leftMargin : Integer = 5;
  heightMenuAndTopBar : Integer = 44;
begin
  mousept := Mouse.CursorPos;
  //get the right x en y coordinates of the selected item
  mousept.x := mousept.x - MainFrm.Left - PageControl1.Left - opdrachtSht.Left - GroupBox1.Left - opdrSondChkLbx.Left - leftMargin;
  mousept.y := mousept.y - MainFrm.Top - heightMenuAndTopBar - PageControl1.Top - opdrachtSht.Top - GroupBox1.Top - opdrSondChkLbx.Top;
  //index will be the selected item index in the checklistbox
  index := opdrSondChkLbx.ItemAtPos(mousept, false);
....
....
end;

Am I thinking to diffucult? Can this be done easier? Or is there a way to retrieve the height of the titlbar and menubar?

Thankx,

Benjamin
Avatar of PreachDotNet
PreachDotNet

Im not sure which application you are using...
In .net I would use the TMainfrm.clientrectangle.top property
Can you not use the property of an object above the checkboxes?
Avatar of b_vaneck

ASKER

The programme language is Delphi.

These 2 lines determine where the mouse is by substracting the .top property from the current mouse position:
mousept.x := mousept.x - MainFrm.Left - PageControl1.Left - opdrachtSht.Left - GroupBox1.Left - opdrSondChkLbx.Left - leftMargin;
  mousept.y := mousept.y - MainFrm.Top - heightMenuAndTopBar - PageControl1.Top - opdrachtSht.Top - GroupBox1.Top - opdrSondChkLbx.Top;

So already tried that option.... Isn't there any property in Windows where the height of the titlebar is kept?

Thankx again,

Benjamin
CyCaption     := GetSystemMetrics(SM_CYCAPTION);

 taken from

http://www.delphicorner.f9.co.uk/articles/forms5.htm
hey b_vanek there's a much MUCH easier way to go about it.

use the onClick event for your checkListBox:

procedure TForm1.CheckListBox1Click(Sender: TObject);
begin
        checkListBox1.Checked[CheckListBox1.ItemIndex]:=true;
end;




and that's it! no calculating which item was found under the mouse ;-)

fidel.
i was looking for the ctrl+shift+c
works like a charm, but is there a way to send it to the public section instead of the private section
whoops, please read the message before the one above!
ASKER CERTIFIED SOLUTION
Avatar of ZhaawZ
ZhaawZ
Flag of Latvia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Hey ZhaawZ,

thanks for the great tip! It really is alot easier this way, as I asked in the beginning. Thought it could be done much easier. So giving you all the points.
PreachDotNet: couldn't get GetSystemMetrics(SM_CYCAPTION); to work. Maybe it is the titlebar's height. But instead of the calculated 44 that I first had, it turned out to be 26 pixels....

Thankx very much,

Benjamin