Recent

Author Topic: [solved] MouseWheel Events in other Components  (Read 4182 times)

the3dcloser_

  • Newbie
  • Posts: 3
[solved] MouseWheel Events in other Components
« on: February 11, 2015, 08:09:00 pm »
Hello Community,
I have a Problem with calling the MouseWheelDown/MouseWheelUp Event of a Form.

I'm working  with OpenGL so I want to implement a Zoom-function while using the MouseWheel. For OpenGL, I use the Handle of a Panel, but it doesn't provide the MouseWheel Events. So I tried to use the Events of the Form, where the Panel is, but is is not working. It works if the Pointer of the Mouse is directly on the Form, but it does not if the Mouse is over the Panel (or any other component)

I was even trying a simple code like shown below, but it does still not work:

Code: [Select]
procedure TMainform.FormMouseWheelDown(Sender: TObject; Shift: TShiftState;
  MousePos: TPoint; var Handled: Boolean);
begin
  if (MousePos.X>192) and ((MousePos.Y>0) and (MousePos.Y<480)) then  //check for Panel range
   ShowMessage('MouseWheelDown');
end;

Can somebody help me or explain me my mistake?
« Last Edit: February 14, 2015, 10:17:12 pm by the3dcloser_ »

howardpc

  • Hero Member
  • *****
  • Posts: 4144

balazsszekely

  • Guest
Re: MouseWheel Events in other Components
« Reply #2 on: February 11, 2015, 08:55:13 pm »
You can subclass TPanel and republish those events. The only drawback is you have to create the panel dynamically. If you want the panel to be available at design time make a component.

Code: [Select]
type
 //...
  TForm1 = class(TForm) 
    procedure FormCreate(Sender: TObject);
  private
    procedure MouseWheelDown(Sender: TObject; Shift: TShiftState;
      MousePos: TPoint; var Handled: Boolean);
    procedure MouseWheelUp(Sender: TObject; Shift: TShiftState;
      MousePos: TPoint; var Handled: Boolean);
  public
  end;

type
  TPanelEx = class(TPanel)
  published
    property OnMouseWheelUp;
    property OnMouseWheelDown;
  end;

var
  Form1: TForm1;
  PanelEx: TPanelEx;

implementation

{$R *.lfm}

{ TForm1 }


procedure TForm1.FormCreate(Sender: TObject);
begin
  PanelEx := TPanelEx.Create(Form1);
  with PanelEx do
  begin
    Name := 'Panel1';
    Parent := Form1;
    Left := 136;
    Top := 64;
    Width := 297;
    Height := 169;
    TabOrder := 0;
    OnMouseWheelDown := @MouseWheelDown;
    OnMouseWheelUp := @MouseWheelUp;
  end;
end;

procedure TForm1.MouseWheelDown(Sender: TObject; Shift: TShiftState;
  MousePos: TPoint; var Handled: Boolean);
begin
  PanelEx.Caption := IntToStr(MousePos.x) + '    ' + IntToStr(MousePos.y);
end;

procedure TForm1.MouseWheelUp(Sender: TObject; Shift: TShiftState;
  MousePos: TPoint; var Handled: Boolean);
begin
  PanelEx.Caption := IntToStr(MousePos.x) + '    ' + IntToStr(MousePos.y);
end;

                                             

the3dcloser_

  • Newbie
  • Posts: 3
Re: MouseWheel Events in other Components
« Reply #3 on: February 11, 2015, 09:54:26 pm »
The Sub-Class sounds good for me, I don't care whether the Panel is created dynamically or by design time.

But when I try your solution, it gives me the following Error in these lines:

unit1.pas(279,22) Error: Variable identifier expected

In:

Code: [Select]
OnMouseWheelDown:=@MouseWheelDown;
OnMouseWheelUp:=@MouseWheelUp;



EDIT: It works fine for me if I copy your code in a new project, but the Error is still appearing when I try to do it in my Project, I'll edit it once again after a deep code-cleaning....

EDIT²: I rewrote some parts of my Project and I found out, that there must be a Problem with my uses-List. There is an error with the HDC I use for OpenGL. It appears if I add the Windows-unit to my code (which I need for the HDC), but then the Error shown below appears.

Code: [Select]
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  Menus, StdCtrls, ExtCtrls, Buttons, Spin, ComCtrls, LoadSave, dglOpenGL,
  Windows, ExportObj, unit_options, types, stuff, ptx_texture;
« Last Edit: February 11, 2015, 10:54:06 pm by the3dcloser_ »

the3dcloser_

  • Newbie
  • Posts: 3
Re: MouseWheel Events in other Components
« Reply #4 on: February 14, 2015, 10:15:20 pm »
Finally, it is working.

The problem, that those two lines are creating an error was easy to solve - I simply need to remove the "@" in front of those two lines.

So this works perfectly:

Code: [Select]
OnMouseWheelDown:=MouseWheelDown;
OnMouseWheelUp:=MouseWheelUp;


 

TinyPortal © 2005-2018