This article is reproduced: http://www.cnblogs.com/jingmoxukong/p/4843776.html
C# custom window
Panda Mohist 2015-10-02 23:06:10
6901 Uploading... Re-upload Cancel Favorites 6
Category Column: C# Articles Tags: C# C# Custom Window
C# column includes this content
78 Articles1 Subscribe
This article is reproduced: http://www.cnblogs.com/jingmoxukong/p/4843776.html
[C#] Draw a custom form
Table of contents
- Go to windows to remove the border
- title
- Events related to the title bar
- draw border
- draw shadows
- References
Go to windows to remove the border
Initial window style:
Set the window FormBorderStyle property to None to remove the surrounding borders
At this point the window will become a whiteboard with nothing.
title
title
Drag a panel control onto the form, set the background image as the title bar.
Window icons and themes
Use a PictureBox as the Logo, import the picture, and set the BackColor property to Transparent.
To use a Label as the window theme, set the Text value to the window and the BackColor property to Transparent.
Minimize, close button
(1) Set the button style
First, drag two PictureBox controls to the title bar;
Then set the images of the minimize and close buttons for the two PictureBox images respectively;
Set the BackColor property to Transparent.
At this point, the effect of the window is as follows:
Note: At the beginning, I used the Button control as the minimize and close buttons. But for some reason, sometimes a border appears around the button, and it cannot be eliminated, so use PictureBox instead.
Interested friends can try this method:
Clear the Text value of the Button; import a corresponding picture for the Image of the Button (if you want to match the background color, it is best to be a transparent picture); set the BackColor property of the Button to Transparent; set the FlatStyle property to Flat,
Both the MouseDownBackColor and MouseOverBackColor properties are set to Transparent, and the BorderSize is set to 0.
Events related to the title bar
The basic style seems to be almost the same, but because we hide the title bar of the windows default form, we can't drag the window, and we can't minimize or close the window.
Well, well, let's write it ourselves.
drag form
Add MouseDown and MouseMove events for the title bar, which is the Panel control we added earlier.
Then dynamically refresh the position of the form according to the position of the mouse.
code show as below:
private Point offset;
private void panelTitleBar_MouseDown( object sender, MouseEventArgs e)
{
if (MouseButtons.Left != e.Button) return;
Point cur = this.PointToScreen(e.Location);
offset = new Point(cur.X - this.Left, cur.Y - this.Top);
}
private void panelTitleBar_MouseMove( object sender, MouseEventArgs e)
{
if (MouseButtons.Left != e.Button) return;
Point cur = MousePosition;
this.Location = new Point(cur.X - offset.X, cur.Y - offset.Y);
}
minimize window
Add a click event for the minimize button, which is the PictureBox we added.
One line of code is done, set the state of the form.
private void pictureBoxMinimize_Click( object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
close the window
For the close button, add a click event.
Close the window, releasing resources.
private void pictureBoxClose_Click( object sender, EventArgs e)
{
this.Close();
this.Dispose();
}
At this point, the basic functions required by a simple form are completed.
draw border
Because the previous form has removed the built-in border of the windows form, the whole form has no border, and it will be a little strange when displayed, especially when the background color of the desktop is white.
The form border can be drawn using the following method
In the form load event, you need to set the FormBorderStyle property to FormBorderStyle.FixedSingle.
#region window border
// Redraw window border
// The following statements must be used to take effect when loading the window
// this.FormBorderStyle = FormBorderStyle.FixedSingle;
protected override CreateParams CreateParams
{ get { const int WS_CAPTION = 0xC00000; const int WS_BORDER = 0x800000 ; CreateParams CP = base.CreateParams; CP.Style &= ~WS_CAPTION | WS_BORDER; return CP; } } #endregion private void Form1_Load( object sender, EventArgs e) { this.FormBorderStyle = FormBorderStyle.FixedSingle; }
After running, the effect is as follows:
draw shadows
the first way
Disadvantages: If it is not the default windows form, only the lower right corner has a shadow effect.
Quote System.Runtime.InteropServices;
private void Form1_Load( object sender, EventArgs e)
{
SetClassLong( this.Handle, GCL_STYLE, GetClassLong( this.Handle, GCL_STYLE) | CS_DropSHADOW);
}
This article is reproduced: http://www.cnblogs.com/jingmoxukong/p/4843776.html
private const int CS_DropSHADOW = 0x20000;
private const int GCL_STYLE = (- 26);
[DllImport( " user32.dll ", CharSet = CharSet.Auto)]
public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
[DllImport( " user32.dll ", CharSet = CharSet.Auto)]
public static extern int GetClassLong(IntPtr hwnd, int nIndex);
renderings
The second way:
Thanks to Jocks' method, the original site: http://bbs.cskin.net/thread-61-1-1.html
Add a reference to SkinForm.dll, then using CCWin
Finally, let the Form object inherit the SkinMain class.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using CCWin;
namespace WinformTest
{
public partial class PrivateForm : SkinMain
{
public PrivateForm()
{
InitializeComponent();
}
private void Form1_Load( object sender, EventArgs e)
{
// SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW);
}
... ...
}
}
The effect is as follows:
So far, a simple custom window has been realized, it looks like it