//</snippet3> // The following code example demonstrates the Clear method. // This example is designed to be used with Windows Forms. // Create a form that contains a Button named Button4. // Paste the code into the form and associate // the Button4_Click method with the button's Click event. //<snippet4> private void Button4_Click(System.Object sender, System.EventArgs e) { Graphics buttonGraphics = Button4.CreateGraphics(); buttonGraphics.Clear(Button4.BackColor); buttonGraphics.Dispose(); }
//</snippet2> // The following code example demonstrates how to create a pen // and set its DashStyle property. // This example is designed to be used with Windows Forms. Create // a form that contains a Button named Button3. Paste the code into the // form and associate the Button3_Click method with the button's // Click event. //<snippet3> private void Button3_Click(System.Object sender, System.EventArgs e) { Graphics buttonGraphics = Button3.CreateGraphics(); Pen myPen = new Pen(Color.ForestGreen, 4.0F); myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot; Rectangle theRectangle = Button3.ClientRectangle; theRectangle.Inflate(-2, -2); buttonGraphics.DrawRectangle(myPen, theRectangle); buttonGraphics.Dispose(); myPen.Dispose(); }
private void resizeButton(Button button) { Graphics g = button.CreateGraphics(); try { Point p = button.Location; int width = button.Width; SizeF size = g.MeasureString(button.Text, button.Font); button.Width = (int)Math.Ceiling(size.Width) + 20; if ((button.Anchor & AnchorStyles.Right) == AnchorStyles.Right) { button.Left = p.X - (button.Width - width); } } finally { g.Dispose(); } }
// Makes a button with the specified text internal static Button MakeButton(String text) { Button b = new Button(); Graphics g = b.CreateGraphics(); SizeF size = g.MeasureString(text, b.Font); b.Height = BUTTON_HEIGHT; int proposedWidth = (int)(size.Width + BUTTON_WIDTH_PAD); if (b.Width < proposedWidth) b.Width = proposedWidth; b.Text = text; g.Dispose(); return b; }