如何在无法访问 System.Windows.Forms.PaintEventArgs 命名空间的情况下在 Winform 上绘图?

发布于 2024-12-11 03:21:52 字数 163 浏览 0 评论 0 原文

从我迄今为止在程序中所做的来看,似乎在 winform 上绘制任何内容的唯一方法是通过 System.Windows.Forms.PaintEventArgs。如果您无权访问这些参数或此名称空间并且只能访问 winform,该怎么办?如何在 winform 上绘制(例如矩形等形状)。

提前致谢。

Form what I have done so far in my program, it seems that the only way to draw anything on a winform is through System.Windows.Forms.PaintEventArgs. What if you don't have access to these arguments or this namespace and you only have access to the winform, how do you draw (say a shape like a rectangle) on a winform.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖上的星空 2024-12-18 03:21:53

如果要在 Paint 事件之外在窗体(或任何其他控件)上绘图,则必须获取该控件的 Graphics 对象。然后就可以随心所欲地画画了。但请注意,下次出现绘画消息时,您绘制的内容可能会被删除。

在表格上绘图:

using (Graphics g = this.CreateGraphics())
{
    // do all your drawing here
}

If you want to draw on the form (or any other control) outside of the Paint event, you have to get the Graphics object for that control. Then you can paint to your heart's content. Note, however, that the next time a paint message comes along, what you've drawn is likely to be erased.

To draw on the form:

using (Graphics g = this.CreateGraphics())
{
    // do all your drawing here
}
ゃ人海孤独症 2024-12-18 03:21:53

PaintEventArgs 是 ="http://msdn.microsoft.com/en-us/library/system.windows.forms.aspx" rel="nofollow">System.Windows.Forms 命名空间。如果您正在使用表单,则意味着您确实有权访问名称空间和类,但您可能无法访问您尝试使用的表单代码...

即使您无权访问对于窗体的源代码,Paint 事件是公共的,您可以从窗体外部的代码向其注册处理程序。 (我猜这是你的问题。)

请参阅这个简单的示例类,它引用了 Form,向绘制处理程序注册,然后进行一些任意绘图。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
    class Painter {
        public Painter(Form form) {
            form.Paint += new PaintEventHandler(form_Paint);
        }

        void form_Paint(object sender, PaintEventArgs e) {
            e.Graphics.DrawLine(Pens.Black, 0, 0, 20, 20);
        }
    }
}

此代码片段中的重要概念是,如果您有对表单(或任何 Control 派生对象)的引用,您可以注册绘制事件,每当需要重新绘制控件时,该事件就会自动调用(并且您可以绘制任何您想要的内容)在该控件上。)

在代码片段中,我在构造函数中传递了表单,并在那里注册了绘制事件,但这只是一个简单的示例。您的代码结构会有所不同,但是...您将有一个表单,并且将有一个初始化步骤,您可以在其中注册事件,然后创建一个执行绘画的方法。

您可以通过创建和处理自己的 Graphics 对象来绘制其他方法,但这不是更好的方法。其一,当您需要重绘窗口时,您不会收到通知,并且需要创建其他重绘机制,例如计时器(作为一个非常简单且丑陋的示例),并且您必须自己管理 Graphics 对象。

PaintEventArgs is a class in the System.Windows.Forms namespace. If you're working with forms it means that you do have access to the namespace and the class, you may not however have access to the Form's code that you're attempting to draw on...

Even if you don't have access to the Form's source code, the Paint event is public and you can register a handler to it from code outside of the Form. (I'm guessing this is your issue.)

See this simple example class that has a reference to a Form, registers with the paint handler and then does some arbitrary drawing.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
    class Painter {
        public Painter(Form form) {
            form.Paint += new PaintEventHandler(form_Paint);
        }

        void form_Paint(object sender, PaintEventArgs e) {
            e.Graphics.DrawLine(Pens.Black, 0, 0, 20, 20);
        }
    }
}

The important concept in this snippet is that if you have a reference to a form (or any Control-derived object) you can register with the paint event which gets called automatically whenever the control need to be repainted (and you can draw anything you want on that control.)

In the snippet I passed the Form in the constructor and registered with the paint event there but that was just for a quick example. Your code's structure will be different but... you will have a Form and there will be an initialization step where you register for the event and then create a method which does your painting.

You can draw other ways by creating and disposing your own Graphics object but this is not the preferable method. For one, you do not get notified when you need to redraw the window and would need to create other mechanisms for redrawing such as a timer (as a very simple and ugly example) and you would have to manage the Graphics object yourself.

献世佛 2024-12-18 03:21:53

您可以通过订阅Control.Paint 事件,您可以在您订阅的事件处理程序中执行所有绘图。这将与相关的 PaintEventArgs 一起提供。

You get access to them by subscribing to the Control.Paint event, and you perform all your drawing within the event handler you've subscribed. That will be provided with the relevant PaintEventArgs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文