Changes Form Wide

  • Thread starter Thread starter Stewart
  • Start date Start date
S

Stewart

2 questions

1. How do i set the whole form's opacity prop? Right now i have to set
an event handler for each element on the form. Is there are better way
to do this?

2. how do i make a form that has no border fully dragable... ie you can
click and hold any where to move it around the desktop

Thanks for any help inadvance
 
2 questions
1. How do i set the whole form's opacity prop? Right now i have to set
an event handler for each element on the form. Is there are better way
to do this?

I'm not sure I understand what you're wanting to do....you should be able to
set the Opacity property on the form, and it will effect everything.
2. how do i make a form that has no border fully dragable... ie you can
click and hold any where to move it around the desktop

Okay, here goes (see code sample below):

1. Set up two private class variables, formPosition (Point), and mouseDown
(bool)
2. Set up three event handlers for MouseDown, MouseMove, and MouseUp. See
the code below for thier contents.
3. Add the handlers above to the form, as well as any other control you want
the user to be able to click on (so labels, panels, etc).

public class Form1 : Form
{

private Point _formLocation;
private bool _mouseDown;

public Form1()
{
InitializeComponent();
}

private void OnMouseDown(object sender, MouseEventArgs e)
{
this.formLocation = new Point(e.X,e.Y);
this.Cursor - Cursors.SizeAll;
this._mouseDown = true;
}

private void OnMouseMove(object sender, MouseEventArgs e)
{
if(this._mouseDown)
{
this.Location = new Point(this.Left - (this._formLocation.Left -
e.X), this.Top - (this._formLocation.Y - e.Y));
}
}

private void OnMouseUp(object sender, MouseEventArgs e)
{
this._mouseDown = false;
this.Cursor = Cursors.Default;
}
}

Enjoy!
 
Casey said:
Okay, here goes (see code sample below):

<Code snipped>

Or you can override the WndProc method of the form. This should only
be used with captionless forms as it will interpret the clicking of the
close button [x] in the corner as moving the form. What it does is
trick Windows into thinking you have the mouse cursor

//C# code
protected override void WndProc(ref Message m)
{
const int WmNcHitTest = 0x84;
const int HtCaption = 2;

if (m.Msg == WmNcHitTest)
m.Result = new IntPtr(HtCaption);
else
base.WndProc(ref m);
}


'VB Code
Protected Overrides Sub WndProc(ByRef m As
System.Windows.Forms.Message)
Const WmNcHitTest As Integer = &H84
Const HtCaption As Integer = &H2

Debug.WriteLine(m.ToString)

If m.Msg = WmNcHitTest Then
m.Result = New IntPtr(HtCaption)
Else
MyBase.WndProc(m)
End If
End Sub


If you want to click on another control, such as a panel, and move the
form, you can use similar code:

private void Panel1_MouseDown(object sender, MouseEventArgs e)
{
const uint WM_SYSCOMMAND = 0x0112;
const uint SC_MOVE = 0xF010;
const int HtCaption = 2;

pnlAdmin.Capture = false;

SendMessage(this.Handle, WM_SYSCOMMAND, new IntPtr(SC_MOVE +
HtCaption),new IntPtr(0));
}


Hope this helps
 
Back
Top