• Delphi GDI(一)


     Delphi 7下IGDIPlus库的使用

     IGDI+是一个免费开源封装微软GDI+功能的Delphi库,该库使得可以用Delphi语言代码快速简短的实现复杂GDI+应用程序。
    官方网站:http://www.mitov.com/html/igdi_.html
    SourceForge:https://sourceforge.net/projects/igdiplus/
    
    安装方法:
    1.首先下载目前最新版的IGDI+,解压到任意目录下,这里我解压到D盘;
    2.打开Delphi 7→Tools→Environment Options→Library→Library path,然后点击右边的“...”,添加IGDI+的目录路径,确定完成,如下图所示:
    
    
    如要在程序中使用IGDI+的话
    1.首先在单元头uses内包含IGDIPlus,如:uses IGDIPlus;
    2.在窗体的OnPaint事件中,添加如下测试代码:
    procedure TForm1.FormPaint(Sender: TObject); 
    var 
      AGraphics: IGPGraphics; 
      APen: IGPPen; 
      AFont: IGPFont; 
      ABrush: IGPSolidBrush; 
      rc: TPoint; 
    begin 
      AGraphics := TGPGraphics.Create(Canvas); 
      AGraphics.SmoothingMode := SmoothingModeAntiAlias;//指定平滑(抗锯齿) 
      AGraphics.TextRenderingHint := TextRenderingHintAntiAlias;//指定使用过程中呈现的文本采用反走样 
      APen := TGPPen.Create($FF000000,3); 
      AGraphics.DrawLine(APen,5,5,100,100); 
     
      APen.Color := $FF0000FF; 
      APen.Width := 2; 
      AGraphics.DrawEllipse(APen,120,5,100,100); 
     
      Canvas.Font.Name := '微软雅黑'; 
      Canvas.Font.Size := 13; 
      AFont := TGPFont.Create(Canvas.Handle); 
      ABrush := TGPSolidBrush.Create($FFFF0000); 
      rc.X := 10; 
      rc.Y := 150; 
      AGraphics.DrawString('无幻博客'+#13#10+'http://blog.csdn.net/akof1314',AFont,rc,ABrush); 
    end; 
     
    3.运行结果如下图所示:
    
    IGDI+库下载:
    地址一:http://www.mitov.com/IGDIPlus.zip
    地址二:http://download.csdn.net/source/3039922
    View Code

    Delphi GDI+ 绘图

    绘图需要有纸、画笔、画刷; Delphi 有 Canvas、Pen、Brush.
    Canvas  :就是画布, 譬如窗体的 Canvas 属性, 就是窗体的画布;
    Pen :是画笔, 可以设置笔色、笔宽等等;
    Brush: 是画刷, 可以设置填充颜色等等.
    --------------------------------------------------------------------------------
    //举例:
    {绘制直线}
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      Canvas.Pen.Color := clRed; {设置画笔颜色}
      Canvas.Pen.Width := 2;     {设置画笔宽度}
      Canvas.MoveTo(10,10);      {直线起点}
      Canvas.LineTo(100,100);    {直线终点}
    {这里的 Canvas 是简写, 也可以写做: Self.Canvas 或 Form1.Canvas }
    end;
    {矩形填充}
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      Canvas.Brush.Color := clYellow; {设置画刷颜色, 也就是填充色}
      Canvas.FillRect(ClientRect);    {填充窗体客户区}
    end;
    最好用 TPaintBox 并在其 OnPaint 事件中绘图。而不是在Image控件中Image1.Canvas.Rectangle(0,0,Image1.Width,Image1.Height);
     
    --------------------------------------------------------------------------------
    TImage、TPaintBox、TPicture、TBitmap、TCanvas、TGraphic 的关系与区别
    TGraphic 是 TBitmap 的父类, 很多绘图函数的参数是 TGraphic, 但我们经常给函数的是 TBitmap;
    TBitmap 的很多功能都是继承自 TGraphic, 譬如:
    Width、Height、LoadFromFile、SaveToFile、LoadFromStream、SaveToStream;
    还有 Palette(调色板)、Transparent(是否透明) 等等.
    TGraphic 实用举例:
    
    --------------------------------------------------------------------------------
    var
      g: TGraphic;
    begin
      g := TBitmap.Create;
      g.LoadFromFile('c:	emp	est.bmp');
      Self.Canvas.StretchDraw(ClientRect, g);
      g.Free;
    end;
    --------------------------------------------------------------------------------
    
    TCanvas 是一个绘图表面, 像画圆、画方、画笔、画刷等等都是它的功能;
    控件的 Canvas 属性就是一个 TCanvas, 譬如:
    
    --------------------------------------------------------------------------------
    var
      cvs: TCanvas;
    begin
      cvs := Self.Canvas;
      cvs.Brush.Color := clYellow;
      cvs.Font.Color := clRed;
      cvs.Font.Name := '宋体';
      cvs.Font.Style := [fsBold];
      cvs.Font.Size := 24;
      cvs.TextOut(10, 10, '万一的 Delphi 博客');
    end;
    --------------------------------------------------------------------------------
    
    有些控件没有直接给 Canvas 属性, 我们也可以通过 TCanvas 获取它的绘图表面;
    一切看得见的控件都应该有绘图表面, 不然系统是怎么画的?
    譬如 TPanel 和 TButton 就没有 Canvas 属性, 没有是因为不常用, 如果需要可以这样:
    
    --------------------------------------------------------------------------------
    var
      cvs: TCanvas;
    begin
      cvs := TCanvas.Create;
      cvs.Handle := GetDC(Panel1.Handle);
      cvs.Pen.Width := 2;
      cvs.Pen.Color := clRed;
      cvs.Brush.Color := clYellow;
      cvs.Rectangle(5, 5, 50, 30);
      cvs.Free;
    end;
    --------------------------------------------------------------------------------
    
    再说 TBitmap, 它从 TGraphic 继承, 同时又把 TCanvas 纳为自己的属性;
    所以它有了处理图片和绘图的双重功能!
    给 TBitmap 也举个例子吧:
    
    --------------------------------------------------------------------------------
    var
      bit: TBitmap;
    begin
      bit := TBitmap.Create;
      bit.LoadFromFile('c:	emp	est.bmp');
      bit.Canvas.Brush.Style := bsClear;
      bit.Canvas.Pen.Color := clRed;
      bit.Canvas.Pen.Width := 2;
      bit.Canvas.Ellipse(2, 2, 88, 66);
      Self.Canvas.Draw(0, 0, bit);
      bit.Free;
    end;
    --------------------------------------------------------------------------------
    
    TPicture 是为了处理更多种格式的图片(譬如: ico、wmf 等)而存在的;
    但它把 TGraphic、TCanvas、TBitmap 的功能通通借用过来, 所以功能更强大.
    TPicture 和 TGraphic 重复的功能(譬如: LoadFromFile)都是内部调用的 TGraphic;
    但如果要给它绘图, 需要调用: TPicture.Bitmap.Canvas.
    下面的例子是用 TPicture 画了个十字图标(显示并保存起来):
    
    --------------------------------------------------------------------------------
    var
      pic: TPicture;
    begin
      pic := TPicture.Create;
      pic.Bitmap.SetSize(32, 32);
      pic.Bitmap.Canvas.Pen.Color := clRed;
      pic.Bitmap.Canvas.Pen.Width := 8;
      pic.Bitmap.Canvas.MoveTo(0, 16);
      pic.Bitmap.Canvas.LineTo(32, 16);
      pic.Bitmap.Canvas.MoveTo(16, 0);
      pic.Bitmap.Canvas.LineTo(16, 32);
      Self.Canvas.Draw(4, 4, pic.Graphic);
      pic.SaveToFile('c:	emp	est.ico');
      pic.Free;
    end;
    --------------------------------------------------------------------------------
    
    TImage 则主要是为了显示图片, 它主要包含的是 TPicture, 有了 TPicture 就有了上面的一切;
    但因它是从 TControl -> TGraphicControl 继承下来的, 所以它具备了控件的基本能力(事件、消息等等).
    TPaintBox 主要用于绘图, 没有处理图片的能力, 所以只包含了 TCanvas;
    它也是从 TControl -> TGraphicControl 继承, 是能够交互的控件了.
    很显然, TImage 比 TPaintBox 的能力强大; 但仅就绘图来讲, 还是 TPaintBox 轻便些.
    View Code

    Delphi GDI+ 文本输出

    unit Unit1;
    interface
    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, CheckLst;
    type
    TForm1 = class(TForm)
    CheckListBox1: TCheckListBox;
    procedure FormPaint(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CheckListBox1Click(Sender: TObject);
    end;
    var
    Form1: TForm1;
    implementation
    {$R *.dfm}
    uses GDIPOBJ, GDIPAPI;
    var fs: Integer;
    procedure TForm1.CheckListBox1Click(Sender: TObject);
    const
    fsArr: array[0..5] of Integer = (FontStyleRegular,
    FontStyleBold,
    FontStyleItalic,
    FontStyleBoldItalic,
    FontStyleUnderline,
    FontStyleStrikeout);
    var
    i: Integer;
    begin
    fs := 0;
    for i := 0to CheckListBox1.Items.Count - 1do
    if CheckListBox1.Checked[i] then
    fs := fs or fsArr[i];
    Repaint;
    end;
    procedure TForm1.FormCreate(Sender: TObject);
    begin
    CheckListBox1.Align := alLeft;
    CheckListBox1.Items.CommaText := 'FontStyleRegular,' +
    'FontStyleBold,' +
    'FontStyleItalic,' +
    'FontStyleBoldItalic,' +
    'FontStyleUnderline,' +
    'FontStyleStrikeout';
    CheckListBox1.Checked[0] := True;
    end;
    procedure TForm1.FormPaint(Sender: TObject);
    var
    g: TGPGraphics;
    sb: TGPSolidBrush;
    font: TGPFont;
    begin
    g := TGPGraphics.Create(Canvas.Handle);
    sb := TGPSolidBrush.Create(aclRed);
    font := TGPFont.Create('微软雅黑', 50, fs);
    g.DrawString('Delphi', -1, font, MakePoint(CheckListBox1.Width + 0.0, 0), sb);
    font.Free;
    sb.Free;
    g.Free;
    end;
    end.
    FontStyle = Integer;
    const
    FontStyleRegular = Integer(0); {普通文本}
    FontStyleBold = Integer(1); {加粗文本}
    FontStyleItalic = Integer(2); {倾斜文本}
    FontStyleBoldItalic = Integer(3); {加粗并倾斜文本}
    FontStyleUnderline = Integer(4); {带下划线的文本}
    FontStyleStrikeout = Integer(8); {中间有直线通过的文本}
    Type
    TFontStyle = FontStyle;
    文本样式类型表:
    Delphi 微软 说明
    FontStyleBold Bold 加粗文本。
    FontStyleItalic Italic 倾斜文本。
    FontStyleRegular Regular 普通文本。
    FontStyleStrikeout Strikeout 中间有直线通过的文本。
    FontStyleUnderline Underline 带下划线的文本。
    坐标单位类型表:
    Delphi 微软 说明
    UnitDisplay Display 指定显示设备的度量单位。通常,视频显示使用的单位是像素;打印机使用的单位是 1/100 英寸。
    UnitDocument Document 将文档单位(1/300 英寸)指定为度量单位。
    UnitInch Inch 将英寸指定为度量单位。
    UnitMillimeter Millimeter 将毫米指定为度量单位。
    UnitPixel Pixel 将设备像素指定为度量单位。
    UnitPoint Point 将打印机点(1/72 英寸)指定为度量单位。
    UnitWorld World 将世界坐标系单位指定为度量单位。
    文本呈现质量模式:
    Delphi 微软 说明
    TextRenderingHintAntiAlias AntiAlias 在无提示的情况下使用每个字符的消除锯齿效果标志符号位图来绘制字符。由于采用了 AntiAlias,质量会得到改善。由于关闭了提示,主干宽度差可能会比较明显。
    TextRenderingHintAntiAliasGridFit AntiAliasGridFit 在有提示的情况下使用每个字符的消除锯齿效果标志符号位图来绘制字符。由于采用了 AntiAlias,质量会得到大大改善,但同时会增加性能成本。
    TextRenderingHintClearTypeGridFit ClearTypeGridFit 在有提示的情况下使用每个字符的标志符号 ClearType 位图来绘制字符。这是质量最高的设置。用于利用 ClearType 字体功能。
    TextRenderingHintSingleBitPerPixel SingleBitPerPixel 使用每个字符的标志符号位图来绘制字符。不使用提示。
    TextRenderingHintSingleBitPerPixelGridFit SingleBitPerPixelGridFit 使用每个字符的标志符号位图来绘制字符。提示用于改善字符在主干和弯曲部分的外观。
    TextRenderingHintSystemDefault SystemDefault 在有系统默认呈现提示的情况下使用每个字符的标志符号位图来绘制字符。将采用用户为系统选择的任何字体修匀设置来绘制文本。
    //颜色透明度var  g: TGPGraphics;  sb: TGPSolidBrush;begin  g := TGPGraphics.Create(Canvas.Handle);  sb := TGPSolidBrush.Create(MakeColor(128,255,0,0)); {128表示半透明}  g.FillRectangle(sb,10,10,100,100);  sb.Free;  g.Free;end;
    --------------------------------------------------------------------------------
    //使用 GDI+ 的颜色类型var  g: TGPGraphics;  sb: TGPSolidBrush;  color: TGPColor; {其实颜色是 DWORD 类型的}begin  g := TGPGraphics.Create(Canvas.Handle);  color := aclRed;  sb := TGPSolidBrush.Create(color);  g.FillRectangle(sb,10,10,100,100);  sb.Free;  g.Free;end;
    View Code
  • 相关阅读:
    elk2
    elk
    skywalking学习ppt
    Spring Boot]SpringBoot四大神器之Actuator
    黑马程序员spring data jpa 2019年第一版本
    css总结7:盒子模型理解
    css总结5:px、em、rem区别介绍
    css总结4:input 去掉外边框,placeholder的字体颜色、字号
    css总结3:Flex 布局教程:Flex-demos(转)
    css总结2:Flex 布局教程:Flex 语法(转)
  • 原文地址:https://www.cnblogs.com/blogpro/p/11345913.html
Copyright © 2020-2023  润新知