
Projects By LANGUAGE
Libraries
Articles & seminars
Source Code

|
Creating Custom Windows |
|
|
Introduction |
|
|
This tutorial introduces you to creating your own custom appearance user interfaces,which you will find to be much more easy then you've ever expected.The code for this tutorial is written in C#, and for it to work on your machine,you must have .NET Framework installed. |
|
|
|
|
Getting to work |
|
|
|
![]() |
|
|
|
|
| |
|
private void pictureBox2_Click_1(object sender,System.EventArgs e) { } |
|
|
So,we need to tell the form that when that picture box is clicked,it must close.We need to add the following code: |
|
|
this.Close(); |
|
|
The application is ready to run for the first time.To run it,we must choose Debug -> Start Without Debugging (or simply press Ctrl+F5).For the final part of this tutorial,let's add functionality to the form by allowing the user to move it.To do that,we must handle events for the MouseDown,MouseMove and MouseUp actions.In the Designer View,select the first PictureBox, the bigger one, and go to the Properties window (or press F4),and click the lightning shaped button.In the Mouse section,double-click MouseDown property.Visual Studio has created for you a function to handle the MouseDown action on the PictureBox(NOT on the form): |
|
|
private void pictureBox1_MouseDown (object sender,System.Windows.Forms.MouseEventArgs e) { } |
|
We'll need to declare a variable to see weather the mouse is pressed or not (We called it mouse_is_down,you can call it however you want).We must declare this variable,after the class definition,right under the declaration of the two PictureBox controls.Add the following code: |
|
|
private bool mouse_is_down=false; |
|
Now,go to pictureBox1_MouseDown method and add the following: |
|
|
|
|
We now know when the mouse is down.But if the user presses the mouse and then releases it,our mouse_is_down remains true.Let's fix that by adding another function that handles the MouseUp event.In the Mouse section,double-click MouseUp property,and add the following code: |
|
|
mouse_is_down=false; |
|
Let's go now to the pictureBox1_MouseMove method, and add: |
|
|
if ( mouse_is_down ) { Point current_pos = Control.MousePosition; current_pos.X = current_pos.X - mouse_pos.X; //add this current_pos.Y = current_pos.Y - mouse_pos.Y; //add this this.Location = current_pos; } |
|
