C # make irregular windows (24bit Color is applicable)

zhaozj2021-02-08  220

C # making irregular window (above 24bit Color applicable) Time: 2004/3/17 Author: Robert Reference: http://www.codeproject.com/csharp/bmprgnform.asp?target=region Email: zsc771120@yahoo.com. CN Keywords: Region GIF IUFE FORM Button Picture Form Button Purpose: People who are troubled by C # irregular windows describe how to make pictures and forms. Region technology can not only do irregular windows, but also Do irregular control appearance, such as an irregular button. Program introduction: I modify the comment in the program, do not modify the list of programs. After all, the E text did not have a more comfortable .1. Primary function explains the following procedure The two main functions generate bitmap regionss, source code is placed in BitmapRegion.cs, you can import this category directly, use the function in it. // Create and Apply the Given Bitmap Region on the support Control // Generate Support bitmap region control public static void createControlREgion (Control Control, Bitmap Bitmap) {// If the control or bitmap does not exist, it returns directly. IF (Control == Null || Bitmap == Null) Return; / / Depending on the bitmap size control size control.width = bitmap.width; control.Height = bitmap.Height; // Process Form Category IF (Control Is System.Windows.Forms.form) {// Forced Conversion Control Object to Form Object Form Form = (Form) Control; // Due to our FORM boundary type (form.formorderstyle) is not None, / / So our FORM size is bigger than the bitmap of Form.Width = 15; form.height = 35; // Setting the Form Border type is none form.formorderstyle = formborderstyle.none; // Setting form background image form.backgroundImage = Bitmap; // Calculate the boundary of opaque sections in the picture (recommended with GIF pictures) graphicsPath graphicsPath = CalculateControlGraphicsPath (Bitmap); // Establishing the region Form.Region = New Region (graphicspath);} // Processing button (Button Category) ELSE IF (Control ISystem.windows.Forms.Button) {// Control Object Transmends to Button Object Category Button Button = (Button) Control; // Remove the text button .Text = ""

/ / Change the mouse style is a hand-shaped mouse button.cursor = cursors.hand; // Set the background pattern Button.BackgroundImage = Bitmap; // Calculate the boundary of the opaque part in the picture (recommended with GIF images) GraphicsPath GraphicsPath = CalculateControlgraphicsPath (Bitmap) ; // Establishing the region button.region = New region (graphicspath);} // Here you can imitate the category of Form or Button to build a heart, such as Panel} // Calculate the picture opaque zone Return to GraphicsPath Private Static GraphicsPath CalculateControlGraphicsPath (Bitmap Bitmap) {// Established GraphicsPath, calculates our bitmap path to use GraphicsPath GraphicsPath = new graphicspath (); // Using the upper left corner (0) point as transparent color // If this is red, then we Calculation is the picture does not contain the red area path color color color color color color color color color color (0, 0); // Store the first opaque point, this value determines that we start checking the opaque zone. Int colopaquepixel = 0; // Check all lines (Y axis) for (int Row = 0; ROW

