在C++Builder中如何绘通过TPaintBox控件制出一个坐标轴
时间: 2024-03-05 22:48:27 浏览: 188
用C++在坐标轴上画线
4星 · 用户满意度95%
在C++Builder中,可以使用TPaintBox控件来绘制坐标轴。具体步骤如下:
1. 在Form上放置一个TPaintBox控件。
2. 在TPaintBox的OnPaint事件中进行绘制,代码如下:
```c++
void __fastcall TForm1::PaintBox1Paint(TObject *Sender)
{
TPaintBox *paintBox = dynamic_cast<TPaintBox*>(Sender);
TCanvas *canvas = paintBox->Canvas;
// 画坐标轴
canvas->Pen->Color = clBlack;
canvas->Pen->Width = 2;
canvas->MoveTo(50, paintBox->Height - 50);
canvas->LineTo(50, 50);
canvas->LineTo(paintBox->Width - 50, 50);
// 画X轴刻度
canvas->Pen->Color = clRed;
canvas->Pen->Width = 1;
for (int i = 0; i < 10; i++)
{
int x = 50 + (paintBox->Width - 100) / 10 * (i + 1);
canvas->MoveTo(x, 50);
canvas->LineTo(x, 40);
}
// 画Y轴刻度
canvas->Pen->Color = clBlue;
canvas->Pen->Width = 1;
for (int i = 0; i < 10; i++)
{
int y = paintBox->Height - 50 - (paintBox->Height - 100) / 10 * (i + 1);
canvas->MoveTo(50, y);
canvas->LineTo(40, y);
}
}
```
3. 在Form的OnResize事件中,重新计算坐标轴的位置和大小,代码如下:
```c++
void __fastcall TForm1::FormResize(TObject *Sender)
{
PaintBox1->Left = 10;
PaintBox1->Top = 10;
PaintBox1->Width = ClientWidth - 20;
PaintBox1->Height = ClientHeight - 20;
PaintBox1->Invalidate();
}
```
这样就可以在TPaintBox控件上绘制出一个简单的坐标轴了。
阅读全文