Solved

Dragging a window without using the title bar.

Posted on 1998-11-17
13
297 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
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 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

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
Simple Delphi Question 9 83
Internet Explorer View Settings Question 15 107
Send message from delphi to whatsapp, is it possible? 3 1,185
Using idhttp to login to instagram 2 76
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

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

16 Experts available now in Live!

Get 1:1 Help Now