如何在OnClick方法中使用Graphics [英] how to use Graphics in OnClick method

查看:107
本文介绍了如何在OnClick方法中使用Graphics的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public   class  ToolStripEnhancedMenuItem:ToolStripMenuItem 
{
受保护 覆盖 void OnClick( EventArgs e)
{
// 我想在这个方法中使用Graphics,我怎么能使用?
}
}





  protected  覆盖  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);

.Checked = false ;
}
}


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

}
}





我画了ToolStripEnhancedMenuItem左边的圆圈。所以我想用OnClick方法擦除它。这就是为什么我想在OnClick方法中使用图形的原因。

解决方案

你可以,但你应该知道在这个事件中呈现的图形不会永久保持这种控制;第一个失效将覆盖您刚刚使用被覆盖的 OnPaint 呈现的图形渲染的像素,如果 base.OnPaint 被调用,事件 Paint 的事件处理程序也将进入游戏。这是它的工作原理。



如果你可以在你的逻辑中考虑它,你可以使用 System.Drawing实例渲染一些东西.Graphics 使用方法从窗口获取 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



但是,我不建议你这样做,除非你非常了解你在做什么。



或者,您最好在 OnPaint 中完成所有渲染。这是一个想法:您从一些数据模型渲染所有图形,矢量图形数据保存在内存中。单击时,您将根据单击数据修改数据模型,然后使用以下方法之一立即使所有场景或部分场景无效:

https://msdn.microsoft.com/en-us/library/system.windows .forms.control.invalidate%28v = vs.110%29.aspx



在内部,这都是基于<$ c $的处理c> WM_PAINT 窗口消息,非常狡猾且高度优化。对于某些背景,请参阅我过去的答案:

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...))

在面板上捕捉绘图

mdi子表单之间的绘制线

如何加速我的vb.net应用程序?

用C#.net鼠标滚轮缩放图像



-SA

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.

解决方案

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


这篇关于如何在OnClick方法中使用Graphics的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