none
mdichild graphics paint won't work

    Question

  • Hi,

    In my application i create multiple panel from my on derived panel class.

    When i try to draw a line in these new panels the sourcecode runs without errors, but no line appears.

    my Form is a mdichild where i have similiar problems when i draw a line direct in the Form load, but when i use the form_paint method the program paints the line.

    my derived panel class has also a paint method which i have overwritten, but no line appears.

    the dawing line source i copied from the MSDN page.

    Thursday, March 23, 2017 3:58 PM

All replies

  •  You are going to need to show us your code or a small example that replicates the exact problem so we can actually see what is wrong.  There should be no problem drawing in an mdi child form.  For example,  below i am using the Protected Overrides OnPaint sub of the mdi child forms to draw an ellipse and two lines on them.

     

     

     


    If you say it can`t be done then i`ll try it

    Thursday, March 23, 2017 5:06 PM
  • Hi PedrodaCostaeg,

    Can you share your code that can reproduce your issue here? So that I can analyze this problems better.
    By the way, you can also refer to this case firstly to check if it is helpful to you.


    Best Regards,
    Cherry Bu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 24, 2017 9:24 AM
    Moderator
  • Sure.

    I startet with

    Private Sub Startform_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.ControlBox = False
        formstartbeendet_xcb = False
        Me.Height = mdimain.Size.Height
        Me.Width = mdimain.Size.Width
    
        'Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red, 5)
        'Dim formGraphics As System.Drawing.Graphics
        'formGraphics = Me.CreateGraphics()
        'formGraphics.DrawLine(myPen, 20, 20, 20, 200)
        'myPen.Dispose()
        'formGraphics.Dispose()
    
    
       Me.Panel1.Height = Me.Height - 190
       Me.Panel1.Width = Me.Width - 22
       Me.Panel1.Controls.Clear()
        Me.MaximizedBounds = SystemInformation.WorkingArea
    
        formstartbeendet_xcb = True
        Urllisteanzeigen()
    End Sub

    This drawline didn't work, therefore i made a comment out of it.

      Private Sub Startform_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Black, 2)
        Dim formGraphics As System.Drawing.Graphics
        formGraphics = e.Graphics
        formGraphics.DrawRectangle(myPen, 0, 116, Me.Width, 1)
        myPen.Dispose()
        formGraphics.Dispose()
      End Sub

    This drawRectabgle works just fine.

    why the drawline in startform_load didn't work, i couldn't say.

    Then i added a new class

    Imports System
    Imports System.Diagnostics
    Imports System.ComponentModel
    Imports System.Windows.Forms
    Imports System.Drawing
    Imports System.Drawing.Drawing2D
    
    
    Public Class ShapedPanel
       Inherits Panel
    
       Private pen As Pen = New Pen(_borderColor, penWidth)
       Private Shared ReadOnly penWidth As Single = 3.0F
    
       Public Sub New()
    
       End Sub
    
       Private _borderColor As Color = Color.Black
       <Browsable(True)> _
       Public Property BorderColor() As Color
          Get
             Return _borderColor
          End Get
          Set(ByVal Value As Color)
             _borderColor = Value
             pen = New Pen(_borderColor, penWidth)
             Invalidate()
          End Set
       End Property
        Private Sub PaintMDI(ByVal sender As Object, ByVal e As PaintEventArgs)
    
            'Dim gr As Graphics = Me.Controls(Me.Controls.Count - 1).CreateGraphics
    
            Dim rect As Rectangle = New Rectangle(0, 0, Me.ClientRectangle.Width, Me.ClientRectangle.Height)
            Dim lBrush As LinearGradientBrush = New LinearGradientBrush(rect, Color.Blue, Color.Green, LinearGradientMode.Vertical)
            e.Graphics.FillRectangle(lBrush, rect)
    
            e.Graphics.DrawString("This is a test", Me.Font, Brushes.Red, 100, 100)
    
    
        End Sub
       Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
          MyBase.OnPaint(e)
          ExtendedDraw(e)
          'DrawBorder(e.Graphics)
                e.Graphics.DrawArc(pen, New Rectangle(0, 0, Edge, Edge), _
                           180, 90)
          e.Graphics.DrawArc(pen, New Rectangle(Width - Edge - 1, -1, _
                           Edge, Edge), 270, 90)
          e.Graphics.DrawArc(pen, New Rectangle(Width - Edge - 1, _
                           Height - Edge - 1, Edge, Edge), 0, 90)
          e.Graphics.DrawArc(pen, New Rectangle(0, Height - Edge - 1, _
                           Edge, Edge), 90, 90)
          e.Graphics.DrawRectangle(pen, 0.0F, 0.0F, CType((Width - 1), _
                                 Single), CType((Height - 1), Single))
          'DrawSingleBordernew(e)
       End Sub
       Protected Overrides Sub OnMouseHover(ByVal e As System.EventArgs)
          Cursor = Cursors.Hand
          MyBase.OnMouseHover(e)
        End Sub
       Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
          Cursor = Cursors.Default
          MyBase.OnMouseLeave(e)
        End Sub
       Private _edge As Integer = 50
       <Browsable(True)> _
       Public Property Edge() As Integer
          Get
             Return _edge
          End Get
          Set(ByVal Value As Integer)
             _edge = Value
             Invalidate()
          End Set
       End Property
    
    
       Private Function GetLeftUpper(ByVal e As Integer) As Rectangle
          Return New Rectangle(0, 0, e, e)
       End Function
    
       Private Function GetRightUpper(ByVal e As Integer) As Rectangle
          Return New Rectangle(Width - e, 0, e, e)
       End Function
    
       Private Function GetRightLower(ByVal e As Integer) As Rectangle
          Return New Rectangle(Width - e, Height - e, e, e)
       End Function
    
       Private Function GetLeftLower(ByVal e As Integer) As Rectangle
    
    
          Return New Rectangle(0, Height - e, e, e)
       End Function
    
       Private Sub ExtendedDraw(ByVal e As PaintEventArgs)
          e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
          Dim path As GraphicsPath = New GraphicsPath()
          path.StartFigure()
          path.StartFigure()
          path.AddArc(GetLeftUpper(Edge), 180, 90)
          path.AddLine(Edge, 0, Width - Edge, 0)
          path.AddArc(GetRightUpper(Edge), 270, 90)
          path.AddLine(Width, Edge, Width, Height - Edge)
          path.AddArc(GetRightLower(Edge), 0, 90)
          path.AddLine(Width - Edge, Height, Edge, Height)
          path.AddArc(GetLeftLower(Edge), 90, 90)
          path.AddLine(0, Height - Edge, 0, Edge)
          path.CloseFigure()
          Region = New Region(path)
       End Sub
    
       Private Sub DrawSingleBorder(ByVal graphics As Graphics)
          graphics.DrawArc(pen, New Rectangle(0, 0, Edge, Edge), _
                           180, 90)
          graphics.DrawArc(pen, New Rectangle(Width - Edge - 1, -1, _
                           Edge, Edge), 270, 90)
          graphics.DrawArc(pen, New Rectangle(Width - Edge - 1, _
                           Height - Edge - 1, Edge, Edge), 0, 90)
          graphics.DrawArc(pen, New Rectangle(0, Height - Edge - 1, _
                           Edge, Edge), 90, 90)
          graphics.DrawRectangle(pen, 0.0F, 0.0F, CType((Width - 1), _
                                 Single), CType((Height - 1), Single))
       End Sub
          Private Sub DrawSingleBordernew(ByVal e As PaintEventArgs)
          e.Graphics.DrawArc(pen, New Rectangle(0, 0, Edge, Edge), _
                           180, 90)
          e.Graphics.DrawArc(pen, New Rectangle(Width - Edge - 1, -1, _
                           Edge, Edge), 270, 90)
          e.Graphics.DrawArc(pen, New Rectangle(Width - Edge - 1, _
                           Height - Edge - 1, Edge, Edge), 0, 90)
          e.Graphics.DrawArc(pen, New Rectangle(0, Height - Edge - 1, _
                           Edge, Edge), 90, 90)
          e.Graphics.DrawRectangle(pen, 0.0F, 0.0F, CType((Width - 1), _
                                 Single), CType((Height - 1), Single))
       End Sub
       Private Sub Draw3DBorder(ByVal graphics As Graphics)
          'TODO Implement 3D border
       End Sub
    
       Private Sub DrawBorder(ByVal graphics As Graphics)
          DrawSingleBorder(graphics)
       End Sub
    End Class

    As you can see it should make a new panel with round edges a make also a line around the new panel automaticaqlly.

    But this doesn't work at all.I tried everything i could think of and search the web for a solution.

    I hope this explains my problem better.

    thx


    Tuesday, March 28, 2017 10:56 AM
  • This drawline didn't work, therefore i made a comment out of it.

    It did work, but the next time the form was drawn (which was probably right at the end of the form load method) then anything already drawn was overwritten. The solution of putting the drawing code in the paint event is correct, because that event occurs whenever the form is redrawn.

    Tuesday, March 28, 2017 11:28 AM
  • That's a very good Answer thanks

    Still my problem with shapedpanel remains.

    this line isn't drawn and it is in paint declared, so there must be still another problem.

    Tuesday, March 28, 2017 12:33 PM
  • Still my problem with shapedpanel remains.

    this line isn't drawn and it is in paint declared, so there must be still another problem.

    Which line doesn't get drawn?  You seem to have a lot of code in that class which has nothing to do with drawing.  That example is not the same as the previous case, because you are overriding OnPaint rather than using the Paint event. However, the main problem is that you are drawing with an invisible pen.  You need to set the pen color to an actual value that exists at the time the class in created. Has this code been converted from C#?
    Tuesday, March 28, 2017 8:54 PM
  • Like i said before the border of the new Panel .

         'DrawBorder(e.Graphics)
                e.Graphics.DrawArc(pen, New Rectangle(0, 0, Edge, Edge), _
                           180, 90)
          e.Graphics.DrawArc(pen, New Rectangle(Width - Edge - 1, -1, _
                           Edge, Edge), 270, 90)
          e.Graphics.DrawArc(pen, New Rectangle(Width - Edge - 1, _
                           Height - Edge - 1, Edge, Edge), 0, 90)
          e.Graphics.DrawArc(pen, New Rectangle(0, Height - Edge - 1, _
                           Edge, Edge), 90, 90)
          e.Graphics.DrawRectangle(pen, 0.0F, 0.0F, CType((Width - 1), _
                                 Single), CType((Height - 1), Single))
          'DrawSingleBordernew(e)
    The source code of
    DrawSingleBordernew(e) and 
    DrawBorder(e.Graphics)

    Are the same, because ive tried to understand why teh border was drawn.

    Wednesday, March 29, 2017 2:20 PM
  • The source code of
    DrawSingleBordernew(e) and 
    DrawBorder(e.Graphics)

    Are the same, because ive tried to understand why teh border was drawn.

    The code might be the same, but the way it is being invoked is quite different.   Your new code does not solve the problem - you have still not initialised the pen colour to a visible color.  You will not see the border while the pen colour is invisible.

    Wednesday, March 29, 2017 8:58 PM