DOTNET winform FAQ 16

Source: Internet
Author: User
Tags dotnet
Directory
1. How to set a from boundary

 

2. How to create a transparent from

3. How to set the position of the form on the screen

4. How to Make the minimization and maximization buttons unavailable

5. How to Make a form disappear

6. How to Make the form non-rectangular.

7. How to Make a form at the top of the screen.

8. How to display a model and a non-model form

9. How to create an MDI form

10. How to hide your form from the task bar.

11. How to Create a form with a startup screen.

12. How to make your form trayicon.

13. How to modify and control the size and length of the form.

14. How to Create a Windows Explorer-style form.

15. How to set the Initial Startup form

16. How to Create a form with a background image
========================================================== ========================================================== ============

1. How to set a from boundary

There are a total of seven different boundary styles for you to set. You can also set them dynamically at the design time or at the runtime through the Code. These seven boundary styles are:

None (system. Windows. Forms. formborderstyle. None)

Fixed 3D (system. Windows. Forms. formborderstyle. fixed3d)

Fixed Dialog (system. Windows. Forms. formborderstyle. fixeddialog)

Fixed Single (system. Windows. Forms. formborderstyle. fixedsingle)

Fixed Tool window (system. Windows. Forms. formborderstyle. fixedtoolwindow)

Sizable (system. Windows. Forms. formborderstyle. sizable)

Sizable tool window

(System. Windows. Forms. formborderstyle. sizabletoolwindow)

In the design mode, set the formborderstyle attribute in the Properties window of vs.net IDE.

In the running mode, you can use the code to complete the process:

Dlgbx1.formborderstyle = system. Windows. Forms. formborderstyle. fixeddialog
There are no major changes in the seven boundary types VB6. You need to set them against different enumeration variables in the running mode.

 

2. How to create a transparent from

You can achieve this through two methods at the design moment and runtime.

At the design time, you can set the opacity attribute in the Properties window of vs.net ide To achieve this effect. The value ranges from 0.0 to 1.0. 0, indicating full transparency and 1.0 indicating full opacity.

You can use the following encoding to set the opactiy attribute of the form at runtime:

Frmtransparentform. Opacity = 0.76; (C #)

It can be seen that it is very simple now, and you no longer need to understand any alpha variable. Transparency is always just an effect and do not abuse it.

 

3. How to set the position of the form on the screen

You can set the startposition attribute of the Form. vs.net generally gives you a conservative option "windowsdefaultlocation". In this way, the system determines a value based on the user's current computer settings when loading the form, you can also change it to another value "center" during design ".

If you must determine the position of the form on the screen in the design mode, you can first set startposition to manual, and then set the value of location X and Y.

It seems more concise to use code for implementation during runtime:

Form1.location = new point (100,100) (VB. NET)
Of course, you can also modify the location's x and y values respectively, corresponding to the left and top properties of the form, such:

Form1.left ++ = 200 (VB. NET)

Form1.top-= 100 (VB. NET)

Another attribute will also affect the position of the form on the screen: the toplocation attribute is very useful when you set the position of the form relative to the taskbar (when you place the task bar on the top or left of the screen, actually, the desktop coordinates () is changed accordingly. You can set this attribute in the design Properties window,

Form1.shorttoplocation = new point (100,100)

The position of the form on the screen depends mainly on the hardware and settings of each user. Therefore, the default "windowsdefaultlocation" or "center" is used conservatively "; the professional practice is to first obtain the system settings and then encode the dynamic computing before setting them. Otherwise, your form will be easily not found on the screen.

 

4. How to Make the minimization and maximization buttons unavailable

When form. minimizebox and form. maximizebox of the form are set to true, they are displayed. If false, they are unavailable. For programming methods, see:

Frmmaxmin. minnimizebox = false (VB. NET)

Frmmaxmin. maxmnimizebox = true (VB. NET)

 

5. How to Make a form disappear

I think the most direct way is to call the hide () method to do this. but I want to provide another method. After reading it, you will get some other inspiration. (VB. net)

Private const ws_ex_toolwindow as int32 = & h80

Private const ws_popup as int32 = & h80000000

Private const ws_visible as int32 = & h0000000

Private const ws_sysmenu as int32 = & h80000

Private const ws_maximizebox as int32 = & h10000

 

Protected overrides readonly property createparams () as system. Windows. Forms. createparams

Get

Dim CP as system. Windows. Forms. createparams

CP = mybase. createparams

CP. exstyle = ws_ex_toolwindow

CP. Style = ws_popup or ws_visible or ws_sysmenu or ws_maximizebox

CP. Height = 0

CP. width = 0

Return CP

End get

End Property

It turns out that both height and width are set to 0. I think this method may be different from the underlying layer of hide () calls.

 

6. How to Make the form non-rectangular.

I don't think what I offer is the most professional practice. At least it cannot do what I expect, that is, it will change back to the rectangle in some events. but at least I can tell you: if you try to call the original Win32's API set‑wrng, it will not work, and I have tried this. now you may need to know the region attributes of the form.

'// (VB. NET)

Public sub setwindowregion ()

 

Dim formpath as system. Drawing. drawing2d. graphicspath

Dim Reg as drawing. Region

Dim LRET as long

 

Formpath = new drawing2d. graphicspath ()

Formpath. addellipse (New rectangle (0, 0,250,120 ))

 

Me. region = new region (formpath)

 

End sub

 

Private sub button#click (byval sender as system. Object, byval e as system. eventargs) handles button1.click

Me. region = nothing

Setwindowregion ()

End sub

 

Protected overrides sub onresize (byval e as system. eventargs)

Me. region = nothing

Setwindowregion ()

End sub

 

7. How to Make a form at the top of the screen.

This is a very practical function. Now you don't have to call other APIs. You only need to set the topmost attribute to true. this attribute can be modified at the design time and runtime. code method:

Mytopform. topmost = true (VB. NET)

8. How to display a model and a non-model form

The forms of model and modeless mainly depend on your application. Most of them are used to display the dialog box. when you need a model form, you call myform. showdialog instead of calling myform. show. There is an optional parameter ower for showdialog to allow you to create a parent-child relationship for a form. for example:

'Visual basic
Private sub mnuabout_click (... ARGs ...)
Dim F as new formoption
F. showdialog me
End sub
One thing to note is that for showdialog, when this sentence is executed, the form is displayed, but the subsequent code will not be executed, and the execution will continue only after the window is closed, for show, the following code is executed immediately after the form is displayed.
9. How to create an MDI form

1. Create a new windows application project

2. Add two forms: form1 and form2.

3. Set the ismdicontainer attribute of form1 to true. Make it an MDI main form.

4. Add a RichTextBox Control to form2 and set the dock to fill.

5. drag a mainmenu in the tools form to the form form1, and create a menu file | Windows | help. The file contains the new and exit menu items; windows includes cascade and horizontal.

6. Set the mdilist attribute of the Windows menu item to true, so that each MDI subwindow is automatically added under the Windows menu item.

7. Double-click the new menu item and add the following code:

Private void menunew_click (Object sender, system. eventargs E)

{

Form2 newmdichild;

Newmdichild = new form2 ();

Newmdichild. mdiparent = this;

Newmdichild. Show ();

 

}

8. Add the following code to Windows cascade and other menu items:

Private void menuwindowcasca_click (Object sender, system. eventargs E)

{

This. layoutmdi (mdilayout. Cascade );

}

There are also the following commonly used:

This. layoutmdi (mdilayout. tilehorizontal );

This. layoutmdi (mdilayout. tilevertical );

9. Run F5.

The final version of vs.net does not know whether there will be a general template. However, it is complicated to generate an MDI window in a completely manual way. It is not as simple as vs. Net IDE.

 

10. How to hide your form from the task bar.

When the boundary style of the form is Tools window, it will not appear on the task bar. In addition, the method described in the preceding heading 5 will not only be invisible to the form, but also not on the task bar.

If you are now in the world of DOTNET, it will become simple. Any winform now has the showintaskbar attribute, so you only need to set this attribute. Similarly, you can change showintaskbar from true to false in the Properties window. Or use the code:

Mytaskbarfrm. showintaskbar = false; (C #)

 

11. How to Create a form with a startup screen.

You need to prepare two winform forms, one called splashscreen, to make it a beautiful form. Then you need a main form called form1, and then add the following code to the form.

// (C #)

Protected override void onload (system. eventargs E)

{

// Make load take a long time

Thread. Sleep (2000 );

 

Base. onload (E );

 

}

Then add the following code to main:

[Stathread]

Static void main ()

{

Splashscreen splashform = new splashscreen ();

Splashform. Show ();

 

Form1 mainform = new form1 ();

Mainform. Load + = new eventhandler (splashform. mainscreen_load );

Application. Run (mainform );

 

}

Do not forget to add a reference to threading: using system. Threading;

 

12. How to make your form trayicon.

You can use the yyicon control to implement this function. Drag the notifyicon from tools windows to your form and add the following code to the following event, F5.

'// VB. NET

Private micona as icon = new icon ("icon1.ico ")

Private miconb as icon = new icon ("icon2.ico ")

Private micondisplayed as Boolean

Public sub new ()

Mybase. New

 

Form1 = me

 

'This call is required by the win Form Designer.

Initializecomponent

 

'Todo: add any initialization after the initializecomponent () call

'This form isn' t used directly so hide it immediately

Me. Hide ()

'Setup the tray icon

Initializenotifyicon ()

End sub

Private sub initializenotifyicon ()

'Setup the default icon

Policyicon = new system. Windows. Forms. policyicon ()

Notifyicon. Icon = micona

Policyicon. Text = "right click for the menu"

Policyicon. Visible = true

Micondisplayed = true

 

'Insert all menuitem objects into an array and add them

'The context menu simultaneously

Dim mnuitms (3) as menuitem

Mnuitms (0) = new menuitem ("show form...", new eventhandler (addressof me. showformselect ))

Mnuitms (0). defaultitem = true

Mnuitms (1) = new menuitem ("toggle image", new eventhandler (addressof me. toggleimageselect ))

Mnuitms (2) = new menuitem ("-")

Mnuitms (3) = new menuitem ("exit", new eventhandler (addressof me. exitselect ))

Dim policyiconmnu as contextmenu = new contextmenu (mnuitms)

Policyicon. contextmenu = policyiconmnu

End sub

 

Public sub showformselect (byval sender as object, byval e as system. eventargs)

'Display the Settings dialog

Dim settingsform as new settingsform ()

Settingsform. showdialog ()

 

End sub

 

Public sub toggleimageselect (byval sender as object, byval e as system. eventargs)

'Called when the user selects the 'toggle image' context menu

 

'Termine which icon is currently visible and switch it

If micondisplayed then

'Called when the user selects the 'show form' context menu

Policyicon. Icon = miconb

Policyicon. Text = "sad"

Micondisplayed = false

Else

Notifyicon. Icon = micona

Policyicon. Text = "happy"

Micondisplayed = true

End if

 

End sub

 

Public sub exitselect (byval sender as object, byval e as system. eventargs)

'Called when the user selects the 'exit 'Context menu

 

'Hide the tray icon

Policyicon. Visible = false

 

'Close up

Me. Close ()

End sub

 

'Form overrides dispose to clean up the component list.

Public overloads overrides sub dispose ()

Mybase. Dispose ()

Components. Dispose ()

End sub

You have prepared the icon file. If you have succeeded, you can see the various functions related to policyicond.

 

13. How to modify and control the size and length of the form.

Modify the size, width, and height attributes of winform. They can also be modified and set at the design and runtime.

Form1.size = new system. Drawing. Size (100,100) (VB. NET)

Form1.width ++ = 100 (VB. NET)

Form1.height-= 20 (VB. NET)

 

14. How to Create a Windows Explorer-style form.

1. Create a new Windows Application

2. drag a Treeview control, a splitterk control, and a listview control from the Toolbox window, and set the dock attribute of the Treeview to: left in the Properties window respectively. Set the dock attribute of the listview control: fill

3: F5 run

 

15. How to set the Initial Startup form

You can right-click your project in the Solution Explorer window, and select Properties in the winform project of C # and Visual Basic, select the form or main () method you launched from the properties page of your project.

In the current vs.net beta2 project, C # automatically generates the main () method, Visual Basic. net project, you must add the main () Code by yourself. in C #, you can change form1 to any form name that you can start:

// (C #)

Static void main ()

{

Application. Run (New form1 ());

}

 

16. How to Create a form with a background image

All forms in the current winform have a backgroundimage attribute. You only need to assign values to it. Normal forms can be set either in running mode or in running mode. For example, add the following code to initializecomponent () or Form constructor:

This. backgroundimage = new Bitmap ("C :\\ dotnetapp \ winform \ tile.bmp ");

It is troublesome for the primary form of MDI, in. net ide form, after you set the ismdicontainer attribute to true, you need to check whether the initializecomponent () has such code (C #):

This. mdiclient1.dock = system. Windows. Forms. dockstyle. Fill;

This. mdiclient1.name = "mdiclient1 ";

Or you can see mdiclient1 System in the attribute window combo box of the window. windows. forms. mdiclient. this is the primary MDI window, but I did not find any information about SYSTEM IN THE DOTNET documentation. windows. forms. mdiclient description. Then you can add the following code (C #) to initializecomponent () or Form constructor #):

This. mdiclient1.backgroundimage = new Bitmap ("C: \ dotnetapp \ winform \ tile.bmp ");

There is an imageview example on the Internet, which demonstrates how to add a line of Logo text to the background of the MDI main form, so that your MDI form looks very commercialized. Specifically, you can do this:

1. Check whether such a statement (C #) exists in the initializecomponent of the code automatically generated by vs. Net #):

This. Controls. addrange (new system. Windows. Forms. Control [] {This. mdiclient1 });

This mdiclient (haha)

2. Create the following two functions to display the logo character:

// (C #)

Protected void mdi_onpaint (Object S, system. Windows. Forms. painteventargs E)

{

Control C = (Control) S;

 

Rectangle R1 = C. clientrectangle;

R1.width-= 4;

R1.height-= 4;

 

Rectangle r2 = R1;

R2.width-= 1;

R2.height-= 1;

 

Font F = new font ("tahoma", 8 );

 

String STR = "mywinform. Net? 2001 mywinform application V1.0 ";

 

Stringformat Sf = new stringformat ();

SF. Alignment = stringalignment. far;

SF. linealignment = stringalignment. far;

 

E. Graphics. drawstring (STR, F, new solidbrush (systemcolors. controldarkdark), R1, SF );

E. Graphics. drawstring (STR, F, new solidbrush (systemcolors. controllight), R2, SF );

 

}

 

Protected void mdi_onresize (Object S, system. eventargs E)

{

Control C = (Control) S;

C. invalidate ();

}

3. Add the following code to initializecomponent () or Form constructor:

(C #)

This. controls [0]. Paint + = new painteventhandler (mdi_onpaint );

This. controls [0]. Resize + = new eventhandler (mdi_onresize );

Add it after initializecomponent () or after this. Controls. addrange function in the initializecomponent function.

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.