Movable form with designed Header using c#

No.of Views752
Bookmarked0 times
Downloads 
Votes0
By  NikhilJohari   On  16 Feb 2010 03:02:18
Tag : CSharp , Miscellaneous
Movable form with designed Header using c#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Background

Many times I have seen that many programmer creates designable hearder for their windows forms but then donsen’t have the fesility of moving like predifined windows forms, but after reading this article this is simple to create a .dll form header movable.You can create any control movable on the form by this code.

First step
First you have to create a Header by Windows Control Library in Visual Studio / any version.After creating header just use this in your windows project as a usercontrol from toolbox after importing.


Interface

 

Image Loading

 

Csharp Code

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Movable_Form
{
public partial class Form1 : Form
{
public int diff_x;
public int diff_y;
public bool mouse_down = false;
public Form1()
{
InitializeComponent();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
diff_x = Form.MousePosition.X - Form.ActiveForm.Location.X;
diff_y = Form.MousePosition.Y - Form.ActiveForm.Location.Y;
mouse_down = true;
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouse_down = false;
}
private void button1_Click(object sender, EventArgs e)
{
Dispose();
this.Close();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouse_down == true)
{
Point p = new Point(MousePosition.X - diff_x, MousePosition.Y - diff_y);
Form.ActiveForm.Location = p;
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
diff_x = Form.MousePosition.X - Form.ActiveForm.Location.X;
diff_y = Form.MousePosition.Y - Form.ActiveForm.Location.Y;
mouse_down = true;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (mouse_down == true)
{
Point p = new Point(MousePosition.X - diff_x, MousePosition.Y - diff_y);
Form.ActiveForm.Location = p;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
mouse_down = false;
}
}
}

Now here you are seeing this form header is different and also movable.For further help contact me . I will happy to help you

 
Sign Up to vote for this article
 
About Author
 
NikhilJohari
Occupation-
Company-
Member Type-Senior
Location-India
Joined date-15 Aug 2009
Home Page-http://fast-get.com
Blog Page-http://dotnetask.blog.co.in
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top