Redraw your form with the SetStyle method

Source: Internet
Author: User
Tags resource visual studio
A little-known SetStyle method allows you to control how a form is redrawn.
by Ken Getz
Note: Ken Getz, along with Brian Randell on Orlando's vslive!, held a seminar on the theme "Build a Rich-Client App with Visual Studio. NET". This tip is selected from the seminar's information.
Using GDI + and Windows Forms, we can easily create a gradient color (gradient) to populate an area. Using the simple method provided by the. NET Framework, you can create linear gradients (line-style gradient fills) or path gradients (paths gradient fills). The real problem, however, is that these complex background graphics are resource-intensive (resource-intensive). Recently, I am doing a mock clock demo program, with a gradient color to fill the clock face. Every second, when the clock redraw its clock face to show the correct position of the clock pointer, it also redrawn the entire background gradient. Even on a fast machine, this method is not very good. I'll introduce you to the SetStyle method, which allows you to specify how and when to redraw your form.
First, use the following code to do the experiment, you can get some fun coloring transformation. Modify the Paint event for a new form so that it contains the code:
Private Sub Frmmain_paint (_ ByVal sender as Object, _ ByVal E as PaintEventArgs) _ Handles Mybase.paint  dim PA Th as New GraphicsPath ()   dim pt as New PointF ()   dim RCT as Rectangle = me.clientrectangle &nbsp ;p Ath. AddRectangle (RCT)   dim PGB as New PathGradientBrush (path)   pt = new PointF (_    CType (ME.CLIENTSIZE.WIDTH/2, single), _   ctype (ME.CLIENTSIZE.HEIGHT/2, single))   PGB. CenterPoint = Pt  dim Colors () as Color = _   {color.red, Color.orange, Color.yellow, _ & Nbsp; color.green, _   color.blue, Color.indigo, Color.violet}  dim Positions () as Single = _   {0.0, 0.1, 0.2, 0.4, 0.6, 0.8, 1}  dim cb as ColorBlend = New colorblend ()  &nbs P;CB. Colors = COLORS  CB. positions = POSITIONS  PGB. Interpolationcolors = Cb  e.graphics.fillrectangle (PGB, RCT)   dim F as New StringFormat ()   f.alignment = stringalignment.center  e.graphics.drawstring (_    date.now.tolongtimestring, _   new Font ("Tahoma", +), Brushes.white, PT, f) End Sub

There's a lot of code here, but they're simple. The code creates a new GraphicsPath object, adds a rectangle to GraphicsPath that fills the entire form, and then creates a Gradientbrush object based on GraphicsPath. The code sets the center point for gradients, creates an array of gradient colors to use, creates an array of locations (the entire range of color gradients), creates a new default ColorBlend object, sets the gradient properties, and then fills the rectangle with gradients.
In addition, add a Timer control to the form, activate it, set the interval to 500 milliseconds, and add the following code to the timer event:
Private Sub Timer1_Tick (_ ByVal sender as System.Object, _ ByVal E As System.EventArgs) _ Handles Timer1.tick Me.invalid Ate () End Sub

Run the program and you can get a nice clock with a color background. Is there a problem? Yes, there are usually problems with the gradient color redraw. And the picture is flashing so much that you have to be wary of its damage to vision. Also, when you try to resize a form, you'll notice that before you force the form to redraw (drag another form onto the form, and then move the previous form.) I do not recommend this approach to end users, and gradients do not adjust themselves.
The workaround is simple: The SetStyle method of a form lets you set different values that affect how the form is redrawn. You can set the Resizedraw style (style) to true so that when you resize the form, the form is automatically redrawn. You can also set the DoubleBuffer style to true so that the entire form will not be completely redrawn each time a part of the form is redrawn. This option takes up additional memory because of the. NET runtime, a copy of a form picture must be stored in memory in addition to the actual form itself consuming memory, but the result is well worth it. (To use the DoubleBuffer option, you must also set the AllPaintingInWmPaint and UserPaint styles to true.) To add the following code to your form's Load event, you can complete this setting:
Me.setstyle (Controlstyles.resizeredraw, True) Me.setstyle (controlstyles.allpaintinginwmpaint _ Or Controlstyles.userpaint Or _ Controlstyles.doublebuffer, True)

Rerun the demo, and you'll see that you've got a resized, attractive, digital clock filled with gradients. For more information about this useful method, see the SetStyle method in the. NET framework help file.

about the Author:
Ken Getz is a senior advisor at MCW Technologies, whose time is spent mainly on programming, writing books and training. Ken has written a lot of technical books, including ASP.net Developer's JumpStart with Paul D. Sheriff. His contact method is keng@mcwtech.com.


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.