Results 1 to 12 of 12

Thread: [2005] Prevent control.paint from flickering

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2007
    Posts
    87

    [2005] Prevent control.paint from flickering

    Well as the title says,

    I am creating an app for a touchscreen wich is always 1024x786(well not like that is important but ok). I wanted a fancy looking interface so I decided to paint every button and control myself.

    To make it full screen I just used a maximized form with no borderstyle(and the esc button to exit). By using the graphics from the paint event I paint my buttons and such things. Now I made a simulation of the numpad and when I press the buttons the form paints them as a string on the screen.

    This all works fine except for the fact that I cant actualy see the form repaint when I call form.refresh(). Ofcourse I dont want to repaint the whole form but only that part where I have to repaint my changed text, are there ways to solve this or should I rewrite my program with DirectX/DirextDraw?

    Greets

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Prevent control.paint from flickering

    If you only want to repaint part of a form or control then you should call Invalidate first to specify the area to redraw, then call Update. Note that you can call Invalidate multiple times before calling Update so you can redraw multiple discrete sections.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: [2005] Prevent control.paint from flickering

    Don't draw the text directly to the screen. There are many controls you could use for the following but one suggestion would be to place a button on the form where the text is to be placed. Set the Button's BorderSize = 0 and the FlatStyle = Flat.

    Then set the Button's text always equal to the output from your numpad.

    If you don't like the Button idea you can use other controls like a panel.

    If you don't want to use the text portion of the actual control, then choose a control on which you can paint and use graphics as you are now to draw on the control.

    The net effect? An easy way to limit the area of the screen that is drawn on when you update the output from the numpad.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2007
    Posts
    87

    Re: [2005] Prevent control.paint from flickering

    thnx both but I think jmcilhinney`s solutions will suit me more, the reason for that is that I just dont wont to use common controls(I use PNG images for my buttons, these are way nicer then the common controls). But a question to jmcilhinney now.

    Your idea seems good but how do I actualy redraw in that area, all my code is inside(or called within) the paint event. If I update all my code will be exequted again right but only that specified area will be redraw?
    Your solution works btw and therefor this thread is solved.
    (Dude you realy know to much hehehe).

    Thnx again and greets

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Prevent control.paint from flickering

    You don't change your Paint event handler at all. It must contain the logic to draw everything. That's not the slow part. It's actually pushing the pixels to the screen that's the slow part. Basically your Paint event handler "draws" the entire picture, then the system decides what parts of that to actually push out to the screen based on what parts have been invalidated.

    You should also note that you can still use Button controls, even if you want to draw them with custom Images. I've never done it myself so I don't know all the details, but basically you have to define a class and inherit the Button class. This gives you all the functionality of a button for free, so you just have to change how it looks. You would set its Region property to the bounds defined by the size and shape of the image, then you'd draw the image onto the control surface. The user will see your custom image and it will behave just like a normal Button with no extra effort from you. You should read about authoring custom controls on MSDN. I'm sure there are tutorials for creating custom buttons all over the Web too, as it's not an uncommon thing to do.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Prevent control.paint from flickering

    you know you can just use standard button controls and assign a PNG to them as their background image right? No custom drawing involved. If the PNG has the text the button should have, then simply set the buttons text property to an empty string.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Prevent control.paint from flickering

    Quote Originally Posted by kleinma
    you know you can just use standard button controls and assign a PNG to them as their background image right? No custom drawing involved. If the PNG has the text the button should have, then simply set the buttons text property to an empty string.
    In that case you'd want to set the FlatStyle to Flat and the FlatAppearance.BorderSize to 0. The Button will still occupy a rectangular space though, so if your image is a special shape then you will need to inherit and set a custom Region.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2007
    Posts
    87

    Re: [2005] Prevent control.paint from flickering

    Somehow using PNG`s as background image dont works good.

    It works all pretty good as long as your buttons are the shape of a rectangle.
    The reason I used PNG`s is of their abbility to hold alpha. When I paint a PNG image in the paint event of the button(or use it as backgroudimage) the alpha dont work. Not even when I set the background color to alpha.

    An example:


    Here you see the difference in drawing on the panel/form or inside the button.

    This is why I didnt want to use controls. Since when I have some sort of background it will get overpainted.


    Greets

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Prevent control.paint from flickering

    I was under the impression that I had already mentioned twice that you could inherit the Button control and make it any shape you wanted. Am I wrong?

    http://www.bobpowell.net/shapedcontrols.htm
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Lively Member
    Join Date
    May 2007
    Posts
    87

    Re: [2005] Prevent control.paint from flickering

    Well I tried that but what I did was just this:

    My inherited class:

    vb Code:
    1. Public Class MyButton
    2.     Inherits Button
    3.     Private myImage As Image
    4.  
    5.     Public Sub New(ByVal img As String, ByVal x As Integer, ByVal y As Integer)
    6.         MyBase.New()
    7.         myImage = Drawing.Image.FromFile(img)
    8.         Me.Bounds = New Rectangle(x, y, myImage.Width, myImage.Height)
    9.         Me.FlatStyle = Windows.Forms.FlatStyle.Flat
    10.         Me.FlatAppearance.BorderSize = 0
    11.         Me.FlatAppearance.CheckedBackColor = Color.Transparent
    12.         Me.FlatAppearance.MouseOverBackColor = Color.Transparent
    13.         Me.BackColor = Color.Transparent
    14.     End Sub
    15.  
    16.     Private Sub iPaint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Me.paint
    17.         Dim g As Graphics = e.Graphics
    18.         g.DrawImage(myImage, 0, 0)
    19.  
    20.     End Sub
    21.  
    22.  
    23.  
    24. End Class

    I got no idea how to remove that "white space" from the shape.

    Greets

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Prevent control.paint from flickering

    This is from post #5:
    You would set its Region property to the bounds defined by the size and shape of the image
    This is from post #7:
    The Button will still occupy a rectangular space though, so if your image is a special shape then you will need to inherit and set a custom Region.
    I don't see in that code where you're setting the Region property to the shape of your round image.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Lively Member
    Join Date
    May 2007
    Posts
    87

    Re: [2005] Prevent control.paint from flickering

    Ok now I guess I did it the right way.

    It looks like this now:


    Its pretty ugly but ok.

    vb Code:
    1. Public Class MyButton
    2.     Inherits Button
    3.     Private myImage As Image
    4.  
    5.     Public Sub New(ByVal img As String, ByVal x As Integer, ByVal y As Integer)
    6.         MyBase.New()
    7.         myImage = Drawing.Image.FromFile(img)
    8.         Me.Bounds = New Rectangle(x, y, myImage.Width, myImage.Height)
    9.         Me.FlatStyle = Windows.Forms.FlatStyle.Flat
    10.         Me.FlatAppearance.BorderSize = 0
    11.         Me.FlatAppearance.CheckedBackColor = Color.Transparent
    12.         Me.FlatAppearance.MouseOverBackColor = Color.Transparent
    13.         Me.BackColor = Color.Transparent
    14.         Me.Region = New System.Drawing.Region()
    15.  
    16.     End Sub
    17.  
    18.     Private Sub iPaint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Me.paint
    19.         Dim g As Graphics = e.Graphics
    20.         g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    21.         g.DrawImage(myImage, 0, 0)
    22.         '  g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
    23.         Dim buttonPath As New System.Drawing.Drawing2D.GraphicsPath
    24.  
    25.  
    26.         Dim newRectangle As Rectangle = Me.ClientRectangle
    27.  
    28.  
    29.  
    30.  
    31.         buttonPath.AddEllipse(newRectangle)
    32.         e.Graphics.DrawPath(Pens.Black, buttonPath)
    33.         Me.Region = New System.Drawing.Region(buttonPath)
    34.  
    35.  
    36.     End Sub
    37.  
    38.  
    39.  
    40. End Class

    With that code.

    It good enough for what it is now but is there anyway to get it as nice as those other 2?

    Greets

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width