Use Visual C # to make scalable personalized form (Author: Lu Yan)

xiaoxiao2021-03-06  105

Use Visual C # to make scalable personalized form

Lu Yan

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 Form's onpaint event: graphics g = E.Graphics;

// Hand drawn forms of various parts

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 (int hITX, int hope)

{

x = HITX;

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 ((E.x <= Left 10 && E.Y <= TOP) || (E.Y <= TOP 10 && E.x <= 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

{

CLOSE ();

Return;

}

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 (E.X, E.Y); // Change the pointer shape of the mouse

IF (mouse! = null)

{

mouse.Action (Control.MousePosition.x, Control.MousePosition.y, this); // Perform time response // Note coordinates are given by Control.MousePosition This static variable is given, its value is the global coordinate of the mouse on the desktop

}

}

Given the pointer shape of each different part:

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;

}

ELSE IF ((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.siznesw;

}

Else IF (x> = width - right || x <= left)

{

Return Cursors.sizewe;

}

Else if (y> = height - bottom || y <= TOP)

{

Return Cursors.sizens;

}

Else

{

Return Cursors.Arrow;

}

}

Finally release the mouse variable in the mouseup event:

Private void form_mouseup (Object Sender, System.Windows.Forms.MouseEventArgs 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

{

IF (WindowState == FormWindowState.Normal)

{

WindowState = formwindowstate.maximized;

Sysbutton = sysbutton_restore;

Invalidate ();

}

Else if (WindowsTate == FormWindowState.maximized)

{

WindowState = formwindowstate.Normal;

Sysbutton = sysbutton_max;

Invalidate ();

}

}

}

Preventing the form from being reduced into a point, preferably gives a suitable value, such as 200,200, for the MinimumSize of the window, such as 200, 200.

to sum up:

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

New Post(0)