This message was edited by zibadian at 2004-9-10 3:35:45
: : : 1. "This solution might cause problems, if you want to use the mousewheel for other things, when the grid doesn't have the focus. "
: : : I check if the dbgrid is focused at the beginning of the event handler. it works.
: : :
: : : 2. "it might slow down the program." What can cause slowing down? I really dont have any idea.
: : :
: : : 3. I tried prior and next methods before. But it didnt scroll one by one. I think it was because of the mouse configuration (Set in Control Panel).
: : :
: : :
: : :
: : 1. I included this as a note, because the posted code didn't check it.
: : 2. The OnMessage() is called for every message the entire application gets, which are 100's per second (if not more). A single if-then is at least a single operation and always has some memory latency (http://encyclopedia.thefreedictionary.com/Latency) accompanied with it. This is in this case not noticable, but if you put several if-then's in the OnMessage(), then it might become noticable.
: : 3. The mouse settings have probable nothing to do with it, because the OnMouseWheel() is called only once per click. This means that the Prior or Next is called only once per click, which should scroll the grid one by one for each click. But perhaps the database handling components work different than I thought (I never use them myself).
: :
:
: My Ideal would be, that why dont we extend the capability of the DBGrid, with deriving a new component from TDBGrid, override the windwos wndProc, process the message, and do the proper actions.
: I wrote a component like this before. This component manages the scrolling and adds a new event 'OnMouseWheel' for futher processing if its nesessary.
:
: Here I post the code:
:
:
: unit WheelGrid;
:
: interface
:
: uses
: SysUtils, Classes, Controls, Grids, DBGrids, Messages, dialogs, Windows;
: type TmwState = (mwUp,mwDown); // mousewheel direction status
: type TMouseWheel = procedure(Sender : TObject;WheelDirection:TMwState) of object; // new event
: type
: TWheelGrid = class(TDBGrid)
: private
: FOnMouseWheel: TMouseWheel;
: procedure SetOnMouseWheel(const Value: TMouseWheel);
: { Private declarations }
: protected
: { Protected declarations }
: public
: procedure WndProc(var Message: TMessage);override; //overriding the wndproc of the grid
: { Public declarations }
: published
: property OnMouseWheel : TMouseWheel read FOnMouseWheel write SetOnMouseWheel; // add the new property
: { Published declarations }
: end;
:
: procedure Register;
:
: implementation
:
: procedure Register;
: begin
: RegisterComponents('OSoft', [TWheelGrid]);
: end;
:
: { TWheelGrid }
:
: procedure TWheelGrid.SetOnMouseWheel(const Value: TMouseWheel);
: begin
: FOnMouseWheel := Value;
: end;
:
: procedure TWheelGrid.WndProc(var Message: TMessage);
: var
: WData : SmallInt; //wheel data
: begin
:
: if Message.Msg = WM_MOUSEWHEEL then //it the windows message is mousewheen (NT ONLY!)
: begin
: WData := HiWord(Message.WParam); //The HI part of the data gives the wheel movement multipled by mouse delta (120 usually)
: // it lower then 0 then wheel up, else wheel down
: Message.Msg := WM_KEYDOWN; //change the message to WM_KEYDOWN
: Message.LParam := 0; //reset lparam
: if WDAta>0 then // if wheel up
: begin
: if Assigned(FOnMouseWheel) then FOnMouseWheel(self,mwUp); //call the event handler
: Message.WParam := VK_UP; // new windows message parameter is VK_UP
: end
: else
: begin
: if Assigned(FOnMouseWheel) then FOnMouseWheel(self,mwDown); //call the event handler
: Message.WParam := VK_DOWN; // new windows message parameter is VK_DOWN
: end;
: end;
: inherited WndProc(Message); // inherit whith the original, or cahnged windows message
: end;
:
: end.
:
:
: I prefer writing components in this case, cos' this way it is reusable.
: In the components OnMouseWheel event you can check the direction like:
:
: procedure TForm1.WheelGrid1MouseWheel(Sender: TObject;
: WheelDirection: TmwState);
: begin
: if WheelDirection = mwUP then
: ShowMessage('UP')
: else
: ShowMessage('DOWN');
: end;
:
:
: This way, the question of time latencies, wont matter, cos' the component windproc invoked only, if a message is sent to.
: Will not effect the speed of your entire application.
: SoftMan
:
:
I dislike creating new components, because the source code is not very portable to other computers. For my work I never know if I'm working on the same machine again, and thus never know if my components are already installed. As for the speed, that's correct.