Visual C++ 绘图库

目录

模拟 Borland BGI 的函数:

arc bar bar3d circle cleardevice clearviewport
closegraph detectgraph drawpoly ellipse fillellipse fillpoly
floodfill getarccoords getaspectratio getbkcolor getcolor getdefaultpalette
getdrivername getfillpattern getfillsettings getgraphmode getimage getlinesettings
getmaxcolor getmaxmode getmaxx getmaxy getmodename getmoderange
getpalette getpalettesize getpixel gettextsettings getviewsettings getx
gety graphdefaults grapherrormsg _graphfreemem _graphgetmem graphresult
imagesize initgraph installuserdriver installuserfont line linerel
lineto moverel moveto outtext outtextxy pieslice
putimage putpixel rectangle registerbgidriver registerfarbgidriver registerbgifont
registerfarbgifont restorecrtmode sector setactivepage setallpalette setaspectratio
setbkcolor setcolor setfillpattern setfillstyle setgraphbufsize setgraphmode
setlinestyle setpalette setrgbpalette settextjustify settextstyle setusercharsize
setviewport setvisualpage setwritemode textheight textwidth  

新增函数:

SetFont GetFont GetGraphicsVer      

模拟 Borland BGI 的常量、数据类型、全局变量:

arccoordstype CGA_COLORS COLORS EGA_colors fill_patterns fillsettingstype
font_names graphics_drivers graphics_errors graphics_modes HORIZ_DIR line_styles
line_widths linesettingstype MAXCOLORS palettetype pointtype putimage_ops
text_just textsettingstype USER_CHAR_SIZE VERT_DIR viewporttype  

基本概念

颜色:

表示颜色与 Borland BGI 的区别最大,因为 Windows 下的颜色十分丰富,远不是过去 DOS 能比的。在这个绘图函数库中,有三种办法表示颜色:

1. 用 16 进制的颜色表示,形式为:
0x00bbggrr (bb=蓝,gg=绿,rr=红)
例如,设置绘图色为蓝色,可以用:
setcolor(0x00ff0000);

2. 用 RGB 宏协助配色,形式为:
RGB(rr, gg, bb);
rr, gg, bb 取值范围是 0~255。例如,设置绘图色为蓝色,可以用:
setcolor(RGB(255, 30, 70));

3. 用预定义颜色:
BLACK: 黑,BLUE: 蓝,GREEN: 绿,CYAN: 青,RED: 红,
MAGENTA: 紫,BROWN: 棕,LIGHTGRAY: 浅灰,DARKGRAY: 深灰,
LIGHTBLUE: 亮蓝,LIGHTGREEN: 亮绿,LIGHTCYAN: 亮青,LIGHTRED: 亮红,
LIGHTMAGENTA: 亮紫,YELLOW: 黄,WHITE: 白
例如,设置绘图色为蓝色,可以用:
setcolor(BLUE);

模拟 Borland BGI 的函数说明

初始化图形环境

void initgraph(int Width, int Height);
void initgraph(int Width, int Height, int Flag);
参数说明:
Width: 绘图环境的宽度。
Height: 绘图环境的高度。
Style: 绘图环境的样式,当值为 SHOWCONSOLE 表示可以保留原控制台窗口。默认为 NULL。

关闭图形环境

void closegraph();

清除屏幕

void cleardevice();
说明:
用当前背景色清空屏幕,并将当前点移至 (0, 0)。

获取当前绘图前景色

COLORREF getcolor();

设置当前绘图前景色

void setcolor(COLORREF color);
参数说明:
color: 要设置的颜色。

获取当前绘图背景色

COLORREF getbkcolor();

设置当前绘图背景色

void setbkcolor(COLORREF color);
参数说明:
color: 要设置的颜色。
注:
与 Borland BGI 库有所不同,BGI 是通过调色板设置的背景色,因此可以在保持绘图内容不变的前提下更换背景色。但是这个绘图库取消了调色板的概念,因此无法自动替换现有填充的背景色。

获取视图信息

void getviewsettings(struct viewporttype *viewport);
参数说明:
viewport: 指向 viewporttype 结构体,保存返回的视图信息。

设置视图

