如何在 WinForms 中手动获取 Graphics 对象的实例? [英] How to manually get instance of Graphics object in WinForms?

查看:24
本文介绍了如何在 WinForms 中手动获取 Graphics 对象的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用 Graphics 类型的对象(至少我能够渲染图像),但我总是通过传递从 OnPaint 方法检索到的图形对象来做到这一点.

I know how to work with object of type Graphics (at least I am able to render images) but I always do that by passing graphics object retrieved from OnPaint method.

我想在应用程序打开时显示一个图像(即在 Form_Load 方法中)但不知道如何获取我可以使用的 Graphics 对象的实例?谢谢

I would like to display an image when the app is opened (ie in Form_Load method) but have no clue how to obtain the instance of Graphics object I could use? Thanks

推荐答案

使用 OnPaint() 提供给您的 e.Graphics 对象是正确的做法.它将在 OnLoad() 方法之后立即运行.该表单在 OnLoad 中尚不可见.

Using the e.Graphics object that OnPaint() supplies to you is the correct way of doing it. It will run right after the OnLoad() method. The form isn't visible yet in OnLoad.

支持从 Control.CreateGraphics() 获取 Graphics 对象.但是,只要表单重新绘制,您用它绘制的任何内容都会被清除.当用户在您的窗口上移动另一个窗口(Aero 之前)或者当她最小化和恢复窗口或以其他方式调整窗口大小时,就会发生这种情况.仅在以高速率制作动画时才使用 CreateGraphics.

Getting a Graphics object from Control.CreateGraphics() is supported. However, whatever you draw with this will be wiped out as soon as the form repaints itself. Which happens when the user moves another window across yours (pre-Aero) or when she minimizes and restores or otherwise resizes the window. Use CreateGraphics only ever when animating at a high rate.

这篇关于如何在 WinForms 中手动获取 Graphics 对象的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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