Control.CreateGraphics 方法
为控件创建 Graphics。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
Public Function CreateGraphics As Graphics
用法
Dim instance As Control
Dim returnValue As Graphics
returnValue = instance.CreateGraphics
public Graphics CreateGraphics ()
public:
Graphics^ CreateGraphics ()
public Graphics CreateGraphics ()
public function CreateGraphics () : Graphics
返回值
控件的 Graphics。
备注
当不再需要返回的 Graphics 时,必须通过调用其 Dispose 方法来释放它。Graphics 只在当前窗口消息期间有效。
提示
除了 InvokeRequired 属性以外,控件上还有四个可以从任何线程上安全调用的方法,它们是:Invoke、BeginInvoke、EndInvoke 和 CreateGraphics。对于所有其他方法调用,则应使用调用 (invoke) 方法之一封送对控件的线程的调用。
示例
下面的代码示例调整指定控件的大小,以便该控件能够容纳带格式的文本。带格式的文本是将控件的分配 Font 应用于文本的 Text 属性。该示例中的 AutoSizeControl
方法还有一个 textPadding
参数,该参数表示应用于控件的所有边缘的空白。要使空白看起来相等,请将文本与 System.Drawing.ContentAlignment 的 MiddleCenter 值对齐(如果您的控件支持该操作)。
Private Sub AutoSizeControl(control As Control, textPadding As Integer)
' Create a Graphics object for the Control.
Dim g As Graphics = control.CreateGraphics()
' Get the Size needed to accommodate the formatted Text.
Dim preferredSize As Size = g.MeasureString( _
control.Text, control.Font).ToSize()
' Pad the text and resize the control.
control.ClientSize = New Size( _
preferredSize.Width + textPadding * 2, _
preferredSize.Height + textPadding * 2)
' Clean up the Graphics object.
g.Dispose()
End Sub
private void AutoSizeControl(Control control, int textPadding)
{
// Create a Graphics object for the Control.
Graphics g = control.CreateGraphics();
// Get the Size needed to accommodate the formatted Text.
Size preferredSize = g.MeasureString(
control.Text, control.Font).ToSize();
// Pad the text and resize the control.
control.ClientSize = new Size(
preferredSize.Width + (textPadding * 2),
preferredSize.Height+(textPadding * 2) );
// Clean up the Graphics object.
g.Dispose();
}
private:
void AutoSizeControl( Control^ control, int textPadding )
{
// Create a Graphics object for the Control.
Graphics^ g = control->CreateGraphics();
// Get the Size needed to accommodate the formatted Text.
System::Drawing::Size preferredSize = g->MeasureString( control->Text, control->Font ).ToSize();
// Pad the text and resize the control.
control->ClientSize = System::Drawing::Size( preferredSize.Width + (textPadding * 2), preferredSize.Height + (textPadding * 2) );
// Clean up the Graphics object.
delete g;
}
private void AutoSizeControl(Control control, int textPadding)
{
// Create a Graphics object for the Control.
Graphics g = control.CreateGraphics();
// Get the Size needed to accommodate the formatted Text.
Size preferredSize = g.MeasureString(control.get_Text(), control.
get_Font()).ToSize();
// Pad the text and resize the control.
control.set_ClientSize(new Size(preferredSize.get_Width()
+ textPadding * 2, preferredSize.get_Height() + textPadding * 2));
// Clean up the Graphics object.
g.Dispose();
} //AutoSizeControl
.NET Framework 安全性
- UIPermission 用于安全子窗口调用此方法。关联的枚举:UIPermissionWindow 的 SafeSubWindows 值。
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0