| View previous topic :: View next topic |
| Author |
Message |
Xeleron Grandmaster Cheater
Reputation: 0
Joined: 05 Jan 2008 Posts: 652
|
Posted: Sat Jan 24, 2009 2:47 am Post subject: [VB.NET] Drag form on label |
|
|
does anyone has a code so i can drag a form by clicking on a label?
i already searchd on google but without succes
_________________
why Hitler? why did you had to pm me that child porn?
btw Hitler, Shave your pubic hair
PM Me for Hitler's Nude pics |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sat Jan 24, 2009 3:12 am Post subject: |
|
|
The easiest way I think is to just use SendMessage, from the Win32 API.
On the MouseDown event for whatever control you want, send iWM_NCLBUTTONDOWN, with a wParam of HT_CAPTION.
Or maybe even better, you can override WndProc, and handle WM_NCHITTEST yourself.
|
|
| Back to top |
|
 |
Xeleron Grandmaster Cheater
Reputation: 0
Joined: 05 Jan 2008 Posts: 652
|
Posted: Sat Jan 24, 2009 3:24 am Post subject: |
|
|
ok.... can you maybe give me a total code of it xD , i a bit inexpierenced with code's and stuff xD
i am more a guy of design and stuff xD
_________________
why Hitler? why did you had to pm me that child porn?
btw Hitler, Shave your pubic hair
PM Me for Hitler's Nude pics |
|
| Back to top |
|
 |
FullyAwesome I post too much
Reputation: 0
Joined: 05 Apr 2007 Posts: 4438 Location: Land Down Under
|
Posted: Sat Jan 24, 2009 3:54 am Post subject: |
|
|
| matthew2010 wrote: | ok.... can you maybe give me a total code of it xD , i a bit inexpierenced with code's and stuff xD
i am more a guy of design and stuff xD |
EDIT: after all that i found a link for you that shows a vb.net example. here. as explained below, instead of in the mousedown event of the form, put it in the event of the label.
i'm sorry it's in C#, but you should get the gist of things and know how to easily do this in vb.net. as slovach said, using the SendMessage API with WM_NCLBUTTONDOWN and HT_CAPTION is quite easy.
constant variables
| Code: | public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2; |
APIs
| Code: | [DllImport("USER32.DLL")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern bool ReleaseCapture(); |
(the event for mousedown on the actual form)
| Code: | private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
} |
quite simple, you're just basically using SendMessage. for a label, double click it to go to the mousedown event of the label and put the SendMessage code there.
_________________
|
|
| Back to top |
|
 |
Xeleron Grandmaster Cheater
Reputation: 0
Joined: 05 Jan 2008 Posts: 652
|
Posted: Sat Jan 24, 2009 4:37 am Post subject: |
|
|
that code from the explane doesn't work ..
_________________
why Hitler? why did you had to pm me that child porn?
btw Hitler, Shave your pubic hair
PM Me for Hitler's Nude pics |
|
| Back to top |
|
 |
FullyAwesome I post too much
Reputation: 0
Joined: 05 Apr 2007 Posts: 4438 Location: Land Down Under
|
Posted: Sat Jan 24, 2009 6:20 pm Post subject: |
|
|
| matthew2010 wrote: | | that code from the explane doesn't work .. |
what's the errors you get from following this?
_________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sat Jan 24, 2009 7:46 pm Post subject: |
|
|
just handle WM_NCHITTEST
| Code: | protected override void WndProc(ref Message m)
{
const int WM_NCHITTEST = 0x84;
const int HTCAPTION = 0x02;
switch(m.Msg){
case WM_NCHITTEST:
m.Result = (IntPtr)HTCAPTION;
break;
default:
base.WndProc(ref m);
}
} |
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Sat Jan 24, 2009 10:53 pm Post subject: |
|
|
You don't need to call the win32 API's in order to move the form. All you have to do is remember the location of the label when you hold down the mouse and then move it based on that.
| Code: |
Point labelLocation;
bool isEnable = false;
private void label1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isEnable = true;
labelLocation = new Point(e.X, e.Y);
}
}
private void label1_MouseMove(object sender, MouseEventArgs e)
{
if (isEnable)
{
this.Location = new Point(this.Location.X + (e.X - labelLocation.X),
this.Location.Y + (e.Y - labelLocation.Y));
}
}
private void label1_MouseUp(object sender, MouseEventArgs e)
{
isEnable = false;
}
|
Last edited by killersamurai on Thu Jan 29, 2009 2:34 pm; edited 1 time in total |
|
| Back to top |
|
 |
NINTENDO Grandmaster Cheater Supreme
Reputation: 0
Joined: 02 Nov 2007 Posts: 1371
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Thu Jan 29, 2009 2:33 pm Post subject: |
|
|
Even though it's c#, it will still be the same in vb.net. The difference is the syntax.
| Code: |
Public Class Form1
Private labelLocation As Point
Private isEnable As Boolean
Private Sub Label1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
isEnable = True
labelLocation = New Point(e.X, e.Y)
End If
End Sub
Private Sub Label1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove
If isEnable Then
Me.Location = New Point(Me.Location.X + (e.X - labelLocation.X), Me.Location.Y + (e.Y - labelLocation.Y))
End If
End Sub
Private Sub Label1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseUp
isEnable = False
End Sub
End Class
|
|
|
| Back to top |
|
 |
|