Learn to do c # skin beautification (5)
-- Create MainForm 2
Navigation of previous articles
Let's review our previous issues.
1. Eliminate the opacity in the graph.
2. Drag the title bar
3. Maximize, minimize, and disable the form.
Here we will solve the problem one by one.
Solve the Problem of opacity:
First, why is there gray. It is easy to think that the gray color is the background color of Picturebox. The image on the corner is transparent to pink, but the background color is displayed. We can solve the problem by knowing the cause, that is, we need to make the background color transparent. Some people say that the background color of picturebox is transparent. Yes, yes, but what about the background color of the entire form? In picturebox, there is not a whole covered form! The form cannot be set as transparent as the background color of the user control, but the TransparencyKey attribute can indirectly solve this problem for us. The role of this attribute is as follows. When this attribute is set to a certain color, all the colors on the form will become transparent.
So we only need to set the TransparencyKey color to the same as the form background color, so the background color will not be automatically transparent! Here we set the background color and TransparencyKey of all controls to the Info color in the system (you can specify this color by yourself, but it is not recommended to use the original Control color, in this case, you can add another common control, and some of the control will be transparent)
Drag the title bar
Because our form does not have a status bar, when we want to move the window, we can see that it cannot be moved ). To solve this problem, you still need to use APIs. So how do you know that someone else needs to move the form? Here we will judge by the MouseDown event. That is, if you press the left mouse button in a specific area, you need to move the client. At this time, we call the corresponding API to notify the system (SendMessage) for the system to respond. For better results, we use this method for all the MouseDown events in the row of the status bar. Now let's run it to see if the form can be moved.
Great! Another problem is solved. Now, you only need to complete the functions of the three buttons in the upper right corner of the form. Okay, let's continue...
To process the button event, double-click the corresponding button in design mode and process it. We need to add a virtual token for every method of these events so that these methods can be overwritten. The reason is very simple. If I don't want to close it directly when I press the close button, but a dialog box pops up to remind the user what to do? There is always no way for people who use this control to modify this skin control. The best way is to let them rewrite the click Event of the button. What do they do, if you don't want to have other functions, you just need not rewrite them.
Let's get started with the minimization function. It's easy to get it done in one sentence: this. WindowState = FormWindowState. Minimized; then, close the button or a sentence: this. close (); it seems that these two functions are really simple! The original maximization was not complex, but when we encountered the problem mentioned above, we knew we would like to find another solution. Check the Code:
When you click the maximize button, we first move the maximize button up and hide it, and then drop the restore button down (remember the position we set for him at the beginning is-30 ?), Then, before maximizing the value, we first record the size and position of the form (that is, the four sentences assigned at the beginning) to facilitate subsequent restoration. In the following four sentences, we will manually create a form to maximize the status. First, the position must be in the upper left corner. Then the size should be the size of the client area on the user's computer Screen. this size is obtained using the function Screen. PrimaryScreen. WorkingArea. After the status is set, we only need to change the form's status mark st to stat. Max. Then, when the user wants to restore it, we only need to reverse the previous action in the click event of btres. Well, isn't it difficult! OK. Finally, let's take a look at the running effect:
Problems found:
Finally, it is done well. However, if you have actually started with me, you may find the following problems:
1. When the click is maximized, the response seems a little slow, meaning that it will be maximized after half a second.
2. another problem is to restore from the minimized state to the maximized State. There will be flashes in the upper left corner of the form (the flash is so fast, there is no way, you can use the DLL uploaded below to run it ). We originally wanted to beautify the window, but it would be worse or worse if these problems were solved. In the next article, I will help you solve this problem.
3. Although the position of the form can be moved, the size cannot be dragged.
In the next article, I will help you solve these problems, so stay tuned!