void setviewport(int left, int top, int right, int bottom, int clip);
参数说明:
left, top, right, bottom: 表示新视图的矩形区域,(left, top) 将成为新的原点。
clip: 是否裁剪,如果非零,所有超出视图区域的绘图都会被裁剪掉。
备注:
执行后,"当前点"会被移动到新的视图的(0,0)位置。

清空视图

void clearviewport();

获取当前线形

void getlinesettings(struct linesettingstype *lineinfo);

设置当前线形

void setlinestyle(int linestyle, unsigned int upattern, int thickness);
参数说明:
linestyle: 线样式。相比较 BGI 库,该库的线样式相对紧凑些。
upattern: 当线样式为 USERBIT_LINE 时,改数据表示自定义的线形。二进制位为 1 表示画线,为 0 表示空白。与 BGI 库不同的是,BGI 库该字段为 16 位,而本绘图库该字段为 32 位。
thickness: 线粗细。

获取填充类型

void getfillsettings(struct fillsettingstype *fillinfo);

设置填充类型

void setfillstyle(int pattern, int color);
参数说明:
pattern: 填充类型。详见 fill_patterns。
color: 填充颜色。

获取自定义填充类型

void getfillpattern(char *pattern);
参数说明:
pattern: 指向 8 个元素的字符数组指针。

设置自定义填充类型

void setfillpattern(const char *upattern, int color);
参数说明:
upattern: 指向 8 个元素的字符数组指针,每个元素 8 个二进制位,由此构成了 8x8 的区域图案,用以填充。
color: 填充颜色。

获取当前缩放因子

void getaspectratio(int *xasp, int *yasp);

设置当前缩放因子

void setaspectratio(int xasp, int yasp);
参数说明:
xasp, yasp: x、y 方向上的缩放因子,实际缩放值为 x/10000 和 y/10000。

设置绘图位操作模式

void setwritemode(int mode);
参数说明:
mode: 位操作模式,见结构 putimage_ops。
备注:
BGI 库仅支持 COPY_PUT 和 XOR_PUT 两种位操作。本绘图库除了模拟原有两种位操作外,还实现了 NOT_PUT、AND_PUT、OR_PUT 三种位操作。

重置所有绘图设置为默认值

void graphdefaults();
备注:
该函数重置视图、当前点、绘图色、背景色、线形、填充类型、字体。

获取点的颜色

COLORREF getpixel(int x, int y);
参数说明:
x, y: 要获取颜色坐标。
返回值:
指定点的颜色。

画点

void putpixel(int x, int y, COLORREF color);
参数说明:
x, y: 点的坐标。
color: 点的颜色。

移动当前点

void moveto(int x, int y);
void moverel(int dx, int dy);
参数说明:
x, y: 新的当前点坐标。
dx, dy: 当前点的偏移量。
备注:
有些绘图操作会从“当前点”开始,这个函数可以设置该点。

画线

void line(int x1, int y1, int x2, int y2);
void linerel(int dx, int dy);
void lineto(int x, int y);
参数说明:
x1, y1: 线的起始坐标。
x2, y2: 线的终止坐标。
cx, dy: 从“当前点”开始画线,画 cx, cy 的距离。
x, y: 从“当前点”开始画线,画至坐标 x, y。

画矩形

void rectangle(int left, int top, int right, int bottom);
参数说明:
left, top, right, bottom: 矩形的左、上、右、下。

获取圆弧坐标信息

void getarccoords(struct arccoordstype *arccoords);
参数说明:
arccoords: 指向 arccoordstype 结构的指针。
备注:
该函数在画圆弧连接的直线时很有用。

画圆弧

void arc(int x, int y, int stangle, int endangle, int radius);
参数说明:
x, y: 圆心。
stangle: 起始角的度数。
endangle: 终止角的度数。
radius: 半径。

画圆

void circle(int x, int y, int radius);
参数同 arc 函数。

画填充圆扇形

void pieslice(int x, int y, int stangle, int endangle, int radius);
参数通 arc 函数。

画椭圆弧线

void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);
参数说明:
x,y: 圆心。
stangle: 起始角的度数。
endangle: 终止角的度数。
xradius: x 方向半径。
yradius: y 方向半径。

画填充椭圆

void fillellipse(int x, int y, int xradius, int yradius);
参数同 ellipse 函数。

