DrawString on MDI background??
-
-
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. .
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 -
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 JobsonYes, 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. .
-
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. .
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 theMDIClientWnd
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
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 theMDIClientWnd
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
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. .