Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DrawString on MDI background??

DrawString on MDI background??

Scheduled Pinned Locked Moved C#
jsonhelpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mirano
    wrote on last edited by
    #1

    Now why the hell is this so difficult? I mean, it works perfectly with child windows, but do I really have to go subclassing and calling API only to draw a simpke string on the background of an MDI window? I appreciate any help about this. Thanks. .

    C 1 Reply Last reply
    0
    • M mirano

      Now why the hell is this so difficult? I mean, it works perfectly with child windows, but do I really have to go subclassing and calling API only to draw a simpke string on the background of an MDI window? I appreciate any help about this. Thanks. .

      C Offline
      C Offline
      Chris Jobson
      wrote on last edited by
      #2

      I don't know if there's a "proper" way to do this, but a workround I've found is to draw the text on a bitmap then assign this bitmap to the MDI window's BackgroundImage property. A snag is that you have to re-create the bitmap whenever the window size changes or it gets tiled (actually you probably only need to recreate it if the window gets smaller, but I haven't tested this). For example, putting the following code in the MyMDIForm_Load and the MyMDIForm_Resize event handlers works: Bitmap bm = new Bitmap(ClientSize.Width, ClientSize.Height); Graphics gr = Graphics.FromImage(bm); gr.FillRectangle(new SolidBrush(Color.Yellow), 0, 0, bm.Width, bm.Height); gr.DrawString("The cat sat on the mat", Font, new SolidBrush(Color.Red), 50, 50); BackgroundImage = bm; Chris Jobson

      M 1 Reply Last reply
      0
      • C Chris Jobson

        I don't know if there's a "proper" way to do this, but a workround I've found is to draw the text on a bitmap then assign this bitmap to the MDI window's BackgroundImage property. A snag is that you have to re-create the bitmap whenever the window size changes or it gets tiled (actually you probably only need to recreate it if the window gets smaller, but I haven't tested this). For example, putting the following code in the MyMDIForm_Load and the MyMDIForm_Resize event handlers works: Bitmap bm = new Bitmap(ClientSize.Width, ClientSize.Height); Graphics gr = Graphics.FromImage(bm); gr.FillRectangle(new SolidBrush(Color.Yellow), 0, 0, bm.Width, bm.Height); gr.DrawString("The cat sat on the mat", Font, new SolidBrush(Color.Red), 50, 50); BackgroundImage = bm; Chris Jobson

        M Offline
        M Offline
        mirano
        wrote on last edited by
        #3

        Yes, thanks, but I already new this one. The solution is awkward one, and I was looking for something better I also know how to subclass the window and then enumarate all windows to find the MDIClientWnd (which is actually a container for all child windows) and then to intercept WM_PAINT and WM_ERASEBKGND messages to get a device context to draw onto. But this all is just too much for a simple drawing on the MDI background, so I was just wondering if I am missing something obvious. Thanks for yor answers anyways, I appreciate it. .

        Richard DeemingR 1 Reply Last reply
        0
        • M mirano

          Yes, thanks, but I already new this one. The solution is awkward one, and I was looking for something better I also know how to subclass the window and then enumarate all windows to find the MDIClientWnd (which is actually a container for all child windows) and then to intercept WM_PAINT and WM_ERASEBKGND messages to get a device context to draw onto. But this all is just too much for a simple drawing on the MDI background, so I was just wondering if I am missing something obvious. Thanks for yor answers anyways, I appreciate it. .

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Try this in the MDI parent form:

          foreach(Control c in Controls)
          {
          MdiClient mc = c as MdiClient;
          if (null != mc)
          {
          using(Graphics g = mc.CreateGraphics())
          {
          g.DrawString(...);
          // etc.
          }
          }
          }

          As far as I can see, the MdiClient class is the managed equivalent of the MDIClientWnd.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          M 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Try this in the MDI parent form:

            foreach(Control c in Controls)
            {
            MdiClient mc = c as MdiClient;
            if (null != mc)
            {
            using(Graphics g = mc.CreateGraphics())
            {
            g.DrawString(...);
            // etc.
            }
            }
            }

            As far as I can see, the MdiClient class is the managed equivalent of the MDIClientWnd.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            M Offline
            M Offline
            mirano
            wrote on last edited by
            #5

            Thanks, your solution is a smart one, and it works, but only partially. When I put this in the Form_Load event it will draw the text, but then any child window that moves over that area of MDI background where the text is drawn will actually erase it. I put it in the load event, so to draw it only ones for the lifetime of the application, if I put it in the paint event it does not work at all. But thanks for your code anyways, it is good to know that MdiClient class maps to the MdiClientWnd, which is something I totally disregarded. .

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups