Use Visual C # to make scalable personalized form

xiaoxiao2021-03-06  118

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 ( );}} Is very simple and easy to understand, I will not have to tell, other events are also as simple, and there is no implementation code for all events, just list the code response class that needs 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 hity) {x = hitx; y = hope;} 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 the system to close the button

IF

(E.x

>

Width

-

20

&&&&

E.Y

>

6

&&&&

E.x

<

Width

-

20

Sysbutton_min.width

&&&&

E.Y

<

6

Sysbutton_min.height)

{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 (ex, EY); // Change the pointer shape IF of the mouse (Mouse! = Null) {mouse.action (control.mouseposition.x, control.mouseposition.y, this); // Execution time response // Note coordinates are 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 f ((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.sizewewe;} else} = =}} else} = 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 a double-click to maximize or restore the title bar: Private

Void

Form_doubleclick

Object

Sender, System.EventArgs E)

{If (y> TOP && y

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:

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 :).

Production resource file link:

http://blog.9cbs.net/jabby12/archive/2004/08/17/77484.aspx

Original link:

http://www.microsoft.com/china/community/column/60.mspx

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

New Post(0)