Solved

Dragging a window without using the title bar.

Posted on 1998-11-17
13
296 Views
Last Modified: 2010-04-04
Dragging a window without using the title bar is easy enough, BUT it's not quite that simple.
I have a form that contains only a TGuage and a TPopupMenu.  The popup menu is linked to the TGauge and modifies certain parameters of the gauge (font, type, etc) and this works fine when you right click on the gauge, but if I use the code below to allow dragging of the form by clicking anywhere, the popup menu stops working.

procedure TForm1.WMNCHitTest( var Msg : TWMNCHitTest );
              begin
                inherited;
                if htClient = Msg.Result
                then
                  Msg.Result := htCaption;
              end;

Can anyone tell me how to have both the popup menu and being able to drag the window by clicking on the gauge?  I've tried everything I can think of.... thanks in advance.
0
Comment
Question by:delphiexpert
  • 5
  • 2
  • 2
  • +4
13 Comments
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1347177
You may have to add a DouseDown event to the Gauge control and then manually show the PopUp menu (PopupMenu1.PopUp(X, Y).  That way you can still drag the form with the left mouse button, and get the PopUp menu working.

Hope this helps.

Stu.
0
 
LVL 1

Author Comment

by:delphiexpert
ID: 1347178
Unfortunately it doesn't.  The code I gave above makes windows think that the user is always clicking on the title bar, not the gauge and so the popup code never gets called.  I tried setting a flag whenever the mousedown event is called that contains the mouse button, and checking that flag in the WMNCHitTest procedure to see if the right mouse button was used... but this didn't work either.  Any further suggestions?  Thanks.

0
 
LVL 4

Expert Comment

by:dwwang
ID: 1347179
Isn't the popupmenu activeted by rightmouse click?
If so, why not just filter the rightclick in the above procedure?
0
 
LVL 4

Expert Comment

by:BoRiS
ID: 1347180
delphiexpert

what you can maybe do is pass this procedure and then call it

procedure WMRButtonDown(var M: TWMRButtonDown); message WM_RBUTTONDOWN; //under the public heading

then call

procedure TForm1.WMRButtonDown(var M: TWMRButtonDown);
var
Pt: Tpoint;
begin
 inherited;
  GetCursorPos(Pt);
  if M.Result = htClient then
  PopupMenu1.Popup(pt.x, pt.y);
end;

Havn't tested, so I don't know if it will work but you can try...

Later
BoRiS
0
 
LVL 10

Expert Comment

by:Lischke
ID: 1347181
What you have to do is to determine whether a control is under the mouse while NCHITTEST:

procedure TForm1.WMNCHitTest( var Msg : TWMNCHitTest );
begin
  if ControlAtPos(ScreenToClient(SmallPointToPoint(Msg.Pos)), False) <> nil then inherited
    else Msg.Result := htCaption;
end;

In this case mouse handling works as normal if the mouse is over a child control, while you can drag the form in any other case.

Ciao, Mike
0
 
LVL 1

Author Comment

by:delphiexpert
ID: 1347182
Lischke,
the problem with the answer you gave is - the control takes up the whole form; its Align property is set to client.  Basically, what I want is this:
if the left mouse button is clicked, drag the form
if the right mouse button is clicked, bring up the menu.
It doesn't matter where on the form the mouse is, the above actions need to happen whereever it is.

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:delphiexpert
ID: 1347183
BoRiS,

thanks for your idea, but that didn't work either.  The procedure didnt ever get called, even when the right mouse button was down.
0
 
LVL 4

Expert Comment

by:dwwang
ID: 1347184
May I know how do you send the message TWMNCHitTest to the form?
0
 

Expert Comment

by:tothpaul
ID: 1347185
It is probably a TGauge bug, with a TPanel, your code work fine.

just put the TGauge on a TPanel (align=alclient) and link then PopupMenu to the Panel !
0
 
LVL 3

Expert Comment

by:bryan7
ID: 1347186
try this:

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
if ssleft in shift then
   begin
   releasecapture; //restore mouse input to prevent from moving too far
   form1.perform(WM_syscommand, $F012, 0);
   end;
end;

bryan
0
 
LVL 1

Author Comment

by:delphiexpert
ID: 1347187
Unfortunately that doesn't work either, when I put the Tgauge on a Tpanel, the  WMNCHitTest procedure wasn't called while the mouse was over the Tpanel (ie, the whole time really).  The menu did work though.  I can easily get the dragging, or the popup menu but not both.  perhaps I'll just put the options somewhere on the main form...
unless anyone can come up with something else.
0
 

Accepted Solution

by:
tothpaul earned 80 total points
ID: 1347188
I suppose, it's possible to change TGauge to handle (or not) mouse events...on a right click, show the popup menu, on the left click, just do nothing (Form1 will take the message)

An other way is to implemente the dragging.

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 leftclick:=(Button=mbLeft);
 cleft:=x;
 ctop:=y;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 leftclick:=false;
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
 if leftclick then begin
  setbounds(left+x-cleft,top+y-ctop,width,height);
 end;
end;

0
 
LVL 1

Author Comment

by:delphiexpert
ID: 1347189
That worked!  Thanks!

(sorry for the delay in accepting the answer, I thought I already had.. obviously it didn't go through)

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
APK file name 7 64
Thread safe  opinion 7 111
QRReport  TQrmemo vertical stretching 1 37
can't find the executable in Simulator 1 54
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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