xege icon indicating copy to clipboard operation
xege copied to clipboard

`ege::draw_text` 的绘制的文字宽度与 `ege::textwidth` 给出的不一致

Open chirsz-ever opened this issue 5 years ago • 2 comments

EGE 版本:20.08 编译器:TDMGCC64 9.2 复现代码:

#include <ege.h>

void draw_string_with_rect(const WCHAR text[], float x, float y) {
	ege::setcolor(ege::BLACK);
	ege::outtextxy(x, y, text);
	int w = ege::textwidth(text);
	int h = ege::textheight(text);
	ege::setcolor(ege::RED);
	ege::rectangle(x, y, x + w, y + h);
}

void draw_string_with_rect_new(const WCHAR text[], float x, float y) {
	ege::setcolor(ege::BLACK);
	ege::ege_drawtext(text, x, y);
	int w = ege::textwidth(text);
	int h = ege::textheight(text);
	ege::setcolor(ege::RED);
	ege::ege_rectangle(x, y, w, h);
}

int main() {
	const WCHAR* strs[] = {
		L"string 1",
		L"字符串 2",
		L"长一点点的字符串",
		L"fffffffffffffff",
		L"DDDDDDDDDDDDDDD",
	};

	ege::initgraph(640, 480, 0);
	ege::setrendermode(ege::RENDER_MANUAL);
	ege::setbkcolor(ege::WHITE);
	ege::setfont(16, 0, "Gadugi");

	// GDI
	for(int i = 0; i < sizeof(strs)/sizeof(strs[0]); ++i) {
		draw_string_with_rect(strs[i], 20, 20 + i * 30);
	}

	// GDI+
	for(int i = 0; i < sizeof(strs)/sizeof(strs[0]); ++i) {
		draw_string_with_rect_new(strs[i], 380, 20 + i * 30);
	}

	ege::getch();
	return 0;
}

运行结果: 图片

chirsz-ever avatar Sep 03 '20 04:09 chirsz-ever

GDI+ 的 Graphics::MeasureString 算出来的结果也不太准,可能需要用 Graphics::DrawDriverString 函数重构 ege::ege_drawtext

chirsz-ever avatar Sep 03 '20 07:09 chirsz-ever

👍

wysaid avatar Sep 04 '20 02:09 wysaid