Solved

SetFocus on grid on Form Mouse Over/Hover?

Posted on 2013-07-01
9
498 Views
Last Modified: 2013-08-08
Now that most of our users are switching to multiple monitors, they are switching back and forth between my grids and their other applications. I would like the grid to be focused when the mouse hovers on the form, so when the user starts scrolling their mouse wheel, it is already focused on the grid, no need to click first.  Any ideas?

Thanks!
0
Comment
Question by:Bianca
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39291696
Which grid are you speaking about: a StringGrid? A DBGrid?
Have you tried DBGrid1.SetFocus in OnMouseOver event?
0
 

Author Comment

by:Bianca
ID: 39291719
It is a TcxDBGrid and I tried the OnMouseOver event on the grid and it causes other issues when user clicks in the cell to edit as some fields are editable in the grid. Exception about no scrollbars found. Basically I want the grid 'clicked' to gain control OnMouseOver. Thanks!
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 39292438
Try using Form.OnActivate and write your grid.setfocus there
This way any time your form become active the focus goes to your grid
0
 

Author Comment

by:Bianca
ID: 39293431
the focus goes to the grid only AFTER I click on the form to activate it.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 39293590
I don't know if I've exactly understood what you need, anyway if you want to trigger an OnMouseOver for the Grid without using itself event, you can do as follows:

drop a TApplicationEvent on your form
In its OnIdle event check if the control under the mouse is your grid and if so there you can do what you want with it (set the focus, scroll, update etc, that is you can access any published property or function of the control).

You can do this for any control, not just for your grids...

here an example on the fly

procedure TForm1.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
var
  ctrl: TWinControl;
begin
  ctrl := FindVCLWindow(Mouse.CursorPos);
  if ctrl <> nil then
  begin
    if ctrl is TcxDBGrid then
    begin
      if not(TcxDBGrid(ctrl).Focused) then
        TcxDBGrid(ctrl).SetFocus;
    end;
  end;
end;

Open in new window

0
 

Author Comment

by:Bianca
ID: 39293742
I have been able to 'set focus' to the grid using my previous OnMouseEnter event of the grid so I can scroll on mouseover BUT if I have clicked on any other window, setting focus to the grid does not work. It sets focus on the record, I can see from the highlighting bar, but the form itself is not 'focused' and the caption is 'Inactive Window' . I cannot scroll until I click on either the toolbar, caption, etc.
0
 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 39296916
You want to use mouse wheel without you click/activate form first?
0
 

Author Comment

by:Bianca
ID: 39308847
YES. Exactly. I want to mouseover the grid and scroll again (the focus is already on the record) but it does not work until AFTER i click on the toolbar, form, etc. somewhere on the form first.
0
 
LVL 26

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 39313679
you can go with global mouse hook and catch if mouse is over you grid:
http://delphi.about.com/od/windowsshellapi/a/mousehook.htm

but this might work too...add TAppliacationEvents component and add event OnMessage.
...
//add this line to form class
procedure WMMouseWheel(var Message: TWMMouseWheel); message WM_MOUSEWHEEL; 
procedure DoScroll(ds: TDataSource; bScrollDown: Boolean);
...
procedure TForm1.WMMouseWheel(var Message: TWMMouseWheel);
var
  ctrl: TWinControl;
begin
  ctrl := FindVCLWindow(Point(Message.XPos, Message.YPos));
  if (ctrl <> nil) and (ctrl is TcxDBGrid) then 
  begin
    DoScroll(cxDBGrid1.DataSource, Message.WheelDelta<0);
    Message.Result:=0;
  end;
end;

//this procedure is from
//http://www.experts-exchange.com/Programming/System/Windows__Programming/MFC/Q_23731495.html
procedure MySetForegroundWindow(WinHandle:DWORD);
var
  pid, fgThread, myThread : DWORD;
begin;
  fgThread := GetWindowThreadProcessId(GetForegroundWindow, pid);
  myThread := GetCurrentThreadId;

  if (fgThread <> myThread) then
  begin;
    if AttachThreadInput(fgThread, myThread, true) then
    begin;
      BringWindowToTop(WinHandle);
      SetForeGroundWindow(WinHandle);
      AttachThreadInput(fgThread, myThread, false);
    end;
  end
  else
  begin;
    BringWindowToTop(WinHandle);
  end;
  InvalidateRect(WinHandle, nil, TRUE);
end;

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
  var Handled: Boolean);
var
  iWheelDelta: SmallInt;
begin
  if (Msg.hwnd = cxDBGrid1.Handle) then
  begin
    case Msg.message of
      WM_MOUSEMOVE:
        begin
          if not Self.Active then
            MySetForegroundWindow(Self.Handle);
        end;
      WM_MOUSEWHEEL:
        begin
          if not cxDBGrid1.Focused then
          begin
            DoScroll(cxDBGrid1.DataSource, HiWord(Msg.wParam)<0);
            Handled := True;
          end;
        end;
    end;
  end;
end;

procedure TForm1.DoScroll(ds: TDataSource; bScrollDown: Boolean);
begin
  if Assigned(ds) and Assigned(ds.Dataset) then
  begin
    if (ds.DataSet.Active) and (ds.DataSet.State=dsBrowse) then
    begin
      if bScrollDown then
      begin
        if (not ds.DataSet.Eof) then ds.DataSet.Next;
      end
      else
      begin
        if (not ds.DataSet.Bof) then ds.DataSet.Prior;
      end;
    end;
  end;
end;

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi 10 Seattle Dataset Actions 5 79
How to use Hashing sha1 in Delphi 2010 4 206
Delphi selector screen 2 70
TEMBEDDEDWB how can i change its user agent ? 8 50
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

21 Experts available now in Live!

Get 1:1 Help Now