C#+ Windows CE + Multiline&对齐标签

时间:2013-06-07 14:50:28

标签: alignment compact-framework label windows-ce multiline

我需要制作一个垂直和水平对齐的多线标签,我不知道怎么做!

我找到了一种使用此功能制作任何控制多线的方法:

    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个属性同时工作!

1 个答案:

答案 0 :(得分: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();
}

它看起来与你的完全一样,但我没有亲自测试过。