[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Moving a borderless window

Posted on 2006-06-06
4
Medium Priority
?
312 Views
Last Modified: 2010-04-05
Hi

I have a window creted with:

procedure TOknoAbout.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  with Params do
    Style := (Style or WS_POPUP) and (not WS_DLGFRAME);
end;

and a window move procedures that take care of it:

procedure TOknoAbout.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  myszX := X;
  myszY := Y;
end;

procedure TOknoAbout.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  if shift = [ssLeft] then
    begin
      OknoAbout.Left := OknoAbout.Left + (X - myszX);
      OknoAbout.Top := OknoAbout.Top + (Y - myszY);
    end;
end;


how to modify the formmousemove procedure for it not to let the windows get out of the screen.workarea ??

thanks for any advice
0
Comment
Question by:abrakadabra1
  • 2
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 560 total points
ID: 16840923
var
    newleft, newtop: integer;
begin
  if shift = [ssLeft] then
    begin    
      newleft := OknoAbout.Left + (X - myszX);
      newtop := OknoAbout.Top + (Y - myszY);
      if (newleft >=0) and ( (newleft + OknoAbout.Width) < screen.width) then
        OknoAbout.Left := newleft;
      if (newtop >=0) and ( (newtop + OknoAbout.Height) < screen.Height) then
        OknoAbout.Top := newtop;
    end;
end;
0
 

Author Comment

by:abrakadabra1
ID: 16840999
thank you for your help
0
 

Author Comment

by:abrakadabra1
ID: 16841223
@TheRealLoki

i have a problem with this. the answer that you gave wasn'e working if the mouse was moved to fat to the screen side. i've modified the procedure to this:

procedure TOknoAbout.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
  NewTop, NewLeft : integer;

begin
  if shift = [ssLeft] then
    begin
      NewLeft := OknoAbout.Left + (X - myszX);
      NewTop := OknoAbout.Top + (Y - myszY);
      if ((NewLeft >= 0) and ((NewLeft + OknoAbout.Width) < Screen.WorkAreaWidth)) then
        OknoAbout.Left := NewLeft;
      if ((NewTop >= 0) and ((NewTop + OknoAbout.Height) < Screen.WorkAreaHeight)) then
        OknoAbout.Top := NewTop;
      if (X < 10) then
        OknoAbout.Left := 0;
      if (Y < 10) then
        OknoAbout.Top := 0;
      if (X > (Screen.WorkAreaWidth - 10)) then
        OknoAbout.Left := Screen.WorkAreaWidth - OknoAbout.Width;
      if (Y > (Screen.WorkAreaHeight - 10)) then
        OknoAbout.Top := Screen.WorkAreaHeight - OknoAbout.Height;
    end;
end;

and now the top and left screen side work ok but there is some kind of a problem with this part of the code:

      if (X > (Screen.WorkAreaWidth - 10)) then
        OknoAbout.Left := Screen.WorkAreaWidth - OknoAbout.Width;
      if (Y > (Screen.WorkAreaHeight - 10)) then
        OknoAbout.Top := Screen.WorkAreaHeight - OknoAbout.Height;

now if the mouse is moved to fast to the right or bottom side of the screen the window still stays far on the screen... http://spisik.szm.sk/img.jpg ... any idea how to correct this ??
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 16846480
hmmm, not sure what is happening.
I've simplified the procedure a bit, maybe this will help

uses math;

procedure TOknoAbout.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  myszX := X;
  myszY := Y;
end;

procedure TOknoAbout.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  NewTop, NewLeft : integer;
begin
  if shift = [ssLeft] then
    begin
      NewLeft := OknoAbout.Left + (X - myszX);
      NewTop := OknoAbout.Top + (Y - myszY);

      NewLeft := Max(0, NewLeft);
      NewLeft := Min(Screen.WorkAreaWidth - OknoAbout.Width, NewLeft);
      NewTop := Max(0, NewTop);
      NewTop := Min(Screen.WorkAreaHeight - OknoAbout.Height, NewTop);

      OknoAbout.Left := NewLeft;
      OknoAbout.Top := NewTop;
    end;
end;


btw, another way to move the form is to make it act as a titlebar, eg.
pubic
    procedure WMNCHitTest(var M: TWMNCHitTest); message wm_NCHitTest;

procedure TOknoAbout.WMNCHitTest(var M: TWMNCHitTest);
begin
  inherited;                    { call the inherited message handler }
  if  M.Result = htClient then  { is the click in the client area?   }
    M.Result := htCaption;      { if so, make Windows think it's     }
                                { on the caption bar.                }
end;
just doing the above code, you can move the form around now.

and you can check the position being moved to with
    procedure WMPosChange(var Message: TWMWINDOWPOSCHANGING); message WM_WINDOWPOSCHANGING;
Message.WindowPos.x and Message.WindowPos.y, although I had trouble checking the left bounds, so didn't look into it.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Integration Management Part 2
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

868 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