閱讀要求:了解 c# 調用 win32api 的基本原理和操作方式
--------------------------------------------------------------------------------
在開發 winform 控件時,通常需要測量文本繪出時的實際尺寸。
.net fcl 中的 gdi+ 類——system.drawing.graphics 提供了用于上述需要的 measurestring 方法,該方法返回了一個 sizef 結構的浮點數表示的結果,從表面上看起來似乎很精確,但在實際使用中發現有時此方法并不能精確測量出文本的實際寬度。
也曾反編譯 system.drawing.dll,但沒看出什么名堂來。
如果使用 gdi 卻能很好地測量出文本的實際繪出寬度,下面提供了調用 gdi win32api 來測量的 c# 實現源代碼,以供參考。
注: 代碼中的 windowsapi 是一個自定義類,包裝了大部分 win32api 調用的 c# 定義。
/// <summary>
/// 在指定的矩形區域內,按照指定的格式,測量文本的實際繪出尺寸。
/// </summary>
/// <param name="graphics">繪圖對象</param>
/// <param name="text">被測量的文本</param>
/// <param name="font">測量所用字體</param>
/// <param name="rc">以矩形表示的要繪制區域</param>
/// <param name="drawflags">文本格式</param>
/// <returns>尺寸</returns>
public static size gettextsize(graphics graphics, string text, font font, rectangle rc, drawtextformatflags drawflags)
{
// 一個記錄設備上下文句柄的變量
intptr hdc = intptr.zero;
if ( graphics != null )
{
// 獲取與提供的 graphics 關聯的設備上下文句柄
hdc = graphics.gethdc();
}
else
{
// 如果未提供 graphics,使用屏幕作為設備上下文
hdc = windowsapi.getdc(intptr.zero);
}
// 測量所用字體的句柄
intptr fonthandle = font.tohfont();
// 將測量所用字體添加到設備上下文
// 并記錄原來所使用的字體
intptr oldhfont = windowsapi.selectobject(hdc, fonthandle);
// rect 用于 win32api 調用,.net fcl 中的 retangle 不適用于 win32api 調用。
// 其定義如下:
//
// [structlayout(layoutkind.sequential)] // 這是必須的。
// public struct rect
// {
// public int left;
// public int top;
// public int right;
// public int bottom;
// }
// 創建一個 gdi win32api 調用所需的 rect 實例
rect rect = new rect();
rect.left = rc.left;
rect.right = rc.right;
rect.top = rc.top;
rect.bottom = rc.bottom;
// 文本繪制格式標志的枚舉定義:
// public enum drawtextformatflags
// {
// dt_top = 0x00000000,
// dt_left = 0x00000000,
// dt_center = 0x00000001,
// dt_right = 0x00000002,
// dt_vcenter = 0x00000004,
// dt_bottom = 0x00000008,
// dt_wordbreak = 0x00000010,
// dt_singleline = 0x00000020,
// dt_expandtabs = 0x00000040,
// dt_tabstop = 0x00000080,
// dt_noclip = 0x00000100,
// dt_externalleading = 0x00000200,
// dt_calcrect = 0x00000400,
// dt_noprefix = 0x00000800,
// dt_internal = 0x00001000,
// dt_editcontrol = 0x00002000,
// dt_path_ellipsis = 0x00004000,
// dt_end_ellipsis = 0x00008000,
// dt_modifystring = 0x00010000,
// dt_rtlreading = 0x00020000,
// dt_word_ellipsis = 0x00040000
// }
// 調用 gdi win32api 以測量文本的實際繪出時尺寸。
windowsapi.drawtext(hdc, text, text.length, ref rect,
(int)(drawflags | drawtextformatflags.dt_calcrect));
// 重設為原來的字體,這是 gdi 必須的。
windowsapi.selectobject(hdc, oldhfont);
// 刪除創建的測量用字體的句柄
windowsapi.deleteobject(fonthandle);
// 釋放已獲取的設備上下文句柄
if ( graphics != null )
graphics.releasehdc(hdc);
else
windowsapi.releasedc(intptr.zero, hdc);
// 返回實測結果
return new size(rect.right - rect.left, rect.bottom - rect.top);
}
以下是三個有用的重載方法:
public static size gettextsize(string text, font font, rectangle rc, drawtextformatflags drawflags)
{
return gettextsize(null, text, font, rc, drawflags);
}
public static size gettextsize(graphics graphics, string text, font font)
{
// 設置一個文本繪制格式,以單行左對齊方式來測量。
drawtextformatflags drawflags =
drawtextformatflags.dt_singleline |
drawtextformatflags.dt_left |
drawtextformatflags.dt_calcrect; // 這個標志表示要測量
rectangle rect = rectangle.empty;
return gettextsize(graphics, text, font, rect, drawflags) ;
}
public static size gettextsize(string text, font font)
{
return gettextsize(null, text, font);
}