C言語でグラフィックス | ||
【2】図形の描画と消去 |
(14)フォント設定(GWsettxt関数)とテキスト描画(GWputtxt関数)
サンプルプログラム 〔gr217.c〕
フォントを「MS 明朝」に設定し、文字列をグラフィックス・ウィンドウに描画する。
#include <GrWin.h> int main(void) { int width = 640, height = 400; char *pt = "ABC123文字列描画のテスト"; GWopen(0); GWsize(-5, &width, &height); GWsize(-3, NULL, NULL); GWvport(0.0, 0.0, (float)width / (float)height, 1.0); GWindow(0.0, 0.0, (float)width - 1.0, (float)height - 1.0); /* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */ GWclear(-1); GWsettxt(0.0, 0.0, 1, -1, -1, "MS 明朝"); /* フォント設定 */ GWputtxt(50.0, 350.0, pt); /* 文字列描画 */ GWline(50.0, 350.0, 300.0, 350.0); /* 確認用の線 */ /* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */ GWquit(); return 0; }
int GWsettxt(float フォント高さ, float フォント傾き, int 基準点, int 文字色, int 背景色, char *フォント名); |
0:中央 | 1:左下(デフォルト) | 2:右下 | 3:右上 | 4:左上 |
5:左 | 6:下 | 7:右 | 8:上 |
int GWputtxt(float X, float Y, char *文字列); |
(15)文字列の長さ(GWgettxt関数)
サンプルプログラム 〔gr218.c〕
前のサンプルプログラムで、文字列の長さ分だけ線を引くためには、文字列の長さを知る必要がある。文字列の長さ分だけ線(下線)を描く。
#include <GrWin.h> int main(void) { int width = 640, height = 400; char *pt = "ABC123文字列描画のテスト"; float w, h, x, y; GWopen(0); GWsize(-5, &width, &height); GWsize(-3, NULL, NULL); GWvport(0.0, 0.0, (float)width / (float)height, 1.0); GWindow(0.0, 0.0, (float)width - 1.0, (float)height - 1.0); /* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */ GWclear(-1); GWsettxt(0.0, 0.0, 1, -1, -1, "MS 明朝"); GWgettxt(&w, &h, &x, &y, pt); /* 文字列のサイズ取得 */ GWputtxt(50.0, 350.0, pt); GWline(50.0, 350.0, w + 50.0, 350.0); /* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */ GWquit(); return 0; }
int GWgettxt(float *幅, float *高さ, float *X, float *Y, char *文字列); |
ここでは、文字列の長さ(幅)だけが知りたいので変数wだけを使って直線を描画している。直線はx座標50.0から描画しているので、終点のx座標は50.0+wとなる。y座標は変わらない。
Copyright © 2005 Hiroshi Masuda |