跳到主要内容

画布(Canvas)

HiEasyX 中的画布允许你在 HiEasyX GUI 中通过 HXBufferPainter 自由绘制你想要的图形。

在 HiEasyX 中,画布的原型函数定义如下:

void Canvas(HXPoint Size, const std::function<void(HXBufferPainter*)> &Callback);

画布控件并没有资料结构体。

参数

参数含义默认取值
Size画布的大小
Callback用户自定义的绘图回调函数,应形如 void(HXBufferPainter*),接受一个 HXBufferPainter 的指针作为目标画布。
提示

有关 HXBufferPainter 的描述,请参考对象 - 绘图对象有关章节。

示例

该示例代码同样可以在代码仓库/example/EasyX/Canvas.cpp 中找到。

#include <include/hex.h>
#include <include/impl/EasyX/hex_impl_easyx.h>

double f(const double &x) {
return -(sin(clock() / 1000.f * x) * tan(x));
}

void Draw(HXBufferPainter *Painter, HXPoint Size) {
const int width = Size.X;
const int height = Size.Y;
const int halfWidth = width / 2;
const int halfHeight = height / 2;

Painter->DrawLine({0, halfHeight}, {width, halfHeight}, HXColor{255, 0, 0, 255});
Painter->DrawLine({halfWidth, 0}, {halfWidth, height}, HXColor{255, 0, 0, 255});

constexpr double step = 0.06;
constexpr double scale = 17;
double x0 = -halfWidth * step;

for (int pixel = -halfWidth; pixel < halfWidth; ++pixel) {
double result = f(x0);
double next = f(x0 + step);

double omega = f(x0 + step / 100);

if (next > result) {
if (omega > result) {
Painter->DrawLine({pixel + halfWidth, (int)(result * scale) + halfHeight},
{pixel + 1 + halfWidth, (int)(next * scale) + halfHeight}, HXColor{255, 255, 0, 255});
}
} else {
if (omega < result) {
Painter->DrawLine({pixel + halfWidth, (int)(result * scale) + halfHeight},
{pixel + 1 + halfWidth, (int)(next * scale) + halfHeight}, HXColor{255, 255, 0, 255});
}
}

x0 += step;
}
}

int main() {
initgraph(640, 480);

BeginBatchDraw();

HX::HXInitForEasyX();
HX::SetBuffer(GetWorkingImage());

setbkcolor(RGB(180, 180, 180));

HX::WindowProfile windowProfile;
windowProfile.Size = {600, 400};

while (true) {
cleardevice();

HX::HXBegin();

ExMessage message{};
while (peekmessage(&message)) {
HX::PushMessage(HX::GetHXMessage(&message));
}

HX::Window(HXStr("画布控件示例"), windowProfile);
HX::Canvas(windowProfile.Size, Draw);

HX::End();
HX::Render();

FlushBatchDraw();

Sleep(1);
}

return 0;
}