建立非矩形form(透明背景) 常用的Window API

1 篇文章 / 0 new
author
建立非矩形form(透明背景) 常用的Window API
有關區域的Windows API函數,用它可以創建各種不規則的區域,詳情請參看MSDN和Delphi的Win32 Help。
CombineRgn, CreateEllipticRgn, CreateEllipticRgnIndirect, CreatePolygonRgn, CreatePolyPolygonRgn, CreateRectRgn, CreateRectRgnIndirect, CreateRoundRectRgn, EqualRgn, ExtCreateRegion, FillRgn, FrameRgn, GetPolyFillMode, GetRegionData, GetRgnBox, InvertRgn, OffsetRgn, PaintRgn, PtInRegion, RectInRegion, SetPolyFillMode, SetWindowRgn

有關Path的Windows API函數,可以建立更加不規則的視窗,如:一個文字的形狀的視窗。
AbortPath, BeginPath, CloseFigure, EndPath, FillPath, FlattenPath, GetMiterLimit, GetPath, PathToRegion, SetMiterLimit, StrokeAndFillPath, StrokePath, WidenPath
 
兩區域的合併模式
CombineRgn(
  p1: HRGN;     //合成後的區域
  p2, p3: HRGN; //個原始區域
  p4: Integer   //合併選項; 見下表
): Integer;     //有四種可能的返回值
//合併選項:
RGN_AND  = 1;
RGN_OR   = 2;
RGN_XOR  = 3;
RGN_DIFF = 4;
RGN_COPY = 5; //複製第一個區域
//返回值:
ERROR         = 0; //錯誤
NULLREGION    = 1; //空區域
SIMPLEREGION  = 2; //單矩形區域
COMPLEXREGION = 3; //多矩形區域 }

透明背景的設計方式
procedure bkTransparent;
var
    AControl : TControl;
    A, Margin, X, Y, CtlX, CtlY : Integer;
begin
    Margin := ( Width - ClientWidth ) div 2; //邊框點數
    //form 區域
    FullRgn := CreateRectRgn(0, 0, Width, Height);
    //form client 區域
    X := Margin;
    Y := Height - ClientHeight - Margin;//標題下方起始位置
    ClientRgn := CreateRectRgn( X, Y, X + ClientWidth, Y + ClientHeight );//去除邊框,標題後區域
    // 'Mask ' form client 區域設為透明
    CombineRgn( FullRgn, FullRgn, ClientRgn, RGN_DIFF );
    //將 form client 區域內元件區域設為非透明
    for A := 0 to ControlCount - 1 do
    begin
        AControl := Controls[A];
        if ( AControl is TWinControl ) or ( AControl is TGraphicControl ) then
        begin
            with AControl do
            begin
                if Visible then
                begin
                    CtlX := X + Left;
                    CtlY := Y + Top;
                    CtlRgn := CreateRectRgn( CtlX, CtlY, CtlX + Width, CtlY + Height );
                    CombineRgn( FullRgn, FullRgn, CtlRgn, RGN_OR );
                end;
            end;
        end;
    end;
    //將參數設定生效
    SetWindowRgn(Handle, FullRgn, TRUE);
end;
取消透明
    FullRgn := CreateRectRgn(0, 0, Width, Height);
    CombineRgn(FullRgn, FullRgn, FullRgn, RGN_COPY);
    SetWindowRgn(Handle, FullRgn, TRUE);
Free Web Hosting