データベースが実際に表示サイズを twip で保存する移行プロジェクトに取り組んでいます。twips を使用して WPF または Winforms コントロールにサイズを割り当てることができないため、実行時に使用できる変換メソッドが .NET にあるかどうか疑問に思っていました。
4 に答える
移行ツールには何かがあることがわかりましたが、実行時には何の役にも立たないでしょう。これが私がしたことです(拡張メソッドのハードコードされた値がポイント/インチの値に変更された場合、ポイントコンバーターとしても機能します):
1 ツイップ = 1/1440 インチ。.NETGraphics
オブジェクトには と メソッドがDpiX
あり、DpiY
これを使用して 1 インチが何ピクセルかを判断できます。これらの測定値を使用して、次の拡張メソッドを作成しましたGraphics
。
using System.Drawing;
static class Extensions
{
/// <summary>
/// Converts an integer value in twips to the corresponding integer value
/// in pixels on the x-axis.
/// </summary>
/// <param name="source">The Graphics context to use</param>
/// <param name="inTwips">The number of twips to be converted</param>
/// <returns>The number of pixels in that many twips</returns>
public static int ConvertTwipsToXPixels(this Graphics source, int twips)
{
return (int)(((double)twips) * (1.0 / 1440.0) * source.DpiX);
}
/// <summary>
/// Converts an integer value in twips to the corresponding integer value
/// in pixels on the y-axis.
/// </summary>
/// <param name="source">The Graphics context to use</param>
/// <param name="inTwips">The number of twips to be converted</param>
/// <returns>The number of pixels in that many twips</returns>
public static int ConvertTwipsToYPixels(this Graphics source, int twips)
{
return (int)(((double)twips) * (1.0 / 1440.0) * source.DpiY);
}
}
これらのメソッドを使用するには、単純に次のことを行う必要があります(オブジェクトをCreateGraphics
返すコンテキストにいると仮定しますDrawing.Graphics
(ここthis
ではフォームであるため、のスーパークラスCreateGraphics
から継承されます)。Form
Control
using( Graphics g = CreateGraphics() )
{
Width = g.ConvertTwipsToXPixels(sizeInTwips);
Height = g.ConvertTwipsToYPixels(sizeInTwips);
}
グラフィックス オブジェクトを取得する方法の一覧については、Graphics
クラスのドキュメントの「備考」セクションを参照してください。より完全なドキュメントは、チュートリアルHow to: Create Graphics Objects にあります。
最も簡単な方法の簡単な要約:
Control.CreateGraphics
- Paint イベントのプロパティに
PaintEventArgs
は がGraphics
ありGraphics
ます。 - 画像を渡すと、その画像に描画できるオブジェクト
Graphics.FromImage
が返されます。Graphics
(注: 実際の画像に twip を使用することはほとんどありません)
移行プロジェクトでは、組み込みの変換サポート関数を使用できます
microsoft.visualbasic.compatibility.vb6.twipsperpixelx
microsoft.visualbasic.compatibility.vb6.twipsperpixely
Graphics
参考までに、オブジェクトを要求する代わりに Win32 API を使用する別の C# バージョン:
using System.Runtime.InteropServices;
static class SomeUtils {
public static int ConvertTwipsToPixels(int twips, MeasureDirection direction) {
return (int)(((double)twips)*((double)GetPixperinch(direction))/1440.0);
}
public static int ConvertPixelsToTwips(int pixels, MeasureDirection direction) {
return (int)((((double)pixels)*1440.0)/((double)GetPixperinch(direction)));
}
public static int GetPPI(MeasureDirection direction) {
int ppi;
IntPtr dc = GetDC(IntPtr.Zero);
if (direction == MeasureDirection.Horizontal)
ppi = GetDeviceCaps(dc, 88); //DEVICECAP LOGPIXELSX
else
ppi = GetDeviceCaps(dc, 90); //DEVICECAP LOGPIXELSY
ReleaseDC(IntPtr.Zero, dc);
return ppi;
}
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(IntPtr hdc, int devCap);
}
public enum MeasureDirection {
Horizontal,
Vertical
}
MeasureDirection
ピクセルが常に正方形であるという保証がないため (kb によると)、 を指定することになっています。