C # Form Skin Creation (ii): Create a form library project and implement a minimize, maximize, close button

Source: Internet
Author: User

very happy to have a friend to pay attention to this blog, but also very sorry to keep the attention of friends waiting, separated on a blog has not been updated 3 months, mainly because of the resignation in March, the beginning of April left the expiry of Beijing High German, came to Shanghai Zhangjiang. At present, the new work is also in the familiar, I hope everyone can understand. Just this weekend a little time, I went on to write, this blog is mainly for beginners, I hope to give them to provide a more understandable form of skin production ideas, I remember when I learned C # programming time is also stones.

Gossip Less, I still continue to write on the blog, last explained how to collect picture resources, this time with 360 security guards to do the example of imitation, originally also wanted to imitate under QQ, but QQ interface is not too popular also more complex, because the main purpose is to let beginners understand the idea of making skin, saying " To give people to fish as a result of fishing ", so the process of the interpretation of this blog will be relatively simple and understandable, so that beginners can learn to understand, and will each of the corresponding code upload to my CSDN resources, interested can be downloaded. I believe that as long as the understanding of the idea, coupled with their efforts, QQ and other interfaces can also be done.

This blog is mainly about the implementation of the custom form of general ideas, and minimize, maximize, close button implementation. The general idea is to remove the default border of the form and use other controls to override the default implementation of the Minimize, Maximize, and Close buttons. First of all, we will not consider too much detail, and then do some refactoring of the code when the function is developed. because it is the first article about the code, it will be from the creation of the project, the introduction of the image resources, step-by-step explanation.

first step : Selection of resource pictures
Resource picture: [http://download.csdn.net/detail/ bbirdsky/6923955] find 360safe.zip in compressed package;
./mainframe/image/
background_mainwnd.jpg  //this as the background of the form, in order to name the simple rename to IMG_BG
Sys_button_***.jpg   //These 4 images corresponding to the minimize, maximize (Restore), Close button are renamed btn_*** .

second step : Creating a solution (MySkins)

and add a Window form Control Library item MySkins to do the custom skin, add a window form Application project Myskinstest for testing, and Myskinstest as the default startup project, and add a reference project to Myskinstest myskins step



Step three : Create a directory structure

Create Contorl, Entity, Frame, util to hold the corresponding class files, and add the picture resources to the project's picture resources, the final project structure


Fourth Step : Test the Code
Add a baseform form to the MySkins project first, and let the default Form1 form inherit from BaseForm in the Myskinstest test project, test project is only used to test the effect of MySkins project, be sure to add myskins dependent project first , or you will not find BaseForm this class!

namespace myskinstest{public    partial class Form1:baseform    {public        Form1 ()        {            InitializeComponent ();}}}    
Fifth Step : Skin code (BaseForm)

1, the core of this article is the BaseForm form base class, first create a normal form in the frame directory, and do the following property settings:
1> set the FormBorderStyle to none, the form has no border and the maximum size, minimize, close button;
Set BackgroundImage to IMG_BG, and set doublebuffered double buffering to True.

2> add three PictureBox, respectively, to replace the minimize, maximize, close button, or try to use the button substitution;
Set the BackColor of the PictureBox to transparent transparent, the default is gray;
The positioning of the three PictureBox is set to Anchor:top,right, so that the maximum position of the button is left unchanged at the right;
Binding the mouse to click, move in, and press the lift event, it is recommended that the same event be handled with the same event method.

3> adds a ToolTip control that displays the prompt information and sets the effect such as:

2, Code Analysis: This talk mainly contains ControlState control state and Resutils resource helper class.

1> because the picture background picture is 4 for a group, so define the state enumeration class: ControlState, the code is as follows:

     <summary>///control state///     </summary> public     enum controlstate     {        Normal = 1,//Control by default        MouseOver = 2,//when        mouse moves controls MouseDown = 3,//When the mouse presses the control        Disable = 4//When the control is not available     }

2> because of the need to get pictures from the resources and need to get the picture by state to slice, because some need help class: Resutils, the code is as follows:

    <summary>///resource support///</summary> class Resutils {///<summary>// Get images based on resource name///</summary>//<param name= "name" > Resource name </param>//<returns> image </returns> public static Bitmap getresasimage (string name) {if (name = = NULL | | name = = ""            ) {return null;        } return (Bitmap) Properties.Resources.ResourceManager.GetObject (name); }///<summary>///Picture button background image is 4, get background picture based on status///</summary>//<param name= "nam        E "> Picture name </param>//<param name=" state "> Status </param>///<returns></returns> public static Bitmap Getreswithstate (String name, controlstate state) {Bitmap Bitmap = (Bitmap) G            Etresasimage (name);            if (bitmap = = null) {return null; } int Block = 0;                Switch (state) {case ControlState.Normal:block = 0; Case ControlState.MouseOver:block = 1;                Break Case ControlState.MouseDown:block = 2;                Break Case ControlState.Disable:block = 3;            Break } int width = bitmap.            WIDTH/4; Rectangle rect = new Rectangle (block * width, 0, width, bitmap.            Height); Return bitmap. Clone (Rect, bitmap.        PixelFormat); }    }

     3> the implementation code for minimizing, maximizing, and closing is as follows:
Span style= "Font-family:verdana; font-size:14px ">     this. WindowState = formwindowstate.maximized;  //maximizes
     this. WindowState = formwindowstate.minimized;  //Minimize
     this. WindowState = Formwindowstate.normal;      //General status
     this. Close ();  //Close

3. First, the final effect achieved a relatively simple custom form with maximized, minimized, and closed functions.
features not available: No form small icon, title, cannot drag the form, resize the form, this is the next SectionTo implementation of the function.
Small icons and titles relatively simple and good implementation, the form drag, resize does not use WIN32API can also be implemented, but the effect is not very good, the next section will be implemented using Win32API!
What you don't know about Win32 can look at this document: http://download.csdn.net/detail/bbirdsky/6910413

Finally, I will continue to keep the update. The corresponding code download: http://download.csdn.net/detail/bbirdsky/7366791.

Related Article

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.