画线的问题

茹果伱在 2011-03-23 08:47:56
我要在PaintBox上面画线,用moveto和lianto怎么化成虚线,请帮我写下具体代码,还有当我鼠标点下的时候,茹果鼠标所在的坐标和直线的距离(这个距离自己算)小于一定值,就显示成虚线,以证明选中了,茹果是点在线的端点,就显示成虚线的框框,怎么实现啊?
...全文
107 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
我不懂电脑 2011-03-23
  • 打赏
  • 举报
回复
建议你用addflow很简单。
画虚线只要设置TPen->Style

void __fastcall TForm1::FormActivate(TObject *Sender)
{
WindowState = wsMaximized;
Timer1->Interval = 50;
randomize();
}

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
x = random(Screen->Width - 10);
y = random(Screen->Height - 10);
Canvas->Pen->Color = (Graphics::TColor) random(65535);
switch (random(5))
{
case 0: Canvas->Pen->Style = psSolid; break;
case 1: Canvas->Pen->Style = psDash; break;
case 2: Canvas->Pen->Style = psDot; break;
case 3: Canvas->Pen->Style = psDashDot; break;
case 4: Canvas->Pen->Style = psDashDotDot; break;
}
Canvas->Rectangle(x, y, x + random(400), y + random(400));
}
xjq2003 2011-03-23
  • 打赏
  • 举报
回复
PaintBox1->Canvas->Pen->Width = 2;
//2改大一些?
PaintBox1->Canvas->Pen->Style = psInsideFrame;

//是不是psInsideFrame;类型的问题啊
茹果伱在 2011-03-23
  • 打赏
  • 举报
回复
//---------------------------------------------------------------------------

#include <vcl.h>
#include <math.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
int style = -1, a1=200, b1=60, a2=100, b2=60;
int oldx, oldy, newx, newy;
class lines
{
public:
int x1, y1, x2, y2;
lines(int a1, int b1, int a2, int b2)
{
x1 = a1;
y1 = b1;
x2 = a2;
y2 = b2;
}
};
void write(TPaintBox *Sender, int a1, int b1, int a2, int b2)
{
Sender->Canvas->MoveTo(a1, b1);
Sender->Canvas->LineTo(a2, b2);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
lines a(a1, b1, a2, b2);
PaintBox1->Canvas->Pen->Color = clRed;
PaintBox1->Canvas->Pen->Width = 2;
PaintBox1->Canvas->Pen->Style = psInsideFrame;
write(PaintBox1, a.x1, a.y1, a.x2, a.y2);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PaintBox1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
oldx = X;
oldy = Y;
lines a(a1, b1, a2, b2);
if(sqrt(pow((X-a.x1), 2) + pow((Y-a.y1), 2)) <= 2)//点在(x1,y1)
{
style = 1;
PaintBox1->Canvas->Pen->Color = Form1->Color;
write(PaintBox1, a.x1, a.y1, a.x2, a.y2);
}
else
if(sqrt(pow((X-a.x2), 2) + pow((Y-a.y2), 2)) <= 2)//点在(x2,y2)
{
style = 2;
PaintBox1->Canvas->Pen->Color = Form1->Color;
write(PaintBox1, a.x1, a.y1, a.x2, a.y2);
}
else
if((a.x1 == a.x2)&&(((Y >= a.y1)&&(Y <= a.y2))||((Y <= a.y1)&&(Y >= a.y2)))//与y轴平行
&&(abs(X-a.x1) < 10))
{
style = 0;
PaintBox1->Canvas->Pen->Color = Form1->Color;
write(PaintBox1, a.x1, a.y1, a.x2, a.y2);
}
else
if((a.y1 == a.y2)&&(((X >= a.x1)&&(X <= a.x2))||((X<= a.x1)&&(X >= a.x2))) //与X轴平行
&&(abs(Y-a.y1) < 10))
{
style = 0;
PaintBox1->Canvas->Pen->Color = Form1->Color;
write(PaintBox1, a.x1, a.y1, a.x2, a.y2);
}
else
if(((X >= a.x1)&&(X <= a.x2)||(X<= a.x1)&&(X >= a.x2))
&&((Y >= a.y1)&&(Y <= a.y2)||((Y <= a.y1)&&(Y >= a.y2)))
&&(abs((X*(a.y2-a.y1)+Y*(a.x1-a.x2)-a.x1*a.y2+a.y1*a.x2))/sqrt(pow(X, 2)+pow(Y, 2))) < 5)
{
style = 0;
PaintBox1->Canvas->Pen->Color = Form1->Color;
write(PaintBox1, a.x1, a.y1, a.x2, a.y2);
}
else
style = 4;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PaintBox1MouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
newx = X;
newy = Y;
lines a(a1, b1, a2, b2);
if(style == 1)
{
PaintBox1->Canvas->Pen->Color = Form1->Color;
write(PaintBox1, a.x1, a.y1, a.x2, a.y2);
PaintBox1->Canvas->Pen->Color = clRed;
PaintBox1->Canvas->Pen->Width = 2;
write(PaintBox1, X, Y, a.x2, a.y2);
a1 = X; //新的 起点坐标
b1 = Y;
}
else
if(style == 2)
{
PaintBox1->Canvas->Pen->Color = Form1->Color;
write(PaintBox1, a.x1, a.y1, a.x2, a.y2);
PaintBox1->Canvas->Pen->Color = clRed;
PaintBox1->Canvas->Pen->Width = 2;
write(PaintBox1, a.x1, a.y1, X, Y);
a2 = X; //新的 起点坐标
b2 = Y;
}
else
if(style == 0)
{
PaintBox1->Canvas->Pen->Color = Form1->Color; //画笔颜色和Form原色一样,起到橡皮差的作用
write(PaintBox1, a.x1, a.y1, a.x2, a.y2);
PaintBox1->Canvas->Pen->Color = clRed;
PaintBox1->Canvas->Pen->Width = 2;
write(PaintBox1, a.x1+(newx-oldx), a.y1+(newy-oldy), a.x2+(newx-oldx), a.y2+(newy-oldy));
a1 = a.x1+(newx-oldx); //新的坐标
b1 = a.y1+(newy-oldy);
a2 = a.x2+(newx-oldx);
b2 = a.y2+(newy-oldy);
}
style = -1;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button2Click(TObject *Sender)
{
ShowMessage(IntToStr(style));
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------


void __fastcall TForm1::PaintBox1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
lines a(a1, b1, a2, b2);
if(style == 1)
{
PaintBox1->Canvas->Pen->Color = Form1->Color;
write(PaintBox1, a.x1, a.y1, a.x2, a.y2);
PaintBox1->Canvas->Pen->Color = clRed;
PaintBox1->Canvas->Pen->Width = 2;
write(PaintBox1, X, Y, a.x2, a.y2);
a1 = X;
b1 = Y;
}
else
if(style == 2)
{
PaintBox1->Canvas->Pen->Color = Form1->Color;
write(PaintBox1, a.x1, a.y1, a.x2, a.y2);
PaintBox1->Canvas->Pen->Color = clRed;
PaintBox1->Canvas->Pen->Width = 2;
write(PaintBox1, a.x1, a.y1, X, Y);
a2 = X; //新的 起点坐标
b2 = Y;
}
}
//---------------------------------------------------------------------------
xjq2003 2011-03-23
  • 打赏
  • 举报
回复
代码发来,我帮你测试一下
茹果伱在 2011-03-23
  • 打赏
  • 举报
回复
怎么没人啊 急。。
茹果伱在 2011-03-23
  • 打赏
  • 举报
回复
来人帮帮忙哦

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