// Add an opaque zone to our GraphicsPath GraphicsPath.AddRectangle (New Rectangle (ColoPaquepixel, Row, ColNext - Colopaquepixel, 1)); // Do not search for opaque points col = colnext;}}} // Returns the calculated opaque Image Path Return GraphicsPath;} Produced under irregular forms can produce irregular graphics. You don't have to change form.formorderstyle is none, the program helps you do this .public class form1: system.Windows.Forms. Form {// Imported Background Image Private Bitmap Bmpfrmback = New Bitmap (TypeOf (Form1), "Back.BMP"); Public Form1 () {InitializeComponent (); // Let your bed transparent BitmapRegion.createControlRolRegion (this, BMPFRMBACK);}} Generate irregular buttons and generate irregular buttons in the same way as the irregular button PUBLIC CLASS FORM1: System.Windows.Forms.form {// Import Form Background Image Private Bitmap Bmpfrmback = New Bitmap (Typeof (Form1) , "back.bmp"); // Import button Background Image Private Bitmap BmpBob = New Bitmap (TypeOf (Form1), "Bob.BMP"); public form1 () {initializeComponent (); // Generate an irregular form BitmapRegion .CreateControlre Gion (this, bmpfrmback); // Generate irregular buttons BitmapRegion.createControlRegion;}} If you want to display different irregular buttons when you leave the button, you only need MouseEleave and MouseEnter on the button. the following code written inside it .private void button1_MouseEnter (object sender, System.EventArgs e) {// irregular button BitmapRegion.CreateControlRegion (button1, bmpBobSay);} private void button1_MouseLeave (object sender, System.EventArgs e ) {/ Generating irregular buttons BitmapRegion.createControlRolion (Button1, BmpB);

} Mobile irregular window does not have a title bar because of the irregular window? How to move? The following code gives you the answer, the code is very simple, I will not explain Chinese explained (I am a Chinese part is an irregular form) Private void Form1_Mousemove object sender, System.Windows.Forms.MouseEventArgs e) {// Check if dragging of the form has occurred if (e.Button == MouseButtons.Left) {// If this is the first mouse move event for left click dragging / / of the form, store the current point clicked so that we can use // it to calculate the form's new location in subsequent mouse move // ​​events due to left click dragging of the form if (isFirst == true) {// Store previous left click position prevLeftClick = new Point (eX, eY); // Subsequent mouse move events will not be treated as first time, // until the left mouse click is released or other mouse click // occur isFirst = false;} / / On subsequent Mouse Move Events with left mouse click Down. // (IE . During dragging of form) else {// This flag here is to do alternate processing for the form // dragging because it causes serious flicking when u allow // every such events to change the form's location. // You can try commenting this Out to see what i mean if (Toblock == false) this.location = new point (this.location.x ex - prevleftClick.x, this.location.y Ey - prevleftclick.y); // store new previous Left Click Position PrevLeftClick = New Point (EX, EY); // Allow or Deny Next Mouse Move Dragging Event Toblock =! Toblock

}} // This is a new mouse move event so reset flag else isFirst = true;} completed program list BitmapRegion.csusing System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace BitmapRegionTest { ///

/// Summary description for bitmapregion. /// public class bitmapregion {public bitmapregion () {} /// /// Create and Apply the region on the support control / // /// The control object to apply the region to /// The bitmap Object to create the region from public static void CreateControlRegion (Control control, Bitmap bitmap) {// return if control and bitmap are null if (control == null || bitmap == null) return; // Set our control's size to be the same as the Bitmap control.width = bitmap.width; control.height = bitmap.Height; // Check if we are dealing with form here if (control is system.windows.forms.form) {// CAS T TO A FORM OBJECT FORM FORM = (form) Control; // setur form's size to be a little larger That The Bitmap Just // In Case The Form's Border Style Is Not set to none in the first place form.width = 15; form.Height = 35; // No border form.FormBorderStyle = FormBorderStyle.None; // Set bitmap as the background image form.BackgroundImage = bitmap; // Calculate the graphics path based on the bitmap supplied GraphicsPath graphicsPath = CalculateControlGraphicsPath (Bitmap); // apply new region form.region = New region;

} // check if we are dealing with button here else if (control is system.windows.forms.button) {// Cast to a button Object Button button = (button) Control; // do not show button text button.text = ""; // Change cursor to hand when over button button.Cursor = Cursors.Hand; // Set background image of button button.BackgroundImage = bitmap; // Calculate the graphics path based on the bitmap supplied GraphicsPath graphicsPath = CalculateControlGraphicsPath ( bitmap); // Apply new region button.Region = new Region (graphicsPath);}} ///