画填充椭圆扇形

void sector(int x, int y, int stangle, int endangle, int xradius, int yradius);
参数同 ellipse 函数。

画无边框填充矩形

void bar(int left, int top, int right, int bottom);
参数说明:
left,top,right,bottom: 矩形的左上角和右下角坐标。

画有边框三维填充矩形

void bar3d(int left, int top, int right, int bottom, int depth, int topflag);
参数说明:
left,top,right,bottom: 矩形的左上角和右下角坐标。
depth: 矩形深度。
topflag: 为 0 时,将不画矩形的三维顶部。可以用来画堆叠的三维矩形。

画多边形

void drawpoly(int numpoints, const int *polypoints);
参数说明:
numpoints: 多边形点的个数。
polypoints: 每个点的坐标,数组元素为 numpoints * 2。
备注:
该函数并不会自动连接多边形首尾。如果需要画封闭的多边形,请将最后一个点设置为与第一点相同。

画填充的多边形

void fillpoly(int numpoints, const int *polypoints);
参数同 drawpoly。

填充区域

void floodfill(int x, int y, int border)
参数说明:
x,y: 填充的起始点。
border: 填充的边界颜色。

在当前点输出文字

void outtext(LPCTSTR textstring);
参数说明:
textstring: 要输出的字符串的指针。

在指定位置输出文字

void outtextxy(int x, int y, LPCTSTR textstring);
参数说明:
x, y: 输出文字的坐标。
textstring: 要输出的字符串的指针。

求字符串的宽

int textwidth(LPCTSTR textstring);
参数说明:
textstring: 指定的字符串指针。
返回值:
该字符串实际占用的像素宽度。

求字符串的高

int textheight(LPCTSTR textstring);
参数说明:
textstring: 指定的字符串指针。
返回值:
该字符串实际占用的像素高度。

设置当前字体样式(新增函数)

void SetFont(int nHeight,int nWidth,int nEscapement,nOrientation,int fnWeight,DWORD fdwItalic,DWORD fdwUnderline,DWORD fdwStrikeOut,LPCTSTR lpszFace);
参数说明:
nHeight: 字符的平均高度。
nWidth: 字符的平均宽度。
nEscapement: 字符串的书写角度,单位 0.1 度。
nOrientation: 每个字符的书写角度,单位 0.1 度。
fnWeight: 字符的笔画粗细。常用的是 400,默认 0 即可。
fdwItalic: 是否斜体,TRUE / FALSE。
fdwUnderline: 是否下划线,TRUE / FALSE。
fdwStrikeOut: 是否删除线,TRUE / FALSE。
lpszFace: 字体名称。

设置当前字体样式(新增函数)

void SetFont(const LOGFONT *font);
参数说明:
font: 指向 LOGFONT 结构的指针,详见 Windows SDK。常用成员同 SetFont 的另一个重载。

获取当前字体样式(新增函数)

void GetFont(LOGFONT *font);

获取图像

void getimage(int left, int top, int right, int bottom, IMAGE *imgdst); // 从屏幕获取
void getimage(const char *imagefile, IMAGE *imgdst); // 从 BMP 文件获取
void getimage(const IMAGE *imgsrc, int left, int top, int right, int bottom, IMAGE *imgdst); // 从 IMAGE 对象中获取
参数说明:
left, top, right, bottom: 获取图像的区域。
imgdst: 保存获取的图像的 IMAGE 对象。
imgsrc: 从该 IMAGE 对象获取图像。
备注:
获取图像与绘制图像与 BGI 库略有区别。

绘制图像

void putimage(int left, int top, IMAGE *img, int op);
参数说明:
left, top: 绘制图像的位置。
img: 保存图像的 IMAGE 对象指针。
op: 绘制图像的位操作,详见 putimage_ops 位操作模式。
备注:
该函数的 op 参数还支持 Windows 定义的多种位操作模式,详见 Windows SDK 函数 BitBlt() 的 dwRop 参数。

获取最大颜色值

int getmaxcolor();

获取最大 x 坐标

int getmaxx();

获取最大 y 坐标

int getmaxy();

获取当前 x 坐标

int getx();

获取当前 y 坐标

int gety();

获取当前版本(新增函数)

