News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

Code To Draw on an MDI parent's background

Started by nandagopal, Nov 29, 2008, 10:20 PM

Previous topic - Next topic

nandagopal

The program declares a WithEvents variable of type MdiClient. When the MDI parent form loads, the program looks through its child controls looking for the MdiClient and saves a reference to that client in the WithEvents variable. The program then creates an MDI child form.

When the MdiClient receives a Paint or Resize event, it calls subroutine DrawPicture, passing it a Graphics object on which to draw. In this example, DrawPicture draws an ellipse with an X over it on the MdiClient control.


Private WithEvents m_MdiClient As MdiClient

Private Sub Form1_Load(ByVal sender As Object, ByVal e As _
    System.EventArgs) Handles MyBase.Load
    ' Get the MDIClient.
    For Each ctl As Control In Me.Controls
        If TypeOf ctl Is MdiClient Then
            m_MdiClient = DirectCast(ctl, MdiClient)
            Exit For
        End If
    Next ctl

    ' Display a child form.
    Dim frm As New Form2
    frm.Width = Me.Width \ 2
    frm.Height = Me.Height \ 2
    frm.MdiParent = Me
    frm.Focus()
    frm.Show()
End Sub

Private Sub m_MdiClient_Paint(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.PaintEventArgs) Handles _
    m_MdiClient.Paint
    DrawPicture(e.Graphics)
End Sub

Private Sub m_MdiClient_Resize(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles m_MdiClient.Resize
    Dim gr As Graphics = m_MdiClient.CreateGraphics()
    DrawPicture(gr)
End Sub

Private Sub DrawPicture(ByVal gr As Graphics)
    gr.Clear(m_MdiClient.BackColor)
    Dim thick_pen As New Pen(Color.Aquamarine, 10)
    gr.DrawEllipse(thick_pen, 5, 5, _
        m_MdiClient.ClientSize.Width - 6, _
        m_MdiClient.ClientSize.Height - 6)
    gr.DrawLine(Pens.Blue, 0, 0, _
        m_MdiClient.ClientSize.Width, _
        m_MdiClient.ClientSize.Height)
    gr.DrawLine(Pens.Blue, m_MdiClient.ClientSize.Width, 0, _
        _
        0, m_MdiClient.ClientSize.Height)
End Sub