Hi KarmaKid15,
> The user should be able to click on the control and drag it around the container and if they overlap bounds with dragging the control will snap back so that it's fully visible in the bounds.
I can give you some suggestions with Winform snap control. Please download the sample code from my skydrive.
http://cid-380a93ce0876cf8b.skydrive.live.com/self.aspx/Microsoft%20MSDN%20Work/Solution%20Code/%5E52009.10.8%5E6WinformSnap.zip
In this sample, I have used SendMessage to move the control at run time and well calculate the pixel for the snap action. The SetSnap method will do the snap work.
private void SetSnap(Control ctrl, Form containerForm)
{
int snapPix = 20;
if (ctrl.Location.X < snapPix)
{
ctrl.Location = new Point(0, ctrl.Location.Y);
}
if (ctrl.Location.Y < snapPix)
{
ctrl.Location = new Point(ctrl.Location.X, 0);
}
if (ctrl.Location.X > (containerForm.Width - snapPix - ctrl.Width - 15))
{
ctrl.Location = new Point(containerForm.Width - ctrl.Width - 15, ctrl.Location.Y);
}
if (ctrl.Location.Y > (containerForm.Height - snapPix - ctrl.Height - 40))
{
ctrl.Location = new Point(ctrl.Location.X, containerForm.Height - ctrl.Height - 37);
}
}
When you use that method, you can easily call it at the end of each moving action.
void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(this.button1.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
this.SetSnap(this.button1, this);
}
Hope this sample gives you some light with snap effect.
>In addition to this i'd like to add cool transparency functionality to it such as on hover its alpha comes into effect.
For this requirement, I have used GetCursorPos API to get mouse cursor location of the screen and set the Opacity property of the form.
Point p = new Point();
GetCursorPos(out p);
if (p.X < this.Location.X || p.X > this.Location.X + this.Width
|| p.Y < this.Location.Y || p.Y > this.Location.Y + this.Height)
{
this.Opacity = 0.2;
}
else
{
this.Opacity = 1;
}
If you have any question with my code, please feel free to tell me.
Sincerely,
Kira Qian
Send us any feedback you have about the help from MSFT at fbmsdn[At]microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the
All-In-One Code Framework!