Fixing form on the screen

  • Thread starter Thread starter Patrick De Ridder
  • Start date Start date
P

Patrick De Ridder

If I remove the top band from the form, the form will be fixed on the
screen (you cannot move the form about on the screen). Is there a way to
stop the form from being moved about, whilst retaining the band at the top
of the form?

Thank you.
Patrick.
 
Hi Patrick,
The easiest way, I believe, is to override WndProc and trap WM_NCHITTEST
message. If the hit happens to fall over the caption just fool the windows
that it is somwhere else (or nowhere). This will stop the form from moving
by the mouse. That was easy. Not so easy, though, is to prevent using Move
command from the system menu. The best is to disable that menu item on the
system menu, but this involves dealing with P\Invoke and Win32 API. The
easiest is to swallow SC_MOVE in the WndProc.
So the code for WndProc, I would suggest, is as follows

private int WM_NCHITTEST = 0x0084;
private int HTCAPTION = 2;
private int HTNOWHERE = 0;
private int WM_SYSCOMMAND = 0x0112;
private int SC_MOVE = 0xF010;
protected override void WndProc(ref Message m)
{
if(m.Msg == WM_NCHITTEST)
{
base.WndProc(ref m);
if((int)m.Result == HTCAPTION) m.Result = (IntPtr)HTNOWHERE;
return;
}
else if(m.Msg == WM_SYSCOMMAND && (m.WParam.ToInt32() & 0xFFF0) ==
SC_MOVE)
{
//Swallow the move request from the system menu
m.Result = IntPtr.Zero;
return;
}

base.WndProc (ref m);
}

Now the form is pinned.
 
Stoitcho Goutsev (100) said:
Hi Patrick,
The easiest way, I believe, is to override WndProc and trap WM_NCHITTEST
...
So the code for WndProc, I would suggest, is as follows

<snipped>

This is truly fantastic.
This is going to make all the difference to my application.
Thank you very much!

Patrick.
 
I'm glad it works for you :-)

--
B\rgds
100

Patrick De Ridder said:
This is truly fantastic.
This is going to make all the difference to my application.
Thank you very much!

Patrick.
 
Back
Top