 |
|
 |
hi, this is not working if the form is RightToLeft, any ideas???
|
|
|
|
 |
|
 |
Hello,
It has just worked perfectly!
Thank you
David D.
|
|
|
|
 |
|
 |
If you want allow to end user decide if move or not a window, you can place a check box that modified the Draggable property.
public bool Draggable
{
set
{
this.draggable = value;
if (!Draggable)
{
RemoveMoveHandlersFromBaseForm();
RemoveHandlesOnChildControls();
}
else
{
AddMoveHandlersFromBaseForm();
AddHandlesOnChildControls();
}
}
get
{
return this.draggable;
}
}
Check that events handlers has been moved into methods.
|
|
|
|
 |
|
 |
You can allows to end user decide if a window can be moved with a check box available at runtime. this check box must to change the Draggable property and you must to remove the handlers:
public bool Draggable
{
set
{
this.draggable = value;
if (!Draggable)
{
RemoveMoveHandlersFromBaseForm();
RemoveHandlesOnChildControls();
}
else
{
AddMoveHandlersFromBaseForm();
AddHandlesOnChildControls();
}
}
get
{
return this.draggable;
}
}
|
|
|
|
 |
|
 |
I have seen many valid and good ideas, however I would say some to be difficult for the beginner. If you need an easy and simple way that you can remember then check out the link on www.pro2visual.com
|
|
|
|
 |
|
 |
Hello,
Thanks for an excellent article.
I used this class in my own article
http://www.codeproject.com/useritems/LoginTime.asp
I was going to write a widget using the Javascript system that Google desktop or Yahoo has but C# is so much more powerful. You can't get the username or save data with a Javascript widget. So I found your article as used it as a starting point. Thanks!!
Two questions for you:
- i've noticed copyright headers in other codeproject source files. what is your opinion about doing that?
- exactly how did you create a resx file under each form source file. I am only able to add an item to the project and then VS does all kinds of crazy stuff when I try to rename it and move it. it creates a source file with getters. Could you please explain your process?
|
|
|
|
 |
|
 |
When I have implemented this concept, I find that the window gets painted in many of the spots it was dragged across leaving a trail. Does that make sense? Is there a way to to prevent that?
|
|
|
|
 |
|
 |
protected override void WndProc(ref Message m)
{
if (m.Msg == 163 && this.ClientRectangle.Contains(this.PointToClient(new Point(m.LParam.ToInt32()))) && m.WParam.ToInt32() == 2)
m.WParam = (IntPtr)1;
base.WndProc(ref m);
if (m.Msg == 132 && m.Result.ToInt32() == 1)
m.Result = (IntPtr)2;
}
Enjoy
aNT---
|
|
|
|
 |
|
 |
Protected Overrides Sub WndProc(ByRef m As Message)
If (((m.Msg = 163) And ClientRectangle.Contains(PointToClient(New Point(m.LParam.ToInt32)))) And (m.WParam.ToInt32 = 2)) Then
m.WParam = CType(1, IntPtr)
End If
MyBase.WndProc(m)
If ((m.Msg = 132) And (m.Result.ToInt32 = 1)) Then
m.Result = CType(2, IntPtr)
End If
End Sub
Enjoy
aNT---
|
|
|
|
 |
|
 |
suuuuuuuuuuuuuuupppppppppppppeeeeeeeeeeerb...lots of thanxxxxxx
|
|
|
|
 |
|
 |
nice code..
modified on Tuesday, July 12, 2011 4:41 AM
|
|
|
|
 |
|
 |
public class FormBase : Form
{
private Point start_point;
private System.ComponentModel.IContainer components = null;
public FormBase()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null)) components.Dispose();
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.SuspendLayout();
this.ClientSize = new System.Drawing.Size(298, 182);
this.Name = "FormBase";
this.Text = "AlerterForm";
this.ControlAdded += new System.Windows.Forms.ControlEventHandler(this.Form_ControlAdded);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form_MouseMove);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form_MouseDown);
this.ResumeLayout(false);
}
private void Form_ControlAdded(object sender, ControlEventArgs e)
{
e.Control.MouseDown += new MouseEventHandler(Form_MouseDown);
e.Control.MouseMove += new MouseEventHandler(Form_MouseMove);
}
void Form_MouseDown(object sender, MouseEventArgs e)
{
this.start_point = new Point(e.X, e.Y);
}
void Form_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
Point p1 = new Point(e.X, e.Y);
Point p2 = this.PointToScreen(p1);
Point p3 = new Point(p2.X - this.start_point.X, p2.Y - this.start_point.Y);
if (this.FormBorderStyle != FormBorderStyle.None) p3.Y -= SystemInformation.CaptionHeight;
this.Location = p3;
}
}
|
|
|
|
 |
|
 |
