2013-06-07 2 views
2

세로 및 가로 맞춤으로 여러 줄 레이블을 만들어야하는데 어떻게 할 수 있는지 알 수 없습니다.C# + Windows CE + 여러 줄 및 레이블 맞추기

나는이 기능으로 모든 컨트롤의 여러 줄을 만들 수있는 방법을 발견했다 :

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"는 의견에 작동하지 않기 때문에! 나는 두 alignements에 "센터"를 넣으면

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); 
    } 

여기서 이상한 것은, 여러 줄이 더 이상 작동하지 않는 것을 :

그래서 나는이 같은 두 정렬을 실현 어디 customControl을 만들기 위해 노력 그러나 "가운데"에 대한 수직 정렬과 "근방"에 대한 수평 정렬 만 있다면 다중 선이 작동합니다.

왜 이런 식으로 작동하는지 이해가 가지 않지만 동시에 3 가지 속성을 동시에 사용할 수있는 방법을 알아 내야합니다!

답변

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(); 
} 

본인의 것과 모양이 비슷하지만 개인적으로 테스트하지 않았습니다.

+0

그래, 같은 코드이고 Compact Framework에서 작동하지 않습니다. (나는 ctrl.Refresh()를 추가하려고했지만 아무 것도 변경하지 않았습니다). 아무도이 문제에 대해 알지 못합니다. – Mikael