Draw in MDIContainer won't work

I am trying to use DrawString and DrawRectangle in the interior area of an MDIContainer form. I can use these functions to draw on a regular form but not this Parent form.

In the Paint event of the form I have tried these ways to get the Graphics object:

Dim g As Graphics = Me.CreateGraphics
and
Dim g As Graphics = e.Graphics

Then using one of those objects used the following code:

g.FillRectangle(Brush, 200, 100, 100, 80)
g.DrawString("TestString", Font, Brush, 100, 100)


The code runs and does not throw an exception, but nothing is drawn onscreen.

Am I getting the Graphics object from the wrong place or is it just not possible to draw on that area of an MDIContainer

Walter


Answer this question

Draw in MDIContainer won't work

  • Zcat

    Complete code for Picture in MDI parent:

        Dim PanelImage As Bitmap
        Private WithEvents Myclient As Windows.Forms.MdiClient
        Private Declare Function GetWindow Lib "user32.dll" Alias "GetWindow" (ByVal hWnd As   Integer, ByVal uCmd As Integer) As Integer

        Private Sub YourForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                Static GW_CHILD As Integer = 5
                Static GW_HWNDNEXT As Integer = 2
                Dim hndl As Integer = Me.GetWindow(Me.Handle.ToInt32, GW_CHILD)
                Dim success As Boolean = False
                While Not success
                    Try
                        Myclient = DirectCast(Windows.Forms.MdiClient.FromHandle(New IntPtr(hndl)), MdiClient)
                        success = True
                    Catch
                        hndl = Me.GetWindow(hndl, GW_HWNDNEXT)
                        success = False
                    End Try
                End While
            Catch
                'ignore
            End Try

        End Sub

        Private Sub Myclient_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Myclient.Paint
            Dim g As Graphics = e.Graphics
            Dim img As Image = groupPanelImage
            g.DrawImageUnscaled(img, 10, 10)
            e.Dispose()
        End Sub

        Private Sub Myclient_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Myclient.Resize
            Myclient.Refresh()
        End Sub


    Private Sub MDIForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    PanelImage = CType(Bitmap.FromStream(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Logo.gif")), Bitmap)
                groupPanelImage.MakeTransparent(Color.White)

    End Sub

    Dotnet67

  • GarethD25

    Hi Walter,

    Drawing on the MDIContainer form is not as simple as that.
    This is because an MDI Container form actually uses an internal form on which the MDI Client forms are 'hosted'.

    You need to do a little more plumbing if you want to paint something there.

    There's a nice article about that on the vbaccelerator site:
    http://www.vbaccelerator.com/home/NET/Code/Libraries/Windows/MDI_Client_Area_Painting/article.asp

    Hope this helps,

    Patrick.

  • Raheel

    you do not need to use api calls to do this..

    you can use this code:



    private void Form1_Load(object sender, System.EventArgs e)
    {
    this.MdiClient.Paint += new PaintEventHandler(MdiClient_Paint);

    }

    //property to get the mdiclient control of a mdi form
    protected MdiClient MdiClient
    {
    get
    {
    foreach (Control c in this.Controls)
    {
    if (c is MdiClient)
    return c as MdiClient;
    }

    return null;
    }
    }

    private void MdiClient_Paint(object sender, PaintEventArgs e)
    {
    e.Graphics.Clear (Color.Red);
    }


    //Roger

  • Draw in MDIContainer won't work