Results 1 to 5 of 5

Thread: [RESOLVED] Offsetting a HatchBrush

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Resolved [RESOLVED] Offsetting a HatchBrush

    Hi,

    This is a followup to this thread, where I'm trying to draw two sets of dots using HatchBrushes to create VS2010-like interface.

    If you take a look at the zoomed image of the IDE background (blue with dark and light blue dots): that is what I'm trying to draw using GDI+.

    In the previous thread ForumAccount helped me find the correct HashStyle to use (Percent20), but there is a problem: I need to draw two sets of these hatchstyles: once with a slightly darker color and once with a slightly lighter color, at an offset of one pixel downwards.

    Well, no problem I thought, I'll just transform the graphics object (TranslateTransform) one pixel down before drawing the second set of dots.
    But nope, that doesn't work. It seems the HatchBrush doesn't care what orientation the axis of the Graphics object are at (even rotated, scaled in one direction, or as I need: translated), the dots are always drawn in the exact same way, at the exact same spot.

    This is a serious problem, because now I don't know of any way to offset the second set of dots... Is this even possible at all? I tried googling, but I got two results for WPF, and the third result was my own post...

    This must be possible without having to draw them all manually? I don't fancy doing that, that would probably be extremely slow.

    Is there perhaps some way to define my own hatch style and use that?

  2. #2
    Lively Member Blupig's Avatar
    Join Date
    Apr 2008
    Posts
    118

    Re: Offsetting a HatchBrush

    I don't know a lot about drawing, but maybe you should look into something a little more complex like DirectX, which is made to handle complicated drawing.

  3. #3
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Offsetting a HatchBrush

    Can't you define a new rectangle that's slightly smaller & is offset?
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Offsetting a HatchBrush

    Quote Originally Posted by Blupig View Post
    I don't know a lot about drawing, but maybe you should look into something a little more complex like DirectX, which is made to handle complicated drawing.
    I'm pretty sure I can't use DirectX drawing to draw a control.

    Quote Originally Posted by JuggaloBrotha View Post
    Can't you define a new rectangle that's slightly smaller & is offset?
    Nope, I tried but the hatch pattern seems relative to the screen and not the rectangle I'm drawing one. In fact, if you let that rectangle move using a timer, then the pattern doesn't move. Try running this with the timer set to 50 ms or something:
    Code:
        Private i As Integer = 0
    
        Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    
            Dim rect = New Rectangle(i, 50, 50, 50)
    
            'Background
            Using backBrush As New SolidBrush(Color.Red)
                e.Graphics.FillRectangle(backBrush, rect)
            End Using
    
            'First dots
            Using dot1Brush As New Drawing2D.HatchBrush(Drawing2D.HatchStyle.Percent20, Color.White, Color.Transparent)
                e.Graphics.FillRectangle(dot1Brush, rect)
            End Using
    
            'Second dots (offset one pixel)
            rect.Offset(0, -1)
            Using dot2Brush As New Drawing2D.HatchBrush(Drawing2D.HatchStyle.Percent20, Color.Black, Color.Transparent)
                e.Graphics.FillRectangle(dot2Brush, rect)
         End Using
    
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            i += 1
            Me.Invalidate()
        End Sub
    You can see the red square moving to the right but the pattern remains absolutely fixed in place (though it's only visible inside the red square).

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Offsetting a HatchBrush

    Ok I finally found the solution. All you need to do is change the RenderingOrigin property of the Graphics object. I shift it up one pixel (by assigning a New Point(0, -1)) and the hatch pattern shifts with it.

    Resolved!

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