簡體   English   中英

如何使用delphi在區域中繪制邊框?

[英]How I can draw a border in a region using delphi?

我正在使用此代碼

procedure DrawPolygonRegion(wnd : HWND; rect : TRect; NumPoints : Integer; DoStarShape : Boolean);
const
  RadConvert = PI/180;
  Degrees    = 360;
  MaxLines   = 100;
var
  x, y,
  xCenter,
  yCenter,
  radius,
  pts,
  I       : Integer;
  angle,
  rotation: Extended;
  arPts   : Array[0..MaxLines] of TPoint;
  rgn  : HRGN;
begin

  xCenter := (rect.Right - rect.Left) div 2;
  yCenter := (rect.Bottom - rect.Top) div 2;
  if DoStarShape then
    begin
      rotation := Degrees/(2*NumPoints);
      pts := 2 * NumPoints;
    end
  else
    begin
      rotation := Degrees/NumPoints;             //get number of degrees to turn per point
      pts := NumPoints
    end;
  radius := yCenter;

  {This loop defines the Cartesian points of the shape. Again,
   I've added 90 degrees to the rotation angle so the shapes will
   stand up rather than lie on their sides. Thanks again to Terry Smithwick and
   David Ullrich for their trig help on CompuServe.}
  for I := 0 to pts - 1 do begin
    if DoStarShape then
      if (I mod 2) = 0 then //which means that
        radius := Round(radius/2)
      else
        radius := yCenter;

    angle := ((I * rotation) + 90) * RadConvert;
    x := xCenter + Round(cos(angle) * radius);
    y := yCenter - Round(sin(angle) * radius);
    arPts[I].X := x;
    arPts[I].Y := y;
  end;

  rgn := CreatePolygonRgn(arPts, pts, WINDING);
  SetWindowRgn(wnd, rgn, TRUE);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DrawPolygonRegion(Handle, BoundsRect, 5, True)
end;

以這種方式設置表單的形狀

在此輸入圖像描述

現在我需要在形狀中繪制一個顏色邊框,但我無法弄清楚如何使這個任務。 我尋找的結果是這樣的。

在此輸入圖像描述

任何想法如何完成這項任務?

如果要繼續使用區域,請在Form的OnPaint事件中調用Win32 API FrameRgn()函數,例如:

type
  TForm1 = class(TForm)
    procedure FormPaint(Sender: TObject);
  private
    Rgn: HRGN;
  protected
    procedure CreateWindowHandle(const Params: TCreateParams); override;
  end;

function CreateMyPolygonRegion(rect : TRect; NumPoints : Integer; DoStarShape : Boolean): HRGN; 
const 
  RadConvert = PI/180; 
  Degrees    = 360; 
  MaxLines   = 100; 
var 
  x, y, 
  xCenter, 
  yCenter, 
  radius, 
  pts, 
  I       : Integer; 
  angle, 
  rotation: Extended; 
  arPts   : Array[0..MaxLines] of TPoint; 
begin 
  xCenter := (rect.Right - rect.Left) div 2; 
  yCenter := (rect.Bottom - rect.Top) div 2; 
  if DoStarShape then 
    begin 
      rotation := Degrees/(2*NumPoints); 
      pts := 2 * NumPoints; 
    end 
  else 
    begin 
      rotation := Degrees/NumPoints;             //get number of degrees to turn per point 
      pts := NumPoints 
    end; 
  radius := yCenter; 

  {This loop defines the Cartesian points of the shape. Again, 
   I've added 90 degrees to the rotation angle so the shapes will 
   stand up rather than lie on their sides. Thanks again to Terry Smithwick and 
   David Ullrich for their trig help on CompuServe.} 
  for I := 0 to pts - 1 do begin 
    if DoStarShape then 
      if (I mod 2) = 0 then //which means that 
        radius := Round(radius/2) 
      else 
        radius := yCenter; 

    angle := ((I * rotation) + 90) * RadConvert; 
    x := xCenter + Round(cos(angle) * radius); 
    y := yCenter - Round(sin(angle) * radius); 
    arPts[I].X := x; 
    arPts[I].Y := y; 
  end; 

  Result := CreatePolygonRgn(arPts, pts, WINDING); 
end; 

procedure TForm1.CreateWindowHandle(const Params: TCreateParams);
begin
  inherited;
  Rgn := CreateMyPolygonRegion(BoundsRect, 5, True);
  SetWindowRgn(Handle, Rgn, TRUE);
end;

procedure TForm1.FormPaint(Sender: TObject); 
begin 
  Canvas.FillRect(ClientRect);
  Canvas.Brush.Color := clRed;
  FrameRgn(Canvas.Handle, Rgn, Canvas.Brush.Handle, 2, 2);
end; 

但是,在Windows 2000及更高版本中,操作系統資源更好,更高效,不再使用窗口區域。 從Delphi 6開始, TForm已經具有TransparentTransparentColor屬性,你應該使用它們。 Transparent屬性設置為True ,將TransparentColor屬性設置為一個唯一的顏色,該顏色不會出現在Form中的任何其他位置( clFuchsia是常用的),繪制所需的形狀並接近與表格相同的WidthHeightTBitmap並且其背景填充了Form的TranparentColor ,然后您可以在OnPaint事件中將TBitmap繪制到Form的Canvas上(或者將TBitmap放到客戶端對齊TImage這樣您就不必手動繪制Form)。 每當Windows復合Form的窗口進行顯示時,它將自動省略使用TransparentColor的最終像素。 最終結果是相同的 - 形狀窗口 - 但Windows將能夠更有效地管理透明度,命中測試,與其他窗口重疊/混合等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM