2

垂直方向と水平方向に配置された複数行のラベルを作成する必要がありますが、どうすればよいかわかりません。

この関数でコントロールを複数行にする方法を見つけました:

    private const int BS_MULTILINE = 0x00002000;
    private const int BS_CENTER = 0x00000300;
    private const int BS_VCENTER = 0x00000C00;
    private const int GWL_STYLE = -16;
    [DllImport("coredll")]
    private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    [DllImport("coredll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    public static void MakeControlMultiline(Control control) {
        IntPtr hwnd = control.Handle;
        int currentStyle = GetWindowLong(hwnd, GWL_STYLE);
        int newStyle = SetWindowLong(hwnd, GWL_STYLE, currentStyle | /*BS_CENTER | BS_VCENTER | */BS_MULTILINE);
    }

「BS_CENTER | BS_VCENTER」は機能しないため、コメントに記載されています。

したがって、次のように、両方の配置を実現する customControl を作成しようとします。

public partial class ImproveLabel : Control {
    ...
    protected override void OnPaint(PaintEventArgs pe) {
        Graphics g = pe.Graphics;
        // text
        StringFormat drawFormat = new StringFormat();
        drawFormat.Alignment = StringAlignment.Center;
        drawFormat.LineAlignment = StringAlignment.Center;
        g.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new Rectangle(0, 0, this.Width, this.Height), drawFormat);
        // Calling the base class OnPaint
        base.OnPaint(pe);
    }

ここで奇妙なことは、両方の配置を「中央」にするとマルチラインが機能しなくなりますが、垂直方向の配置を「中央」に、水平方向の配置を「近く」にするとマルチラインが機能することです。

なぜこのように機能するのかわかりませんが、3 つの属性を同時に機能させる方法を理解するために助けが必要です!

4

1 に答える 1

0

以下のコードは、 P/Invoke SetWindowLongの例からそのまま引用したものです。

private const int GWL_STYLE = -16;
private const int BS_CENTER = 0x00000300;
private const int BS_VCENTER = 0x00000C00;
private const int BS_MULTILINE = 0x00002000;

public static void SetButtonStyle(Button ctrl)
{
    IntPtr hWnd;
    int style;

   // ctrl.Capture = true;
    // hWnd = GetCapture();
    // ctrl.Capture = false;

   // Comment below and uncomment above if using Visual Studio 2003
   hWnd = ctrl.Handle;

    style = GetWindowLong(hWnd, GWL_STYLE);
    SetWindowLong(hWnd, GWL_STYLE, (style | BS_CENTER | BS_VCENTER | BS_MULTILINE));

    ctrl.Refresh();
}

それはあなたのものとほぼ同じように見えますが、私は個人的にテストしていません.

于 2013-06-08T18:28:53.817 に答える