473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1807
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joshua Russell | last post by:
Hi, Both the methods below open up a windows form called MasterForm. However, one works better than the other. Method 1 opens the form correctly but I don't have any reference to the instance of...
0
by: Duncan Mole | last post by:
Hi, I have created a control which draws a title bar and provides a drop down menu for a Smart Device Application. It seemed to work fine until I came to add an event handler to act on Paint...
2
by: Serge Klokov | last post by:
Hi! 1. Please, help with example "paint on form by mouse" 2. Below is my example, but it clear the line after each Refresh()... how to fix? 3. How to draw the line in Mouse_Move event? ...
2
by: VSNT | last post by:
Is there a way to paint the backcolor of a main menu. I can paint the menu ites from the size and paint events but there doesnot appear to be an accessable paint method for the actual MainMenu...
3
by: Mike Cooper | last post by:
I have been staring at the above error for over a week now! I have a an inherited data class looking like thus: Public Class DataGridBoolColumnInherit Inherits...
6
by: Jrsmith | last post by:
I am developing a mdi application that will act as a front end to a database. I am finding that when i show any of the mdi child forms in my application it is noticeable that the form is shown...
2
by: Mario | last post by:
Hi, I am trying to create an application with multiple windows forms. The problem that I have is that after creating the window forms, I do not know how to open formN after closing Main form. ...
7
by: Gary Brown | last post by:
Hi, I have a computation intensive application that tries to update the display quite often. After a few seconds the window stops updating and the window goes blank. A breakpoint in the Paint...
9
by: raylopez99 | last post by:
Just an observation: pens for drawing lines in Win Forms are tricky when assignment is inside the paint handler. inside of the Paint handler, but not inside a "using" brace (that is, outside of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.