Use Visual C # to make scalable personalized forms (references)

zhaozj2021-02-16  86

introduction:

Everyone wants your application to leave a deep impression, let your program form a good way to have a distinctive "outerwear". Imagine: Suddenly jump out of a cool interface in a lot of ordinary windows, you can make people look bright and more interested.

How to customize the scalable personalized window in VB and VC has long been secret, there is already a large number of related articles to introduce, nothing more than how to call the system API, but call the API in .NET but relative There are also more articles that are more troublesome, so there are also some articles that make personalized forms, usually use transparent background plus pictures, so you can't move or you can arbitrarily zoom in. Is there a way to achieve a scalable personalized form without calling a system API? Of course, .NET Framework provides a very powerful system class library, let's take a scalable personalized form that uses "pure" .NET.

We need to replace all the "skin" of the form to our own definition, including the title bar, border and system, etc., so we first need to customize a set of own skin graphics files. Because the form is scalable, we cannot simply take a whole picture as a form of skin, but to cut pictures into different parts as needed, in general, there are several parts (red line) For cutting lines):

According to the orientation, the images are named: Bottom_Light, Bottom_Middle, Bottom_Right, Middle_LEFT, MIDDLE_RIGHT, TOP_LEFT, TOP_MIDDLE, TOP_RIGHT, SYSB /TTON_MIN, SYSBUTTON_MAX, SYSB /TTON_CLOSE, SYSBUTTON_RESTON_RESTON_RESTORE, and so on. Note that some pictures are places that can be telescopic, such as the picture of Middle_left, Bottom_Middle, etc., can be just a small piece, after which it needs to be repeated. Some of the fixed size pictures, such as Bottom_left, Top_Left, etc., will only be used to distinguish when actually applications.

With the above principles, you can make skin pictures, as shown below:

You can then put these images into the ImageList control or resource file for program calls. (Please refer to how to make a resource file:

Visual C # Resource File Programming - Create a resource file)

Next, we use Visual Studio .Net to create a new Windows application project, in the property settings, set the form's FORMDERSTYLE attribute to None, as shown in the following figure:

Define an resource manager:

Private ResourceManager RM;

Then use the following method to take the picture in the form of the Form (the resource file is named Skin.Resources):

RM = New ResourceManager ("SkinWindow.skin", assembly.getexecutingassembly ());

Bottom_left = (bitmap) RM.GetObject ("Bottom_left");

... (Other pictures also take this method)

Overloading the ONPAINT event:

Graphics g = E.Graphics; // Hand drawn forms

DrawMiddle_left (E.Graphics); // Draw left Border

DrawBottom_Middle (E.GRAPHICS); / / Draw Border

DrawMiddle_Right (E.Graphics); // Painting Right Box

Drawbottom_left (e.graphics); // Draw the lower corner

DrawBottom_Right (E.GRAPHICS); / /

Drawtop_left (e.graphics); // Draw the title bar left

Drawtop_right (E.Graphics); // Painting the title bar

Drawtop_middle (e.graphics); // Draw the middle of the title bar

Drawsys_button (E.Graphics); // Painting System New

The following is a specific implementation part of the above-described paink method, I only give a code example of the left frame, other parts, please read the reader to give a back:

private void DrawMiddle_Left (Graphics g) {Brush brush = new TextureBrush (Middle_Left, new Rectangle (0, 0, Middle_Left.Width, Middle_Left.Height)); g.FillRectangle (brush, 0, TITLE_WIDTH, Middle_Left.Width, Height - Bottom_Middle .Height - Title_width;

The clothes are put on, and now our procedure has different appearances:

It is already cool, but it is just a flower shelf, because the border, the title bar, the system is called the fake border, the title bar and system, so this form cannot be blocked. It is useless to narrow, and it is useless. We used to write programs to never need to care, these are the basic functions of the form? I have never thought of this actually a problem?

How to do it? The answer is what we are doing, but it will be more trouble, because the border is canceled, so Windows will not help you send a system event. If you can't catch what happen, there is no way to write a response code, so we want I have detected the coordinates of the mouse, and according to the action of the mouse, I will issue an event message and then respond.

First we first define some code of response events, I defined an abstract base class mouseAction, used to represent all mouse events, it has an abstract method Action:

Public Abstract Class MouseAction

{

Public Abstract Void Action (int Screenx, Int Screeny, System.Windows.Forms.form Form);

}

Then define each of its derivative classes to indicate the code for each mouse event response.

Below is a code response to the right stretch window event:

public class MouseSizeRight: MouseAction {private int lx; public MouseSizeRight (int LocationX) {lx = LocationX;} public override void Action (int ScreenX, int ScreenY, System.Windows.Forms.Form form) {form.Width = ScreenX - lx Form.invalidate ();}}

Very simple and easy to understand, I will not have to repeat that other events are also as simple, and there is no implementation code for all events, just list the code response classes that need to be implemented: MouseSizEleft: Stretch left Box

MouseSizeBottom: Stretch the next border

MouseSizetop: stretch on the border

MouseSizetopleft: Stretching the upper left corner

MouseSizetopright: Trearate the uppermost corner

MouseSizeBottomLeft: stretching the lower left corner

MouseSizeBottomright: Stretch the lower right corner

MouseDrag: Drag of mouse

The mouse drag is also very simple, but it is slightly different from the zoom stretching of the window, here, here, the implementation code:

public class MouseDrag: MouseAction {private int x, y; public MouseDrag hitX (int hitX, int hitY) {x =; y = hitY;} public override void Action (int ScreenX, int ScreenY, System.Windows.Forms.Form form ) {form.location = new point (screenx - x, screeny - y);}}

Next, we start writing the code that issues an event, first define several variables:

Private int = 5, Right = 5, Bottom = 5, TOP = 5, Title_Width = 45; // Border and the size of the title bar

Private int x = 0, y = 0; // Save the temporary coordinates of the mouse

Private mouseaction mouse; // Event response object of mouse

Then record the current coordinates of the mouse in the Form's mousedown event:

X = E.x;

Y = E.Y;

Attached: E is System.Windows.Forms.MouseEventArgs

Then define an event response object according to the mouse of coordinates:

// Mouse click on the left border IF ((EX <= left 10 && ey <= top) || (EY <= TOP 10 && EX <= Left) {mouse = new mouseesizetopleft (location.x, location.y Width, height; Return;}

Of course, some events can also respond directly:

// Mouse click on the system to close the button IF (E.X> Width - 20 && E.Y> 6 && E.x

Most of the event responds are actually completed in the Mousemove event:

private void Form_MouseMove (object sender, System.Windows.Forms.MouseEventArgs e) {this.Parent.Cursor = CheckCursorType (eX, eY); // change the shape of the mouse pointer if (mouse = null!) {mouse.Action (Control .Mouseposition.x, control.mouseposition.y, this); // Execute time response // Note coordinates are given by control.MousePosition This static variable is given, its value is the global coordinate}}}} on the desktop}} The shape of the mouse in different parts:

Private Cursor Checkcursortype (INT X, INT Y) {IF (((x <= left 10 && y <= TOP) || (Y <= TOP 10 && x <= Left) || (((x> = Width - Right - 10 && Y> = Height - Bottom || (Y> = HEIGHT - BOTTOM - 10 && x> = width - Right))) {Return Cursors.sizenwse;} elseiff (((Y <= TOP) 10 && x> = width - right) || (Y <= TOP && x> = Width - Right - 10)) || (((x <= left && y> = height - bottom - 10) || (Y < > = Height - Bottom && x <= Left 10))) {Return Cursors.SizENESW;} else if (x> = width - Right || x <= left) {return cursors.sizewe;} else if (y> = Height - Bottom || Y <= TOP) {Return Cursors.sizens;} else {return.s.Arrow;}}

Finally release the mouse variable in the mouseup event:

Private void form_mouseup (Object sender, system.windows e) {mouse = null;

In order to be more realistic, you can also add the double-click to maximize or restore the title bar:

private void Form_DoubleClick (object sender, System.EventArgs e) {if (y> TOP && y

to sum up:

Now compile your program, run, your form has all the features you have in the normal form, and there is also a different look, unknown people, you can't guess how you get it. , Good, I don't know if I have someone else, hurry out to show off :).

转载请注明原文地址:https://www.9cbs.com/read-14003.html

New Post(0)