471,570 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

how to paint on top of a forms main menu?

Hi,

I am trying to draw on top of the main menu area of a form (this is just a
test
at the moment). I'm handling the forms paint event and using the following
code:

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs
e)
{
try
{
using (Graphics g = Graphics.FromHwnd(mainMenu1.Handle))
{
RectangleF testBox = new RectangleF(g.ClipBounds.Width - 50,
g.ClipBounds.Y, 50, g.ClipBounds.Height);
using (StringFormat sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.FillRectangle(Brushes.Yellow, testBox);
g.DrawString("TEST", this.Font, Brushes.Black, testBox, sf);
}
}
base.OnPaint(e);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

....however I get an OutOfMemoryException error on the line above that starts
using (Graphics g = Graphics.FromHwnd(...

Anybody have any ideas about how I might achieve this?
All ideas welcome, although I don't want to have to use the Win API if I can
help it.

Regards,
Peter
Feb 1 '06 #1
2 1735
Peter,

You can't do what you are doing, trying to get the Graphics instance
from the handle of the menu. The menu handle is not a window handle, but
something else completely.

What you have to do is get the handle of the window the menu is attached
to, and then paint that. This is not the recommended way, however.

Do you have to paint over the menu, or can you just get by painting the
menu items? If it is the latter, then just set the OwnerDraw property to
true and handle the DrawItem event.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Row" <ge******@spammers.com> wrote in message
news:Ox*************@TK2MSFTNGP14.phx.gbl...
Hi,

I am trying to draw on top of the main menu area of a form (this is just a
test
at the moment). I'm handling the forms paint event and using the following
code:

private void Form1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
try
{
using (Graphics g = Graphics.FromHwnd(mainMenu1.Handle))
{
RectangleF testBox = new RectangleF(g.ClipBounds.Width - 50,
g.ClipBounds.Y, 50, g.ClipBounds.Height);
using (StringFormat sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.FillRectangle(Brushes.Yellow, testBox);
g.DrawString("TEST", this.Font, Brushes.Black, testBox, sf);
}
}
base.OnPaint(e);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

...however I get an OutOfMemoryException error on the line above that
starts
using (Graphics g = Graphics.FromHwnd(...

Anybody have any ideas about how I might achieve this?
All ideas welcome, although I don't want to have to use the Win API if I
can
help it.

Regards,
Peter

Feb 1 '06 #2
Why don't you want to use API? AFAIK it's the only way to do it, and you'll
find an example using API on my site:
http://www.dotnetrix.co.uk/menus.html --> Custom MenuBar color

The example uses a SolidBrush but you can easily replace that with a
TextureBrush.

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Peter Row" <ge******@spammers.com> wrote in message
news:Ox*************@TK2MSFTNGP14.phx.gbl...
Hi,

I am trying to draw on top of the main menu area of a form (this is just a
test
at the moment). I'm handling the forms paint event and using the following
code:

private void Form1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
try
{
using (Graphics g = Graphics.FromHwnd(mainMenu1.Handle))
{
RectangleF testBox = new RectangleF(g.ClipBounds.Width - 50,
g.ClipBounds.Y, 50, g.ClipBounds.Height);
using (StringFormat sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.FillRectangle(Brushes.Yellow, testBox);
g.DrawString("TEST", this.Font, Brushes.Black, testBox, sf);
}
}
base.OnPaint(e);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

...however I get an OutOfMemoryException error on the line above that
starts
using (Graphics g = Graphics.FromHwnd(...

Anybody have any ideas about how I might achieve this?
All ideas welcome, although I don't want to have to use the Win API if I
can
help it.

Regards,
Peter

Feb 2 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Joshua Russell | last post: by
reply views Thread by Duncan Mole | last post: by
2 posts views Thread by Serge Klokov | last post: by
2 posts views Thread by VSNT | last post: by
6 posts views Thread by Jrsmith | last post: by
2 posts views Thread by Mario | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.