Use Borland C # builder to create irregular forms

Source: Internet
Author: User
Xu Changyou Youyou online
Creating irregular forms involves API calls and a large number of programming. Below we can use Borland C # builder to easily implement an irregular form. The following uses an example to describe its production process.
1. Prepare an irregular form bitmap
Ii. Form settings
3. Code Completion

1. Prepare an irregular form bitmap
For convenience, we can look for a few skins for other software.
Here, skins/ocean/kingdvd_disable.bmp in the installation directory of Kingsoft video overlord 2003 are used.
Of course, you can use a drawing tool to create a shape bitmap. The background uses a special color, such as white. This color will be used later.
[Related textures]

Ii. Form settings
1. Create a C # Application
[Related textures]

2. Select the new form and set its attributes:
(1) set the formborderstyle attribute to none.
(2) set the backgroundimage attribute of the form to the front bitmap file.
(3) set the transparencykey attribute to the background color of the bitmap file, which is white in this example. (This attribute indicates which parts of the application form need to be set to transparent. )
(4) adding a picturebox1 is to close the bitmap and close the application when you click it.
(5) Add a contextMenu1, add a menu item "exit", and set the contextMenu of winform to contextmenu1.

Press F9 to run your program and you will be able to see your irregular forms.
[Related textures]

3. Code Completion
The preceding window cannot be moved. Add the following code to make it move and post the complete sample code:

Using System;
Using System. Drawing;
Using System. Collections;
Using System. ComponentModel;
Using System. Windows. Forms;
Using System. Data;

Namespace SForm
{
/// <Summary>
/// Summary description for WinForm.
/// </Summary>
Public class WinForm: System. Windows. Forms. Form
{
/// <Summary>
/// Required designer variable.
/// </Summary>
Private System. ComponentModel. Container components = null;
Private System. Windows. Forms. ContextMenu contextMenu1;
Private System. Windows. Forms. MenuItem menuItem1;
Private Point mouseOffset; // records the coordinates of the mouse pointer.
Private bool isMouseDown = false;
Private System. Windows. Forms. PictureBox pictureBox1; // record whether the mouse button is pressed

Public WinForm ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent ();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <Summary>
/// Clean up any resources being used.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region Windows Form Designer generated code
/// <Summary>
/// Required method for Designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void InitializeComponent ()
{
System. Resources. ResourceManager resources = new System. Resources. ResourceManager (typeof (WinForm ));
This. contextMenu1 = new System. Windows. Forms. ContextMenu ();
This. menuItem1 = new System. Windows. Forms. MenuItem ();
This. pictureBox1 = new System. Windows. Forms. PictureBox ();
This. SuspendLayout ();
//
// ContextMenu1
//
This. contextMenu1.MenuItems. AddRange (new System. Windows. Forms. MenuItem [] {
This. menuItem1 });
//
// MenuItem1
//
This. menuItem1.Index = 0;
This. menuItem1.Text = "quit ";
This. menuItem1.Click + = new System. EventHandler (this. menuitem#click );
//
// PictureBox1
//
This. pictureBox1.Image = (System. Drawing. Image) (resources. GetObject ("pictureBox1.Image ")));
This. pictureBox1.Location = new System. Drawing. Point (290, 8 );
This. pictureBox1.Name = "pictureBox1 ";
This. pictureBox1.Size = new System. Drawing. Size (14, 13 );
This. pictureBox1.SizeMode = System. Windows. Forms. PictureBoxSizeMode. AutoSize;
This. pictureBox1.TabIndex = 0;
This. pictureBox1.TabStop = false;
This. pictureBox1.Click + = new System. EventHandler (this. picturebox#click );
//
// WinForm
//
This. AutoScaleBaseSize = new System. Drawing. Size (6, 14 );
This. BackgroundImage = (System. Drawing. Image) (resources. GetObject ("$ this. BackgroundImage ")));
This. ClientSize = new System. Drawing. Size (365,235 );
This. ContextMenu = this. contextMenu1;
This. Controls. Add (this. pictureBox1 );
This. FormBorderStyle = System. Windows. Forms. FormBorderStyle. None;
This. MaximizeBox = false;
This. MinimizeBox = false;
This. Name = "WinForm ";
This. Text = "WinForm ";
This. transparencykey = system. Drawing. color. White;
This. mousedown + = new system. Windows. Forms. mouseeventhandler (this. winform_mousedown );
This. mouseup + = new system. Windows. Forms. mouseeventhandler (this. winform_mouseup );
This. mousemove + = new system. Windows. Forms. mouseeventhandler (this. winform_mousemove );
This. resumelayout (false );
}
# Endregion

/// <Summary>
/// The main entry point for the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (new WinForm ());
}

Private void menuItem1_Click (object sender, System. EventArgs e)
{
This. Close ();
}

Private void WinForm_MouseMove (object sender, System. Windows. Forms. MouseEventArgs e)
{
If (isMouseDown)
{
Point mousePos = Control. MousePosition;
MousePos. Offset (mouseOffset. X, mouseOffset. Y );
Location = mousepos;
}

}

Private void winform_mousedown (Object sender, system. Windows. Forms. mouseeventargs E)
{
Int xoffset;
Int yoffset;

If (E. Button = mousebuttons. Left)
{
Xoffset =-E. X-systeminformation. framebordersize. width;
Yoffset =-E. Y-systeminformation. captionheight-
SystemInformation. FrameBorderSize. Height;
MouseOffset = new Point (xOffset, yOffset );
IsMouseDown = true;
}
}

Private void WinForm_MouseUp (object sender, System. Windows. Forms. MouseEventArgs e)
{
// Modify the isMouseDown value of the mouse state
// Make sure that the form is moved only when the left mouse button is pressed and moved.
If (E. Button = mousebuttons. Left)
{
Ismousedown = false;
}
}

Private void picturebox#click (Object sender, system. eventargs E)
{
This. Close ();
}
}
}

Note: If the color depth of the monitor is greater than 24 bits, and the transparencykey is set to white, the non-transparent part of the form will still be displayed. I don't know why.
Interested friends exchange, Home: http://yousoft.hi.com.cn

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.