In project development, the design of the form will affect the user's overall impression on the software. Therefore, the form effect should be beautifully designed.
029 create a mouse penetrating form
To make the desktop more beautiful, you can add a glass-like effect on the desktop. You can use the mouse to operate the desktop through the "glass. This example uses the mouse to penetrate the Form class to implement the above functions. The API functions SetWindowLong and GetWindowLong are used.
1. Create a project. The default form is Form1. Set the FormBordeStyle attribute of the form to None, the BackColor attribute to Gainsboro, the Opacity attribute to 60%, and the WindowState attribute to Maximized.
2. Add a policyicon control in the Form1 form, set its Icon attribute to the specified Icon, and display the prompt information. Add a ContextMenuStrip control as the shortcut menu of the program.
Namespace _ partition {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private const uint WS_EX_LAYERED = 0x80000; private const int WS_EX_TRANSPARENT = 0x20; private const int GWL_EXSTYLE = (-20); private string Var_genre = ""; // record the type of the current operation # region sets information for the specified window in the window structure /// <summary> /// set information for the specified window in the window structure /// </summary> /// <param name = "hwnd"> handle of the window for which you want to obtain information </param> /// <param name = "nIndex"> to retrieve </param> /// <param name = "dwNewLong"> the new value of the window information specified by nIndex </param> /// <returns> </returns> [DllImport ("user32 ", entryPoint = "SetWindowLong")] private static extern uint SetWindowLong (IntPtr hwnd, int nIndex, uint dwNewLong ); # endregion # region obtains information from the specified window structure /// <summary> /// retrieves information from the specified window structure /// </summary> // <param name = "hwnd"> handle of the window for which you want to obtain information </param> /// <param name = "nIndex"> information to be retrieved </param>/ // <returns> </returns> [DllImport ("user32 ", entryPoint = "GetWindowLong")] private static extern uint GetWindowLong (IntPtr hwnd, int nIndex ); # endregion # region enables the window to have the mouse penetrating function. /// <summary> // enables the window to have the mouse penetrating function. /// </summary> private void CanPenetrate () {uint intExTemp = GetWindowLong (this. handle, GWL_EXSTYLE); // obtain information from the current window structure // set information for the current window in the window structure uint oldGWLEx = SetWindowLong (this. handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);} # endregion private void Form1_Load (object sender, EventArgs e) {this. showInTaskbar = false; // The form does not appear in the Windows taskbar. The CanPenetrate (); // custom method is used to implement mouse penetration through the API functions SetWindowLong and GetWindowLong. this. topMost = true; // make the form always on top of other forms} # region sets the color and transparency status /// <summary> // sets the color and transparency status /// </summary> private void SetEstate (Form Frm, object sender) {Var_genre = (ToolStripMenuItem) sender ). name; string Tem_Str = Var_genre; if (Var_genre.IndexOf ('_')> = 0) {Var_genre = Tem_Str.Substring (0, Tem_Str.IndexOf ('_');} switch (Var_genre) {case "ToolColor": {Color Tem_Color = Color. gainsboro; switch (Convert. toInt32 (ToolStripMenuItem) sender ). tag. toString () {case 1: Tem_Color = Color. gainsboro; break; case 2: Tem_Color = Color. darkOrchid; break; case 3: Tem_Color = Color. royalBlue; break; case 4: Tem_Color = Color. gold; break; case 5: Tem_Color = Color. lightGreen; break;} Frm. backColor = Tem_Color; break;} case "ToolClarity": {double Tem_Double = 0.0; switch (Convert. toInt32 (ToolStripMenuItem) sender ). tag. toString () {case 1: Tem_Double = 0.1; break; case 2: Tem_Double = 0.2; break; case 3: Tem_Double = 0.3; break; case 4: Tem_Double = 0.4; break; case 5: Tem_Double = 0.5; break; case 6: Tem_Double = 0.6; break; case 7: Tem_Double = 0.7; break; case 8: Tem_Double = 0.8; break; case 9: Tem_Double = 0.9; break;} Frm. opacity = Tem_Double; break;} case "ToolAcquiescence": {Frm. backColor = Color. gainsboro; Frm. opacity = 0.6; break;} case "ToolClose": {Close (); break ;}}# endregion private void ToolColor_Glass_Click (object sender, EventArgs e) {SetEstate (this, sender );}}}
030 form skin replacement program
The basic principle of this example is to change the image for each component of the Form. Based on this principle, you must first analyze the components of the form, it mainly includes the title bar, left border, right border, bottom border, middle area of the form, and possible menu bar, the title bar and three borders cannot be changed by setting relevant attributes. To solve this problem, you can cancel the FormBorderStyle attribute of the form, add a Panel control in the title bar and three borders, and then select different skin types, set an image for each component of the form, and finally achieve the form skin replacement effect. In the above process, the main technical problem is how to create an Image object from a specified file and how to obtain the Image path.
1. Create a project. The default form is Form1. Set the AutoscaleMode attribute of Form1 to Inherit, the DoubleBuffered attribute to True, and the IsMdiContainer attribute to True.
2. add six Panel controls to the form, which are used as the title bar, bottom border, left border, right border, bottom left border, and bottom right border of the form respectively. Add a ContextMenuStrip control, A shortcut menu for skin replacement. Three PictureBox controls are added to maximize, minimize, and disable images.
Namespace _ 030_WinCusSkin {public partial class Form1: Form {String strImagesPath = Application. startupPath. substring (0, Application. startupPath. substring (0, Application. startupPath. lastIndexOf ("\\")). lastIndexOf ("\"); int top, left, hei, wid; bool bol = false, bo = false, bolTop = false, bolLeft = false, bolRight = false, bolBottom = false, bolLeftCornu = false, bolRightCornu = false; int x = 0, y = 0; public Form1 () {InitializeComponent () ;}// close the event. Private void picClose_Click (object sender, System. EventArgs e) {Application. Exit ();} // maximize event. Private void picMaximize_Click (object sender, System. EventArgs e) {if (! Bol) // if the form is in the normal state {top = this. top; // obtain the Top attribute value of the form left = this. left; // obtain the Left attribute value of the form. hei = this. height; // obtain the form's Height attribute value wid = this. width; // obtain the Width attribute value of the form. this. top = 0; // set the Top attribute value of the form to zero. this. left = 0; // set the Left attribute value of the form to zero int hg = SystemInformation. maxWindowTrackSize. height; // obtain the default maximum Height of the window, int wh = SystemInformation. maxWindowTrackSize. width; // obtain the default maximum Width of the window. this. height = hg; // set the form's Height attribute value this. width = wh; // Set the Width attribute value of the form to "bol = true"; // set the form flag to maximize if (menItemSkin1.Checked) // if you select the "purple flower" menu item, set this. picMaximize. image = Image. fromFile (strImagesPath + @ "\ images \ purple \ max.png"); if (menItemSkin2.Checked) // if you select the "Blue classic" menu item, set to maximize the Image attribute this. picMaximize. image = Image. fromFile (strImagesPath + @ "\ images \ blue \ max.png"); if (menItemSkin3.Checked) // if you select the "Green Garden" menu item, set the Image attribute of the maximized Image this. picMaximize. imag E = Image. fromFile (strImagesPath + @ "\ images \ green \ max.png");} else // if the form is maximized {this. top = top; // set the Top attribute value of the form. this. left = left; // set the Left attribute value of the form. this. height = hei; // set the value of the Height attribute of the form. this. width = wid; // set the Width attribute value of the form to "bol = false"; // set the form flag to indicate the normal state if (menItemSkin1.Checked) this. picMaximize. image = Image. fromFile (strImagesPath + @ "\ images \ purple \ max_Normal.png"); if (menItemSkin2.Checked) this. picMaxi Mize. image = Image. fromFile (strImagesPath + @ "\ images \ blue \ max_Normal.png"); if (menItemSkin3.Checked) this. picMaximize. image = Image. fromFile (strImagesPath + @ "\ images \ green \ max_Normal.png") ;}// minimize the event. Private void picMinimize_Click (object sender, System. eventArgs e) {top = this. top; left = this. left; hei = this. height; wid = this. width; this. height = 0; this. width = 0; bo = true;} // when the window is activated again. Private void Form_Activated (object sender, System. eventArgs e) {if (bo) {this. top = top; this. left = left; this. height = hei; this. width = wid; bo = false ;}// select disable. Private void mItemColse_Click (object sender, System. EventArgs e) {Application. Exit () ;}// move the cursor over the upper border. Private void panel_Top_MouseDown (object sender, System. windows. forms. mouseEventArgs e) {x = e. x; y = e. y; this. bolTop = true;} // move the cursor over the border. Private void panel_Top_MouseMove (object sender, System. windows. forms. mouseEventArgs e) {if (bolTop) {this. top + = e. y-y; this. left + = e. x-x ;}/// release the cursor over the upper border. Private void panel_Top_MouseUp (object sender, System. Windows. Forms. MouseEventArgs e) {this. bolTop = false ;}// press the left border with the mouse. Private void panel_Left_MouseDown (object sender, System. Windows. Forms. MouseEventArgs e) {x = e. X; this. bolLeft = true;} // move the cursor over the left border. Private void panel_Left_MouseMove (object sender, System. windows. forms. mouseEventArgs e) {if (bolLeft) {this. width + = x-e. x; this. left + = e. x-x ;}/// move the cursor from the Left Border. Private void panel_Left_MouseUp (object sender, System. Windows. Forms. MouseEventArgs e) {this. bolLeft = false ;}// press the right border with the mouse. Private void panel_Right_MouseDown (object sender, System. Windows. Forms. MouseEventArgs e) {x = e. X; this. bolRight = true;} // move the cursor over the right border. Private void panel_Right_MouseMove (object sender, System. windows. forms. mouseEventArgs e) {if (bolRight) {this. width + = e. x-x ;}/// move the cursor away from the right border. Private void panel_Right_MouseUp (object sender, System. Windows. Forms. MouseEventArgs e) {this. bolRight = false ;}// press the mouse on the lower border. Private void panel_Bottom_MouseDown (object sender, System. Windows. Forms. MouseEventArgs e) {y = e. Y; this. bolBottom = true;} // move the cursor over the lower border. Private void panel_Bottom_MouseMove (object sender, System. windows. forms. mouseEventArgs e) {if (bolBottom) {this. height + = e. y-y ;}/// move the cursor off the lower border. Private void panel_Bottom_MouseUp (object sender, System. Windows. Forms. MouseEventArgs e) {this. bolBottom = false ;}// press the mouse in the lower left corner. Private void panelLeftCornu_MouseDown (object sender, System. windows. forms. mouseEventArgs e) {x = e. x; y = e. y; this. bolLeftCornu = true;} // move the cursor in the lower left corner. Private void panelLeftCornu_MouseMove (object sender, System. windows. forms. mouseEventArgs e) {if (bolLeftCornu) {this. width + = x-e. x; this. left + = e. x-x; this. height + = e. y-y ;}// move the cursor away from the lower left corner. Private void panelLeftCornu_MouseUp (object sender, System. windows. forms. mouseEventArgs e) {x = e. x; y = e. y; this. bolLeftCornu = false;} // press the mouse in the lower right corner. Private void panelRightCornu_MouseDown (object sender, System. Windows. Forms. MouseEventArgs e) {this. bolRightCornu = true ;}// move the cursor in the lower right corner. Private void panelRightCornu_MouseMove (object sender, System. windows. forms. mouseEventArgs e) {if (bolRightCornu) {this. width + = e. x-x; this. height + = e. y-y ;}/// move the mouse in the lower right corner. Private void panelRightCornu_MouseUp (object sender, System. windows. forms. mouseEventArgs e) {this. bolRightCornu = false;} private void Form_Load (object sender, EventArgs e) {menItemSkin2_Click (sender, e); // default Blue classic} private void menItemSkin1_Click (object sender, EventArgs e) {// set the BackgroundImage attribute of the Panel control this. panel_Top.BackgroundImage = Image. fromFile (strImagesPath + @ "\ images \ purple \ top.png"); this. panel_Left.BackgroundImage = Image. fromFile (strImagesPath + @ "\ images \ purple \ left.png"); this. panel_Right.BackgroundImage = Image. fromFile (strImagesPath + @ "\ images \ purple \ right.png"); this. panel_Bottom.BackgroundImage = Image. fromFile (strImagesPath + @ "\ images \ purple \ bottom.png"); // you can set this to minimize the Image attribute of the Image control. picMinimize. image = Image. fromFile (strImagesPath + @ "\ images \ purple \ min.png"); if (bol = true) // if the current form is in the maximized state {// set the Image attribute of the maximized Image control this. picMaximize. image = Image. fromFile (strImagesPath + @ "\ images \ purple \ max.png");} else {// sets the Image attribute of the maximize Image control this. picMaximize. image = Image. fromFile (strImagesPath + @ "\ images \ purple \ max_normal.png");} // you can disable the Image attribute this of the Image control. picClose. image = Image. fromFile (strImagesPath + @ "\ images \ purple \ close.png"); // you can specify this when selecting a menu. menItemSkin1.Checked = true; this. menItemSkin2.Checked = false; this. menItemSkin3.Checked = false; // set the background image attribute of the main menu of the form. this. menuStrip1.BackgroundImage = Image. fromFile (strImagesPath + @ "\ images \ purple \ menu.gif"); // set the background image attribute of the form this. backgroundImage = Image. fromFile (strImagesPath + @ "\ images \ purple \ background.gif");} private void menItemSkin2_Click (object sender, EventArgs e) {this. panel_Top.BackgroundImage = Image. fromFile (strImagesPath + @ "\ images \ blue \ top.png"); this. panel_Left.BackgroundImage = Image. fromFile (strImagesPath + @ "\ images \ blue \ left.png"); this. panel_Right.BackgroundImage = Image. fromFile (strImagesPath + @ "\ images \ blue \ right.png"); this. panel_Bottom.BackgroundImage = Image. fromFile (strImagesPath + @ "\ images \ blue \ bottom.png"); this. picMinimize. image = Image. fromFile (strImagesPath + @ "\ images \ blue \ min.png"); if (bol = true) {this. picMaximize. image = Image. fromFile (strImagesPath + @ "\ images \ blue \ max.png");} else {this. picMaximize. image = Image. fromFile (strImagesPath + @ "\ images \ blue \ max_normal.png");} this. picClose. image = Image. fromFile (strImagesPath + @ "\ images \ blue \ close.png"); this. menItemSkin1.Checked = false; this. menItemSkin2.Checked = true; this. menItemSkin3.Checked = false; this. menuStrip1.BackgroundImage = Image. fromFile (strImagesPath + @ "\ images \ blue \ menu.gif"); this. backgroundImage = Image. fromFile (strImagesPath + @ "\ images \ blue \ background.gif");} private void menItemSkin3_Click (object sender, EventArgs e) {this. panel_Top.BackgroundImage = Image. fromFile (strImagesPath + @ "\ images \ green \ top.png"); this. panel_Left.BackgroundImage = Image. fromFile (strImagesPath + @ "\ images \ green \ left.png"); this. panel_Right.BackgroundImage = Image. fromFile (strImagesPath + @ "\ images \ green \ right.png"); this. panel_Bottom.BackgroundImage = Image. fromFile (strImagesPath + @ "\ images \ green \ bottom.png"); this. picMinimize. image = Image. fromFile (strImagesPath + @ "\ images \ green \ min.png"); if (bol = true) {this. picMaximize. image = Image. fromFile (strImagesPath + @ "\ images \ green \ max.png");} else {this. picMaximize. image = Image. fromFile (strImagesPath + @ "\ images \ green \ max_normal.png");} this. picClose. image = Image. fromFile (strImagesPath + @ "\ images \ green \ close.png"); this. menItemSkin1.Checked = false; this. menItemSkin2.Checked = false; this. menItemSkin3.Checked = true; this. menuStrip1.BackgroundImage = Image. fromFile (strImagesPath + @ "\ images \ green \ menu.gif"); this. backgroundImage = Image. fromFile (strImagesPath + @ "\ images \ green \ background.gif");} private void exit ToolStripMenuItem_Click (object sender, EventArgs e) {Application. exit ();} private void panel_Top_DoubleClick (object sender, EventArgs e) {picMaximize_Click (sender, e );}}}