/// Calculate the graphics path that representing the figure in the bitmap /// excluding the transparent color which is the Top Left Pixel. /// /// The Bitmap Object to Calculate Our Graphics Path from /// Calculated graphics path private stat GraphicsPath CalculateControlGraphicsPath (Bitmap Bitmap) {// Create Graphicsp ath for our bitmap calculation GraphicsPath graphicsPath = new GraphicsPath (); // Use the top left pixel as our transparent color Color colorTransparent = bitmap.GetPixel (0, 0); // This is to store the column value where an opaque pixel is First found. // this value will determine where we start scanning for trailing opaque pixels. Int colopaquepixel = 0; // Go through all rows (y axis) for (int Row = 0; ROW

) {// If this is an opaque pixel, mark it and search for anymore trailing behind if (bitmap.GetPixel (col, row) = colorTransparent) {// Opaque pixel found, mark current position colOpaquePixel = col;! // Create another variable to set the current pixel position int colNext = col; // Starting from current found opaque pixel, search for anymore opaque pixels // trailing behind, until a transparent pixel is found or minimum width is reached for (colNext = colOpaquePixel; colNext /// Summary Description for Form1. /// public class form1: system.windows.forms.form {// [DLLIMPORT ("User32.dll")] // private static extern Bool PostMessage (INTPTR HWND) , int msg, long wParam, long lParam); private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4 ; ///

/// Required designer variable. /// <

/ Summary> private System.ComponentModel.Container components = null; // Load our bitmaps private Bitmap bmpFrmBack = new Bitmap (typeof (Form1), "back.bmp"); private Bitmap bmpBob = new Bitmap (typeof (Form1), " bob.bmp "); private Bitmap bmpBobSay = new Bitmap (typeof (Form1)," bobsay.bmp "); private Bitmap bmpSmiles = new Bitmap (typeof (Form1)," smiles.bmp "); private Bitmap bmpSmilesAngry = new Bitmap (typeof (Form1), "smilesangry.bmp"); private Bitmap bmpGreenBtnUp = new Bitmap (typeof (Form1), "greenbtnup.bmp"); private Bitmap bmpGreenBtnDown = new Bitmap (typeof (Form1), "greenbtndown.bmp") Private Bitmap bmpx = new bitmap (TypeOf (form1), "x.bmp"); private bitmap bmpxsmile = new bitmap (TypeOf (form1), "xsmile.bmp"); // To store the location of Previous Mouse Left Click In The Form // SO That We can use it to calculate the new form location dragging private point prevlevLlex; // to determine if it is the first time entry for every dragging of the form private bool isf irst = true; // Acts like a gate to do allow or deny private bool toBlock = true; public Form1 () {// // Required for Windows Form Designer support // InitializeComponent (); // Make our bitmap region for the form BitmapRegion.CreateControlRegion (this, bmpFrmBack); // Make our bitmap regions for the buttons BitmapRegion.CreateControlRegion (button1, bmpBob); BitmapRegion.CreateControlRegion (button2, bmpSmiles); BitmapRegion.CreateControlRegion (button3, bmpGreenBtnUp); BitmapRegion.CreateControlRegion ( Button4, bmpx);} ///

/// clean up any resources being used. /// <

/ Summary> protected override void Dispose (bool disposing) {if (disposing) {if (components = null!) {Components.Dispose ();}} base.Dispose (disposing);} #region Windows Form Designer generated code // /

/// Required method for Designer support - do not modify /// the contents of this method with the code editor /// private void InitializeComponent () {this.button1 = new System.Windows. .Form.Button (); this.Button2 = new system.windows.forms.button (); this.button3 = new system.windows.Forms.Button (); this.button4 = new system.windows.Forms.Button ( ); This.Suspendlayout (); // // button1 // this.button1.location = new system.drawing.point (104, 88); this.button1.name = "button1"; this.button1.tabindex = 0 ; this.button1.Text = "button1"; this.button1.Click = new System.EventHandler (this.button1_Click); this.button1.MouseEnter = new System.EventHandler (this.button1_MouseEnter); this.button1.MouseLe Ave = new system.eventhandler (this.button1_mouseeleave); // // button2 // this.button2.location = new system.drawing.point (328, 80); this.button2.name = "button2"; this. button2.TabIndex = 1; this.button2.Text = "button2"; this.button2.Click = new System.EventHandler (this.button2_Click); this.button2.MouseEnter = new System.EventHandler (this.button2_MouseEnter); This.Button2.MouseLeave = new system.EventHandler (this.button2_mouseeleave); /// Button3 // this.button3.location = new system.drawing.point (184, 200); this.button3.name = "Button3 "

this.button3.TabIndex = 2; this.button3.Text = "button3"; this.button3.Click = new System.EventHandler (this.button3_Click); this.button3.MouseEnter = new System.EventHandler (this.button3_MouseEnter ); This.Button3.Mouseleave = new system.eventhandler (this.button3_mouseeleave); // // Button4 // this.button4.location = new system.drawing.point (344, 232); this.button4.name = "button4"; this.button4.TabIndex = 3; this.button4.Text = "button4"; this.button4.Click = new System.EventHandler (this.button4_Click); this.button4.MouseEnter = new System.EventHandler (this.button4_MouseEnter); this.button4.MouseLeave = new System.EventHandler (this.button4_MouseLeave); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size (5, 13); this.ClientSize = New system.drawing.size (432, 270); this.controls.addrange (new system.windows.forms.control [] {this.button4, this.button3, this.button2, this.button1}; this.name = "form1"; this.text = "form1"; this.mousemove = new system.windows.Forms.MouseEventhandler (this.form1_mousemove); this.ResumeLayout (false);} # EndRegion ///

/// The main entry point for the application. /// [stathread] static void main () {Application.run (new form1 ());} private void button1_mouseenter (Object) Sender, System.EventArgs E) {// make Bitmap Region for Button BitmapRegion.createControlRegion (Button1, BmpBobsay);

} Private void button1_MouseLeave (object sender, System.EventArgs e) {// Make bitmap region for button BitmapRegion.CreateControlRegion (button1, bmpBob);} private void button2_MouseEnter (object sender, System.EventArgs e) {// Make bitmap region for button BitmapRegion.CreateControlRegion (button2, bmpSmilesAngry);} private void button2_MouseLeave (object sender, System.EventArgs e) {// Make bitmap region for button BitmapRegion.CreateControlRegion (button2, bmpSmiles);} private void button3_MouseEnter (object sender, System. EventArgs e) {// Make bitmap region for button BitmapRegion.CreateControlRegion (button3, bmpGreenBtnDown);} private void button3_MouseLeave (object sender, System.EventArgs e) {// Make bitmap region for button BitmapRegion.CreateControlRegion (button3, bmpGreenBtnUp); } Private void button4_mouseenter (Object sender, system.eventargs e) {// make Bitmap Region for Button BitmapRegion.createControlRegion (Button4, Bmpxs) mile);} private void button4_MouseLeave (object sender, System.EventArgs e) {// Make bitmap region for button BitmapRegion.CreateControlRegion (button4, bmpX);} private void button1_Click (object sender, System.EventArgs e) {MessageBox.Show ( "bob");} private void button2_Click (object sender, System.EventArgs e) {MessageBox.Show ( "smiles");} private void button3_Click (object sender, System.EventArgs e) {MessageBox.Show ( "green button ");} Private void button4_click (object sender, system.eventargs e) {MessageBox.show (" EXITING NOW ... "); Close ();} / * protected override void onmousedown (MouseEventArgs E) {IF (e) Button ==

MouseButtons.Left) {const int HTCAPTION = 2; const int WM_NCLBUTTONDOWN = 161; int ix = eX & 0xffff; int iy = eY & 0xffff; long ll = ix | iy << 16; PostMessage (this.Handle, WM_NCLBUTTONDOWN, HTCAPTION ,}}}} * / private void form1_mousemove (Object sender, system.windows.Forms.MouseEventArgs e) {// check if Dragging of the form HAS Occurred if (E.Button == MouseButtons.Left) {//iff this is the first mouse move event for left click dragging of the form, // store the current point clicked so that we can use it to calculate the form's // new location in subsequent mouse move events due to left click dragging of the form if (isFirst == true) {// Store previous left click position prevLeftClick = new Point (eX, eY); // Subsequent mouse move events will not be treated as first time, until the // left mouse click is released or other mouse Click Occur IsFirst = false;} // on subsequent Mouse Move Events with left mouse click down. (During dragging of form) else {// This flag here is to allow alternate processing for dragging the form because it // causes serious flicking when u allow every such events to change the form's location. // You CAN try commenting this out to see what I mean if (Toblock == false) this.location = new point (this.location.x ex - prevleftClick.x, this.location.y Ey - prevLeftClick.y); / / Store new previous left click position prevlevLlex = new point (ex, EY); // allow or deny next mouse move dragging event toblock =! Toblock

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

New Post(0)