Public Class FormBase
Inherits Form
Private M_StartPoint As Point
Public Sub New()
Me.InitializeComponent()
End Sub
Private components As System.ComponentModel.IContainer
Private Sub InitializeComponent()
Me.SuspendLayout()
'
'FormBase
'
Me.ClientSize = New System.Drawing.Size(304, 172)
Me.Name = "FormBase"
Me.Text = "Test"
Me.ResumeLayout(False)
End Sub
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then components.Dispose()
MyBase.Dispose(disposing)
End Sub
Private Sub Form_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles Me.ControlAdded
AddHandler e.Control.MouseDown, AddressOf Me.Form_MouseDown
AddHandler e.Control.MouseMove, AddressOf Me.Form_MouseMove
End Sub
Private Sub Form_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
Me.M_StartPoint = e.Location
End Sub
Private Sub Form_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If e.Button <> Windows.Forms.MouseButtons.Left Then Return
Dim p2 As Point = Me.PointToScreen(e.Location)
Dim p3 As New Point(p2.X - Me.M_StartPoint.X, p2.Y - Me.M_StartPoint.Y)
If Me.FormBorderStyle <> Windows.Forms.FormBorderStyle.None Then p3.Y -= SystemInformation.CaptionHeight
Me.Location = p3
End Sub
End Class
-- modified at 9:43 Sunday 26th March, 2006
|
|
|
|
 |
|
 |
I would like to see this code in C# mode, please.
harri
|
|
|
|
 |
|
 |
This is in the topic "A Shorter and Better way"
-- modified at 6:18 Monday 10th April, 2006
|
|
|
|
 |
|
 |
When the FormBorderStyle is not equals to FormBorderStyle.None.
New Code:
void Form_MouseMove(object sender, MouseEventArgs e)
{
//
//If drag = true, drag the form
//
if (this.drag)
{
Point p1 = new Point(e.X, e.Y);
Point p2 = this.PointToScreen(p1);
Point p3 = new Point(p2.X - this.start_point.X, p2.Y - this.start_point.Y );
if (this.FormBorderStyle != FormBorderStyle.None) p3.Y -= SystemInformation.CaptionHeight;
this.Location = p3;
}
}
-- modified at 9:44 Sunday 26th March, 2006
|
|
|
|
 |
|
 |
I kind of like your ideea. Clean and nice .net code.
protected internal static readonly ... and I wish the list could continue ...
|
|
|
|
 |
|
 |
I believe this can also be accomplished by simply handling WM_NCHITTEST in your form's DefWndProc() and returning HT_CAPTION in the result.
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
 |
|
 |
There's also another way to trigger it from a mousedown handler.
Unless you REALLY need to be cross platform I'd recommend one of those Win32 API methods. Much simpler and cleaner.
|
|
|
|
 |
|
 |
Hi Ravi,
Could you please explain this alternate method.And if you can refer any good article for the Windows API in C#, it would be a great help.
|
|
|
|
 |
|
 |
Override the form's DefWndProc() method (which is called to process every good old Win32 message) and handle the WM_NCHITTEST message by returning HT_CAPTION. This fools Windows to thinking the user is dragging the caption bar which causes the form to be repositioned.
See this[^] MSDN link for more info. protected override void DefWndProc
(ref Message m)
{
int WM_NCHITTEST = 0x0084;
if (m.Msg == WM_NCHITTEST) {
m.Result = HT_CAPTION;
return;
}
base.DefWndProc (ref m);
}/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
 |
|
 |
I found this code at:
http://www.codeproject.com/csharp/csharpmovewindow.asp[^]
Sincerely, I think it is not the best way.
protected override void WndProc(ref Message m) {
const int WM_NCHITTEST = 0x84;
const int HTCLIENT = 0x01;
const int HTCAPTION = 0x02;
if (m.Msg == WM_NCHITTEST) {
this.DefWndProc(ref m);
if (m.Result == new IntPtr(HTCLIENT))
m.Result = new IntPtr(HTCAPTION);
else
base.WndProc (ref m);
}
else
base.WndProc (ref m);
}
-- modified at 6:45 Monday 27th March, 2006
|
|
|
|
 |
|
 |
Yes, overriding WndProc() (instead of DefWndProc()) and first testing for a return value of HTCLIENT seems like the right thing to do. Thanks for pointing it out!
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
 |
|
 |
I need the user to grag my window if he clicks anywhere, that means not only on the surface of the form , also on any controls on the form like Labels. The picture i pasted on the article shows a form with a big label.The code you provided doesnt work for this.Do you have any Solutions? But i am thankful for Ravi and you for the great bit of Information. I didnt have any experience on overriding the wndproc. Thank you very much
|
|
|
|
 |
|
 |
You're right!!!
I need that form can be moved using any control into the form. Your code works like a charm... using the API methods.. does not work if you has some control docked in fill mode on the form (i.e: a skin control).
Thank you a lot
|
|
|
|
 |