2012-04-10 107 views
3

我在繪製VCL風格的窗口元素時遇到繪畫不正確的問題。在具有圓角的樣式上,我在控件邊界矩形和樣式的圓角窗口角之間的空白處獲得了白色背景。Delphi XE2風格繪畫

enter image description here

上面的影像是用水族光板岩運行,但帶有圓角的任何風格會顯示此相同的問題。我錯過了什麼?

type 
    TSample = class(TCustomControl) 
    protected 
    procedure Paint; override; 
    end; 

{ TForm1 } 
procedure TForm1.FormCreate(Sender: TObject); 
var 
    R: TRect; 
    S: TSample; 
begin 
    R := ClientRect; 
    InflateRect(R, -20, -20); 
    S := TSample.Create(Application); 
    S.Parent := Self; 
    S.BoundsRect := R; 
end; 

{ TSample } 
procedure TSample.Paint; 
var 
    Details: TThemedElementDetails; 
begin 
    Details := StyleServices.GetElementDetails(twCaptionActive); 
    StyleServices.DrawParentBackground(Self.Handle, Canvas.Handle, Details, False); 
    StyleServices.DrawElement(Canvas.Handle, Details, ClientRect); 
end; 
+0

順便說一句,我也試過ParentBackground:=真,沒有任何變化。也嘗試確保csOpaque被刪除。 – 2012-04-10 18:36:01

+0

您是否嘗試調試'StyleServices.DrawElement'方法以查看它如何在畫布中繪製位圖? – RRUZ 2012-04-10 21:47:34

+0

坦率地說,我希望避免潛入主題引擎的內部,但如果沒有人有更好的想法,那就是我必須做的。 – 2012-04-11 17:32:27

回答

4

好吧,我花了幾分鐘在你的問題,我找到了答案。繪製圓角的關鍵是調用StyleServices.GetElementRegion函數獲取區域,然後使用SetWindowRgn函數將區域應用於控件。

檢查該樣本

procedure TSample.Paint; 
var 
    Details : TThemedElementDetails; 
    Region : HRgn; 
    LRect : TRect; 
begin 
    Details := StyleServices.GetElementDetails(twCaptionActive); 
    LRect := Rect(0, 0, Width, Height); 
    StyleServices.GetElementRegion(Details, LRect, Region); 
    SetWindowRgn(Handle, Region, True); 
    StyleServices.DrawParentBackground(Self.Handle, Canvas.Handle, Details, False); 
    StyleServices.DrawElement(Canvas.Handle, Details, ClientRect); 
end; 

這是結果

enter image description here

+0

這絕對有用,非常感謝您的幫助!這也適用於使用屏幕位圖作爲標題區域。對於其他人做類似的事情,值得指出的是,整個客戶端矩形需要傳遞給GetElementRegion。 – 2012-04-12 17:12:53