在Delphi中实现用鼠标当画笔画图

就是按鼠标时候会和WINDOWS画图软件里一样画图的功能要每一步的过程和用处还有控件的位置==~2点之前要~好的话再+50分有没有详细的流程步骤...我要写在纸上..还有... 就是按鼠标时候会和WINDOWS画图软件里一样画图的功能
要每一步的过程和用处还有控件的位置 = = ~2点之前要~
好的话再+50分
有没有详细的流程步骤...我要写在纸上..还有那个控件哪弄....谢...
详细的话提高分数 = =
展开
 我来答
airlean
2009-06-21 · TA获得超过134个赞
知道答主
回答量:65
采纳率:0%
帮助的人:52.1万
展开全部
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, Spin, ExtDlgs, Menus;

type
TForm2 = class(TForm)
Label1: TLabel;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
SpeedButton4: TSpeedButton;
SpeedButton5: TSpeedButton;
Button1: TButton;
ListBox1: TListBox;
ColorBox1: TColorBox;
SpinEdit1: TSpinEdit;
ColorBox2: TColorBox;
Button2: TButton;
Button3: TButton;
SavePictureDialog1: TSavePictureDialog;
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
SpeedButton6: TSpeedButton;
SpeedButton7: TSpeedButton;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Label1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton4Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
procedure SpeedButton5Click(Sender: TObject);
procedure SpeedButton3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure ColorBox1Change(Sender: TObject);
procedure SpinEdit1Change(Sender: TObject);
procedure ColorBox2Change(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure SpeedButton6Click(Sender: TObject);
procedure SpeedButton7Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;
xx, yy: array[0..1] of Integer;
t, d, Lx: Integer;
P0, P1: TPoint;
fn: string;
bmp1: TBitmap;
implementation

uses Unit3;

{$R *.dfm}

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
application.mainform.close;
end;

procedure TForm2.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var x0, y0, x1, y1, i: Integer; r, r0: real;
p: array[0..10] of TPoint;
pp: array[0..5] of TPoint;
begin
if d = 1 then begin
case Lx of
1: begin
canvas.Rectangle(P0.X, P0.Y, P1.X, P1.Y);
P1 := point(X, Y);
canvas.Rectangle(P0.X, P0.Y, P1.X, P1.Y);
end;
2: begin
canvas.Rectangle(P0.X, P0.Y, P1.X, P0.Y + (P1.X - P0.X));
P1 := point(X, Y);
canvas.Rectangle(P0.X, P0.Y, P1.X, P0.Y + (P1.X - P0.X));
end;
3: begin
canvas.Ellipse(P0.X, P0.Y, P1.X, P1.Y);
P1 := point(X, Y);
canvas.Ellipse(P0.X, P0.Y, P1.X, P1.Y);
end;
4: begin
canvas.Ellipse(P0.X, P0.Y, P1.X, P0.Y + (P1.X - P0.X));
P1 := point(X, Y);
canvas.Ellipse(P0.X, P0.Y, P1.X, P0.Y + (P1.X - P0.X));
end;
5: begin
canvas.MoveTo(P0.X, P0.Y); canvas.LineTo(P1.X, P1.Y);
P1 := point(X, Y);
canvas.MoveTo(P0.X, P0.Y); canvas.LineTo(P1.X, P1.Y);
end;
6: begin
x0 := round((P0.X + P1.X) / 2);
y0 := round((P0.Y + P1.Y) / 2);
if abs((P0.X - P1.X) / 2) < abs((P0.Y - P1.Y) / 2) then
r := abs((P0.X - P1.X) / 2)
else
r := abs((P0.Y - P1.Y) / 2);
for i := 0 to 5 do begin
x1 := round(x0 + r * cos(pi * 4 * i / 5 + pi / 2));
y1 := round(y0 - r * sin(4 * pi * i / 5 + pi / 2));
pp[i] := point(x1, y1);
end;
canvas.Polygon(pp);
P1 := point(X, Y); //新点,重画
x0 := round((P0.X + P1.X) / 2);
y0 := round((P0.Y + P1.Y) / 2);
if abs((P0.X - P1.X) / 2) < abs((P0.Y - P1.Y) / 2) then
r := abs((P0.X - P1.X) / 2) else r := abs((P0.Y - P1.Y) / 2);
for i := 0 to 5 do begin
x1 := round(x0 + r * cos(4 * pi * i / 5 + pi / 2));
y1 := round(y0 - r * sin(4 * pi * i / 5 + pi / 2));
pp[i] := point(x1, y1);
end;
canvas.Polygon(pp);
end; //五角星
7: begin
x0 := round((P0.X + P1.X) / 2);
y0 := round((P0.Y + P1.Y) / 2);
if abs((P0.X - P1.X) / 2) < abs((P0.Y - P1.Y) / 2) then
r := abs((P0.X - P1.X) / 2)
else
r := abs((P0.Y - P1.Y) / 2);
r0 := sin(pi / 10) * r / sin(7 * pi / 10);
for i := 0 to 4 do begin
x1 := round(x0 + r * cos(2 * pi * i / 5 + pi / 2));
y1 := round(y0 - r * sin(2 * pi * i / 5 + pi / 2));
p[2 * i] := point(x1, y1);
x1 := round(x0 + r0 * cos(2 * pi * i / 5 + pi / 2 + pi / 5));
y1 := round(y0 - r0 * sin(2 * pi * i / 5 + pi / 2 + pi / 5));
p[2 * i + 1] := point(x1, y1);
end;
p[10] := p[0];
canvas.Polygon(p);
P1 := point(X, Y); //新点,重画
x0 := round((P0.X + P1.X) / 2);
y0 := round((P0.Y + P1.Y) / 2);
if abs((P0.X - P1.X) / 2) < abs((P0.Y - P1.Y) / 2) then
r := abs((P0.X - P1.X) / 2) else r := abs((P0.Y - P1.Y) / 2);
r0 := sin(pi / 10) * r / sin(7 * pi / 10);
for i := 0 to 4 do begin
x1 := round(x0 + r * cos(2 * pi * i / 5 + pi / 2));
y1 := round(y0 - r * sin(2 * pi * i / 5 + pi / 2));
p[2 * i] := point(x1, y1);
x1 := round(x0 + r0 * cos(2 * pi * i / 5 + pi / 2 + pi / 5));
y1 := round(y0 - r0 * sin(2 * pi * i / 5 + pi / 2 + pi / 5));
p[2 * i + 1] := point(x1, y1);
end;
p[10] := p[0];
canvas.Polygon(p);
end; //实五角星

end;
end;
end;

procedure TForm2.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
canvas.MoveTo(X, Y); P0 := point(X, Y); P1 := P0;
d := 1;
end;

procedure TForm2.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var x0, y0, x1, y1, i: Integer; r, r0: real;
p: array[0..10] of TPoint;
pp: array[0..5] of TPoint;
begin
case Lx of
1: begin
bmp1.canvas.Rectangle(P0.X, P0.Y, P1.X, P1.Y);
end;
2: begin
bmp1.canvas.Rectangle(P0.X, P0.Y, P1.X, P0.Y + (P1.X - P0.X));
end;
3: begin
bmp1.canvas.Ellipse(P0.X, P0.Y, P1.X, P1.Y);
end;
4: begin
bmp1.canvas.Ellipse(P0.X, P0.Y, P1.X, P0.Y + (P1.X - P0.X));
end;
5: begin
bmp1.canvas.MoveTo(P0.X, P0.Y);
bmp1.canvas.LineTo(X, Y);
end;
6: begin
P1 := point(X, Y);
x0 := round((P0.X + P1.X) / 2);
y0 := round((P0.Y + P1.Y) / 2);
if abs((P0.X - P1.X) / 2) < abs((P0.Y - P1.Y) / 2) then
r := abs((P0.X - P1.X) / 2) else r := abs((P0.Y - P1.Y) / 2);
for i := 0 to 5 do begin
x1 := round(x0 + r * cos(4 * pi * i / 5 + pi / 2));
y1 := round(y0 - r * sin(4 * pi * i / 5 + pi / 2));
pp[i] := point(x1, y1);
end;
bmp1.canvas.Polygon(pp);
end; //画空五角星
7: begin
P1 := point(X, Y);
x0 := round((P0.X + P1.X) / 2);
y0 := round((P0.Y + P1.Y) / 2);
if abs((P0.X - P1.X) / 2) < abs((P0.Y - P1.Y) / 2) then
r := abs((P0.X - P1.X) / 2) else r := abs((P0.Y - P1.Y) / 2);
r0 := sin(pi / 10) * r / sin(7 * pi / 10);
for i := 0 to 4 do begin
x1 := round(x0 + r * cos(2 * pi * i / 5 + pi / 2));
y1 := round(y0 - r * sin(2 * pi * i / 5 + pi / 2));
p[2 * i] := point(x1, y1);
x1 := round(x0 + r0 * cos(2 * pi * i / 5 + pi / 2 + pi / 5));
y1 := round(y0 - r0 * sin(2 * pi * i / 5 + pi / 2 + pi / 5));
p[2 * i + 1] := point(x1, y1);
end;
p[10] := p[0];
bmp1.canvas.Polygon(p);
end; //画实五角星

end;
d := 0;
end;

procedure TForm2.Label1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbRight then
Label1.Font.Size := Label1.Font.Size + 1
else
Label1.Font.Size := Label1.Font.Size - 1;
Label1.Caption := 'Label1字号' + inttostr(Label1.Font.Size) + ' ';
end;

procedure TForm2.SpeedButton1Click(Sender: TObject);
begin
Lx := 1;
end;

procedure TForm2.SpeedButton4Click(Sender: TObject);
begin
Lx := 2;
end;

procedure TForm2.SpeedButton2Click(Sender: TObject);
begin
Lx := 3;
end;

procedure TForm2.SpeedButton5Click(Sender: TObject);
begin
Lx := 4;
end;

procedure TForm2.SpeedButton3Click(Sender: TObject);
begin
Lx := 5;
end;

procedure TForm2.SpeedButton6Click(Sender: TObject);
begin
Lx := 6;
end;

procedure TForm2.SpeedButton7Click(Sender: TObject);
begin
Lx := 7;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
bmp1 := TBitmap.Create;
fn := 'D:\存图\tmg.bmp';
try
bmp1.LoadFromFile(fn);
except
end;
Form2.canvas.Draw(0, 0, bmp1);
canvas.Pen.Mode := pmNotXor;
bmp1.canvas.Pen.Mode := pmcopy;
bmp1.Width := Form2.ClientWidth;
bmp1.Height := Form2.ClientHeight;
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
repaint;
//form2.Canvas.Draw(0,0,bmp1);
end;

procedure TForm2.ListBox1Click(Sender: TObject);
begin
canvas.Brush.Color := ColorBox1.Selected;
bmp1.canvas.Brush.Color := ColorBox1.Selected;
case ListBox1.ItemIndex of
0: canvas.Brush.Style := bsSolid;
1: canvas.Brush.Style := bsClear;
2: canvas.Brush.Style := bsBDiagonal;
3: canvas.Brush.Style := bsFDiagonal;
4: canvas.Brush.Style := bsCross;
5: canvas.Brush.Style := bsDiagCross;
6: canvas.Brush.Style := bsHorizontal;
7: canvas.Brush.Style := bsVertical;
end;
bmp1.canvas.Brush.Style := canvas.Brush.Style;
end;

procedure TForm2.ColorBox1Change(Sender: TObject);
begin
canvas.Brush.Color := ColorBox1.Selected;
bmp1.canvas.Brush.Color := ColorBox1.Selected;
end;

procedure TForm2.SpinEdit1Change(Sender: TObject);
begin
canvas.Pen.Width := SpinEdit1.Value;
bmp1.canvas.Pen.Width := SpinEdit1.Value;
end;

procedure TForm2.ColorBox2Change(Sender: TObject);
begin
canvas.Pen.Color := ColorBox2.Selected;
bmp1.canvas.Pen.Color := ColorBox2.Selected;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
form3.show;
self.Hide;
end;

procedure TForm2.Button3Click(Sender: TObject);
begin
if SavePictureDialog1.Execute then begin
fn := SavePictureDialog1.FileName;
bmp1.SaveToFile(fn);
end;
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
bmp1.Free;
end;

end.

我有源码
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式