2011-08-20 59 views
3

我想自定義繪製tabcontrol。當我使用由GetTabRect返回的尺寸繪製選項卡時,與通常繪製的方式相比,選項卡明顯更寬。我以爲它試圖爲圖像騰出空間,但我沒有爲選項卡定義圖像。爲什麼GetTabRect會返回更大的尺寸?所有者繪製tabcontrol有更寬的選項卡

不知道這與它有什麼關係,但這裏是我如何將它設置爲自定義TabControl構造函數的所有者。

this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | 
       ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw | 
       ControlStyles.SupportsTransparentBackColor, true); 

回答

4

我發現這裏的解決方案:http://www.codeproject.com/Messages/2707590/Re-Tab-Size.aspx

報價:

當ControlStyle.UserPaint設置爲true,控制不再發送WM_SETFONT消息。
發送FontChange信息所需要的代碼:

[DllImport("user32.dll")] 
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); 

private const int WM_SETFONT = 0x30; 
private const int WM_FONTCHANGE = 0x1d; 

protected override void OnCreateControl() 
{ 
    base.OnCreateControl(); 
    this.OnFontChanged(EventArgs.Empty); 
} 

protected override void OnFontChanged(EventArgs e) 
{ 
    base.OnFontChanged(e); 
    IntPtr hFont = this.Font.ToHfont(); 
    SendMessage(this.Handle, WM_SETFONT, hFont, (IntPtr)(-1)); 
    SendMessage(this.Handle, WM_FONTCHANGE, IntPtr.Zero, IntPtr.Zero); 
    this.UpdateStyles(); 
} 
+1

尤里卡!這一直在困擾我很長一段時間! +1 – series0ne

相關問題