如何使用delphi在区域中绘制边框?

时间:2012-04-20 00:56:59

标签: 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;

以这种方式设置表单的形状

enter image description here

现在我需要在形状中绘制一个颜色边框,但我无法弄清楚如何制作这个任务。我寻找的结果是这样的。

enter image description here

任何想法如何完成这项任务?

1 个答案:

答案 0 :(得分:4)

如果您要继续使用某个区域,请在表单的FrameRgn()事件中调用Win32 API OnPaint函数,例如:

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及更高版本中,操作系统资源更好,更高效,不再使用窗口区域。 {1}}自Delphi 6以来TFormTransparent属性可用,您应该使用它们。将TransparentColor属性设置为Transparent,将True属性设置为不会出现在表单中任何其他位置的唯一颜色(常用TransparentColor),绘制所需的属性形状并与clFuchsia接壤,该TBitmap与表格相同WidthHeight,其背景填充表格的TranparentColor,然后您可以绘制{{ 1}}在TBitmap事件中的表单Canvas上(或者将OnPaint放到客户端对齐TBitmap上,这样您就不必手动绘制表单)。每当Windows组合您的表单窗口进行显示时,它将自动省略使用TImage的最终像素。最终结果是相同的 - 形状窗口 - 但Windows将能够更有效地管理透明度,命中测试,覆盖/混合其他窗口等。