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
Solved

Dragging a window without using the title bar.

Posted on 1998-11-17
13
300 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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