创建一个保存/绘制一些图形的类?

发布于 2024-10-11 10:54:47 字数 141 浏览 2 评论 0原文

好的,我想创建一个类来处理特殊的矩形图形。

在我的表单中,我想要两个特殊的矩形。 所以,基本上,我的表单中需要该类的两个实例,对吗?

我设法初始化了两个,好吧。但是,我到底应该如何管理班级中的绘图/图形等,以及如何在我的表单中显示结果?

Ok, I want to create a class that will handle a special rectangle graphic.

In my form, I want to have two of these special rectangles.
So, basically, I need two instances of that class in my form, right?

I manage to initialize two, alright. But, how exactly am I supposed to manage drawing/graphics etc in a class, and the results to be displayed in my form?

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

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

发布评论

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

评论(2

伪装你 2024-10-18 10:54:47

您需要弄清楚一些概念才能将其组合在一起:

  • 您需要跟踪要绘制的对象。您部分在那里,但这通常是通过某种类型的集合来完成的,例如 List(of ...)
  • 您需要处理 Form 类(或 Panel 或 Control 或您想要绘制的任何视觉对象或on)
  • 您需要在 Paint 处理程序中绘制对象
  • 每当应用程序的状态发生更改时,您需要对正在绘制的对象调用 Invalidate 以“强制”重新绘制。

这是一个简短的片段:

     ' suppose you have:
     Private _myRects as New List(of Rectangle) ' populated elsewhere

     ' then you handle the paint event of a UI control
     Private Sub Control_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) _
     Handles MyBase.Paint
        Dim g As Graphics = e.Graphics

        ' loop through your collection drawing each rectangle:
        for each rect As Rectangle in _myRects 
            g.FillRectangle(Brushes.Aqua, rect)
        next for

        ...more drawing as needed
    end sub

这是关于 .NET 的 相当不错的教程用VB画画。如果您按照本教程进行操作,您应该拥有完成您喜欢的任何类型的 2D .NET 绘图的所有部分。 (乐趣要到第 2 页才开始,但不要跳过第 1 页!)

There are a few concepts you need to figure out to put this together:

  • You need to track the objects you're going to draw. You're partially there but this is usually done with a collection of some sort like List(of ...)
  • You need to handle the Paint event of your Form class (or Panel or Control or whatever visual object you want to draw in or on)
  • You need to draw your objects within you Paint handler
  • Whenever the state of your application changes you need to call Invalidate on the object that is being painted to "force" a fresh repaint.

Here's a quick snippet:

     ' suppose you have:
     Private _myRects as New List(of Rectangle) ' populated elsewhere

     ' then you handle the paint event of a UI control
     Private Sub Control_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) _
     Handles MyBase.Paint
        Dim g As Graphics = e.Graphics

        ' loop through your collection drawing each rectangle:
        for each rect As Rectangle in _myRects 
            g.FillRectangle(Brushes.Aqua, rect)
        next for

        ...more drawing as needed
    end sub

And here is a pretty nice tutorial on .NET painting with VB. If you follow it through you should have all the pieces to do any kind of 2D .NET drawing you like. (The fun doesn't start until page 2 but do not skip page 1!)

梦里兽 2024-10-18 10:54:47

听起来您需要阅读的两件事是开发自定义控件在 Windows 窗体中使用 GDI+

拿一张舒适的椅子和一杯美味的热可可;你有很多阅读要做。

Sounds like the two thing you need to read up on are Developing Custom Controls and Using GDI+ in Windows Forms.

Grab a comfy chair and a nice cup of hot cocoa; you have a lot of reading to do.

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