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

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

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

最佳答案

如果您要继续使用某个区域,请调用 Win32 API FrameRgn()表单的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 及更高版本上,不再使用窗口区域会更好、更有效地利用操作系统资源。 TFormTransparentTransparentColor自 Delphi 6 以来可用的属性,您应该改用它们。设置Transparent属性(property)给 True ,设置TransparentColor属性设置为不会出现在表单中其他任何位置的唯一颜色(通常使用 clFuchsia),绘制所需形状并与 TBitmap 接壤那是一样的WidthHeight作为表单,其背景填充了表单的TranparentColor ,然后就可以画出TBitmap到表格的CanvasOnPaint事件(或者将 TBitmap 放在客户端对齐 TImage 上,这样您就不必手动绘制表单)。每当 Windows 合成您的窗体窗口进行显示时,它会自动忽略使用 TransparentColor 的最终像素。 .最终结果是相同的 - 一个成形的窗口 - 但 Windows 将能够更有效地管理透明度、 HitTest 、与其他窗口重叠/混合等。

关于delphi - 如何使用delphi在区域中绘制边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10238887/

相关文章:

ios - XE7 更新 1 和 iOS 8.1 模拟器不工作

Delphi GLScene 将场景导出为 STL

delphi - Delphi中For循环后的循环变量是什么?

ios - 查找存储启动图像的位置

delphi - 如何为整个整数数组赋值?

delphi - utf-8字符串的base64编码

delphi - 在 Delphi 中存储一组值

delphi - 在 Delphi XE 或 Delphi XE2 中使用单元 HtmlHelpViewer 时,使用 regsvr32.exe 的 DLL 注册卡住

c - 将 C 源代码移植到 Delphi

delphi - 在 CLX TEdit 的 KeyPress 事件中拦截 TAB 键