根据位图做出漂亮的不规则FORM

samool 发布暂无评论7915 阅读

AI摘要

本文介绍了如何使用Delphi编程语言和位图来创建不规则形状的窗体。文章提供了详细的代码实现,包括创建窗体、按钮和图片控件,以及在窗体创建时根据位图生成不规则区域,并设置窗体的区域。此外,还提供了鼠标按下事件的处理,用于关闭窗体。

unit Unit1; 

interface 

uses 
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
 ExtCtrls, StdCtrls, Buttons; 

type 
 TForm1 = class(TForm) 
 Image1: TImage; 
 Button1: TButton; 
 procedure FormCreate(Sender: TObject); 
 procedure Button1Click(Sender: TObject); 
 procedure Image1MouseDown(Sender: TObject; Button: TMouseButton; 
 Shift: TShiftState; X, Y: Integer); 
 private 
  function CreateRegion(wMask: TBitmap; wColor: TColor;hControl: THandle): HRGN; 
  { Private declarations } 
 public 
  { Public declarations } 
end; 

var 
 Form1: TForm1; 

implementation 

{$R *.DFM} 

function Tform1.CreateRegion(wMask:TBitmap;wColor:TColor;hControl:THandle): HRGN; 
var 
 dc, dc_c: HDC; 
 rgn: HRGN; 
 x, y: integer; 
 coord: TPoint; 
 line: boolean; 
 color: TColor; 
begin 
 dc := GetWindowDC(hControl); 
 dc_c := CreateCompatibleDC(dc); 
 SelectObject(dc_c, wMask.Handle); 
 BeginPath(dc); 
 for x:=0 to wMask.Width-1 do 
 begin 
  line := false; 
  for y:=0 to wMask.Height-1 do 
  begin 
   color := GetPixel(dc_c, x, y); 
   if not (color = wColor) then 
   begin 
    if not line then 
    begin 
     line := true; 
     coord.x := x; 
     coord.y := y; 
    end; 
   end; 
   if (color = wColor) or (y=wMask.Height-1) then 
   begin 
    if line then 
    begin 
     line := false; 
     MoveToEx(dc, coord.x, coord.y, nil); 
     LineTo(dc, coord.x, y); 
     LineTo(dc, coord.x + 1, y); 
     LineTo(dc, coord.x + 1, coord.y); 
     CloseFigure(dc); 
    end; 
   end; 
  end; 
 end;
 EndPath(dc); 
 rgn := PathToRegion(dc); 
 ReleaseDC(hControl, dc); 
 Result := rgn; 
end; 

procedure TForm1.FormCreate(Sender: TObject); 
var 
 w1:TBitmap; 
 w2:TColor; 
 rgn: HRGN; 
begin 
 w1:=TBitmap.Create; 
 w1.Assign(image1.Picture.Bitmap); 
 w2:=w1.Canvas.Pixels[0,0]; 
 rgn := CreateRegion(w1,w2,Handle); 
 if rgn<>0 then 
 begin 
  SetWindowRgn(Handle, rgn, true); 
 end; 
 w1.Free; 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
 Close; 
end; 

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton; 
Shift: TShiftState; X, Y: Integer); 
begin 
 ReleaseCapture; 
 SendMessage(Handle, WM_SYSCOMMAND, $F012, 0); 
end; 

end. 

文章作者:mantousoft
文章来源:2ccc.com
作者邮箱:[email protected]

  1. 01SaMDeditor V2.4 更新内容2026-12-02
  2. 02恭喜《轻武爱学习》小程序备案成功!2026-11-24
  3. 03SaView 图阅 1.1.1 更新日志2026-11-18
  4. 04Book2EPUB 1.0.4 更新日志2026-11-18
  5. 05SaView 图阅 一款轻便纯粹的看图工具2026-11-16
  6. 06免费工具Book2EPUB:轻松将电子书转换为 EPUB2026-11-11
  7. 07SaBlog2026 主题功能介绍(v1.1.36)2026-10-26
  8. 08PHP2Shell 重大安全漏洞事件深度分析报告2026-10-23
  9. 09LiteLLM v1.106.0 更新内容总结2026-10-20
  10. 10AI Native 受控软件工程2026-10-12
COMMENTS / 一纸留言

读者留言 0 条

一纸留言,便是山水间的一次相逢。

还没有留言,欢迎写下第一段交流。

WRITE A COMMENT

写下你的留言