Solved

Dragging a window without using the title bar.

Posted on 1998-11-17
13
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

690 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