Click here to Skip to main content
14,360,959 members
Rate this:
Please Sign up or sign in to vote.
See more:
public class ToolStripEnhancedMenuItem : ToolStripMenuItem
{
       protected override void OnClick(EventArgs e)
      { 
         //I want to use Graphics in this method,how can i use ?
      }
}


protected override void OnPaint(PaintEventArgs e)
{
    if ((CheckMarkDisplayStyle == CheckMarkDisplayStyle.RadioButton))
    {
        Size radioButtonSize = RadioButtonRenderer.GetGlyphSize(e.Graphics, RadioButtonState.CheckedNormal);
        int radioButtonX = ContentRectangle.X + 3;
        int radioButtonY = ContentRectangle.Y + (ContentRectangle.Height - radioButtonSize.Height) / 2;

        CheckState state = this.CheckState;


        if (state == System.Windows.Forms.CheckState.Checked)
        {
            if ((this.Text.Contains(" ")) && (!this.Text.Contains("*")))
            {
                //this.Text = this.Text.Replace(" ", "*");
                DrawCircle(e.Graphics,Color.Black);

                finalCheckedMenuItem = this;
            }

        }
        else
        {
            if (this.Text.Contains("*"))
            {
                //this.Text = this.Text.Replace("*", " ");

                DrawCircle(e.Graphics, Color.White);

                this.Checked = false;
            }
        }


        e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Black), new Rectangle(new Point(radioButtonX, radioButtonY), this.Size), new StringFormat());

    }
}


I painted the circle in the left of ToolStripEnhancedMenuItem. so i want to use OnClick method to erase it. It's the reason of why i'm want to use graphics in OnClick method.
Posted
Updated 25-Mar-15 16:14pm
v3
Comments
Andy Lanng 25-Mar-15 5:51am
   
What do you want to do with the graphic? Give us some context to work with, please ^_^
xuyunhai 25-Mar-15 21:09pm
   
I improved my question. thanks for your answer.
Stephen Hewison 25-Mar-15 5:57am
   
If you're looking to using the Paint event on a control. You can call the control.Update method. This will in turn raise the Paint event.
Ramza360 25-Mar-15 12:28pm
   
If for whatever reason you absolutely want the Graphics object there, you would need to create the graphics (i.e. this.CreateGraphics()). Though the Paint method is the best place for such things.
xuyunhai 25-Mar-15 21:04pm
   
Isn't exist CreateGraphics() method In ToolStripMenuItem Class

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You can, but you should know that the graphics rendered in this event won't be permanently kept on this control; very first invalidation will overwrite the pixels you just rendered with the graphics rendered by overridden OnPaint, and, if base.OnPaint was called, the event handlers of the event Paint would also come into the game. This is how it works.

If you can take it into account in your logic, you could render something using the instance of System.Drawing.Graphics obtained from the window using the method System.Drawing.Graphics.FromHwnd:
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromhwnd(v=vs.110).aspx,
https://msdn.microsoft.com/en-us/library/system.drawing.graphics%28v=vs.110%29.aspx.

However, I would not advise you to do that, unless you understand very well what you are doing.

Alternatively, you could better do all the rendering in your OnPaint. This is the idea: you render all graphics from some data model, vector graphics data kept in memory. On click, you modify the data model according to click data, and then immediately invalidate all the scene or part of it using one of the these methods:
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.invalidate%28v=vs.110%29.aspx.

Internally, this is all based on processing of WM_PAINT window message, which is very cunning and highly optimized. For some background, please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...)),
capture the drawing on a panel,
Drawing Lines between mdi child forms,
How to speed up my vb.net application?,
Zoom image in C# .net mouse wheel.

—SA
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100