Recent

Author Topic: [SOLVED]How to link TImage mouseWheel event to ComboBox Handler  (Read 3280 times)

sapper

  • New Member
  • *
  • Posts: 35
I have a TBitmap, dimensions (w,h) of (256, (256*n)) where n >=1.

On my form I have a 256 *256 TImage into which I CopyRect 256 by 256 Rects from the TBitmap.

I have a ComboBox with items 1..n.

As the user selects each item of the ComboBox the corresponding part of the TBitmap is displayed in the TImage.

If the MouseWheel is turned when the mouse pointer is over the ComboBox, the ComboBox items are iterated through and the images are "scrolled" through too via the ComboBox's onChange handler..

When the mouse pointer is over the TImage and the WheelMouse is turned I want the images to be "scrolled" through.

Is there a way to link the TImage's MouseWheel event to the ComboBox's default MouseWheel handler or to make the ComboBox iterate through its items the same as it does when using the MouseWheel over it?

Due to limited space on the form I don't want to put the TImage in a scrollbox.

The program is for Windows.

« Last Edit: April 21, 2014, 04:22:31 am by sapper »

karaba

  • New Member
  • *
  • Posts: 49
Re: How to link TImage mouseWheel event to ComboBox Handler
« Reply #1 on: April 20, 2014, 03:25:15 am »
sure use the mouse wheel on the image to set the combobox1.ItemIndex to the one that the scroll process would select, you just have to track how combobox traslates the sizes to lines and use the same interval/threshold.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to link TImage mouseWheel event to ComboBox Handler
« Reply #2 on: April 20, 2014, 05:33:18 am »
The sign of WheelDelta gives you the direction of the wheel movement. When using the OnMouseWheel event of the image. The following code should give you identical results to the combo box:
Code: [Select]
procedure TForm1.Image1MouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
var
  i: integer;
begin
  with ComboBox1 do  //<---- Replace this with your ComboBox
  begin
    //Worth it?
    if Items.Count=0 then exit;

    i := ItemIndex;

    //Change according to direction
    if (WheelDelta>0) then
      i := i - 1
    else
      i := i + 1;

    //Check range
    if i < 0 then
      i := 0
    else if i >= Items.Count then
      i := Items.Count - 1;

    //Any change?
    if i <> ItemIndex then
    begin
      //Apply it
      ItemIndex := i;

      //Fire OnChange
      if assigned(OnChange) then
        OnChange(Sender);
    end;
  end;
end;

sapper

  • New Member
  • *
  • Posts: 35
Re: How to link TImage mouseWheel event to ComboBox Handler
« Reply #3 on: April 21, 2014, 04:22:06 am »
Thanks. Works exactly how I wanted.

 

TinyPortal © 2005-2018