覆盖面板油漆事件 [英] Override panel paint event

查看:58
本文介绍了覆盖面板油漆事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前有一个类似的问题,并且想出了如何在表单中添加位图.现在,我需要弄清楚在旋转后如何将位图添加到面板中.

初学者在这个图形的东西,所以请帮帮我.

这会将位图加载到表单上

I had a similar question earlier and have figured out how to add a bitmap to a form. Now I need to figure out how to add the bitmap to a panel after I have rotated it.

Beginner at this graphics stuff so please help me out.

This loads a bitmap onto the form

Public Class Form1
    Private angle As Integer
    Dim imagetwo As Bitmap
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        imagetwo = New Bitmap("C:\Documents and Settings\wiswalld\My Documents\DEP Police Patch.bmp")
        imagetwo.MakeTransparent(Color.Black)
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.TranslateTransform(60, 60)
        e.Graphics.RotateTransform(angle)
        e.Graphics.DrawImage(imagetwo, -50, -50, 100, 100)
        e.Graphics.ResetTransform()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.angle += 5
        Me.Refresh()
    End Sub
End Class

推荐答案

您不要覆盖事件",这甚至是不可能的.您只能通过在事件的调用列表中添加事件句柄来处理事件.这是你做的.对于所有控件,尤其是对于Form而言(并且仅因为您已经对其进行了子类化),重写虚拟受保护方法OnPaint更好,更容易.

您也可以将Panel子类化,但是在大多数情况下,它不会添加所需的任何功能,因此您最好将Control子类化并覆盖OnPaint.这是因为无论如何迟早您都将需要派生类,例如,访问受保护的SetStyles,例如,引入优化的双缓冲以避免闪烁.另外,您需要调用Invalidate,而不是Refresh.您可以使用参数(RectangeRegion)调用Invalidate来重新渲染场景的一部分.

我看到的代码中唯一的大问题是位图文件的硬代码路径名.在任何情况下,硬编码路径名都不会有用,但是我希望这只是一个尝试.您也没有报告您遇到的任何问题(为什么?这是要提到的第一件事?您希望获得什么样的帮助?),但是如果这是错误的轮换或位置,则只需要进行转换即可您需要和他们的订单来掌握他们的工作方式,这很容易做到.

—SA
You don''t "override event", this is not even possible. You can only handle event by adding an event handle to its invocation list; this is what you do. For all controls, especially for Form (and only because you already subclass it), it''s better and easier to override virtual protected method OnPaint.

You can subclass Panel as well, but in most cases it does not add any functionality you need, so you better subclass Control and override OnPaint. This is because sooner or later you will need derived class anyway, for example, to access protected SetStyles, for example, to introduce optimized double buffering to avoid flicker. Also, you need to call Invalidate, not Refresh. You can call Invalidate with parameters (Rectange or Region) to re-render just a part of the scene.

The only big problem in your code I can see is a hard-code path name for a bitmap file. There are no situations where a hard-code path name could be useful, but I hope this is just for a try. You also did not report any problems you are facing with (why? this is the first thing to mention? what kind of help do you expect?), but if this is a wrong rotation or position, you just need to play with the transforms you need and their order to get a grip on how they work, which is pretty easy to do.

—SA


这篇关于覆盖面板油漆事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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