int GetGraphicsVer(); 

模拟 Borland BGI 的常量、数据类型、全局变量

颜色枚举常量 COLORS

常量 颜色   常量 颜色
BLACK 0 DARKGRAY 0x545454 深灰
BLUE 0xA80000 LIGHTBLUE 0xFC5454 亮蓝
GREEN 0x00A800 绿 LIGHTGREEN 0x54FC54 亮绿
CYAN 0xA8A800 LIGHTCYAN 0xFCFC54 亮青
RED 0x0000A8 LIGHTRED 0x5454FC 亮红
MAGENTA 0xA800A8 LIGHTMAGENTA 0xFC54FC 亮紫
BROWN 0x0054A8 YELLOW 0x54FCFC
LIGHTGRAY 0xA8A8A8 浅灰 WHITE 0xFCFCFC

最大的颜色常量 MAXCOLORS

该常量始终为 0xffffff

视图设置信息

struct viewporttype
{
    int left, top, right, bottom;
    int clip;
}

线样式

enum line_styles
{
    SOLID_LINE = 0,
    DOTTED_LINE = 1,
    CENTER_LINE = 2,
    DASHED_LINE = 3,
    USERBIT_LINE = 4
};

线宽

enum line_widths
{
    NORM_WIDTH = 1,
    THICK_WIDTH = 3
};

线形

struct linesettingstype
{
    int linestyle;
    unsigned upattern;
    int thickness;
};

填充模式

enum fill_patterns
{
    EMPTY_FILL, /* fills area in background color */
    SOLID_FILL, /* fills area in solid fill color */
    LINE_FILL, /* --- fill */
    LTSLASH_FILL, /* /// fill */
    SLASH_FILL, /* /// fill with thick lines */
    BKSLASH_FILL, /* \\\ fill with thick lines */
    LTBKSLASH_FILL, /* \\\ fill */
    HATCH_FILL, /* light hatch fill */
    XHATCH_FILL, /* heavy cross hatch fill */
    INTERLEAVE_FILL, /* interleaving line fill */
    WIDE_DOT_FILL, /* Widely spaced dot fill */
    CLOSE_DOT_FILL, /* Closely spaced dot fill */
    USER_FILL /* user defined fill */
};

填充类型

struct fillsettingstype
{
    int pattern;
    int color;
};

struct pointtype
{
    int x, y;
};

圆弧坐标信息

struct arccoordstype
{
    int x, y;
    int xstart, ystart, xend, yend;
};

位操作模式

enum putimage_ops
{
    COPY_PUT, /* MOV */
    XOR_PUT, /* XOR */
    OR_PUT, /* OR */
    AND_PUT, /* AND */
    NOT_PUT /* NOT */
};

不支持的函数

驱动相关函数

Windows 硬件无关,因此不再需要 DOS 下繁琐的图形卡驱动了,并且颜色提升到了 24bit 真彩色,以下函数及数据无效:
detectgraph, getdrivername,getgraphmode,getmaxmode,getmodename,getmoderange,grapherrormsg,_graphfreemem,_graphgetmem,graphresult,installuserdriver,registerbgidriver,registerfarbgidriver,restorecrtmode,setgraphbufsize,setgraphmode,setactivepage,setvisualpage,CGA_COLORS,EGA_colors,graphics_drivers,graphics_errors,graphics_modes。

调色板相关函数

由于颜色数量由过去的16色提高到了24位色,所以没有了调色板的概念,因此涉及到调色板的函数都是无效的:
getdefaultpalette,getpalette,getpalettesize,setallpalette,setpalette,setrgbpalette,palettetype。

字体相关函数

Windows 下的字体取决于用户的系统,所以不再使用以下 BGI 库中的字体相关函数:
gettextsettings,installuserfont,settextjustify,settextstyle,setusercharsize,font_names,HORIZ_DIR,text_just,textsettingstype,USER_CHAR_SIZE,VERT_DIR。

略有改动的函数:

getimage / putimage / imagesize:被新的 getimage / putimage / IMAGE 取代,新的一组语句更简单、更安全,并且支持直接读取图片,详见:http://hi.baidu.com/yangw80/blog/item/464b42c8a795971e7f3e6ffa.html

 


ver: 20090909
by yw80@msn.com