SetBkColor函数功能:该函数用指定的颜色值来设置当前的背景色,如果指定的颜色值超出了当前设备的表示范围,则设置为最近似的、设备可以表示的颜色。
函数原型:
COLORREF SetBkColor( HDChdc, COLORREFcrColor);
参数说明:
hdc: 设置上下文句柄
crColor: 标识新的背景颜色值。如果想要获得COLORREF的值,请使用RGB宏。
返回值:
如果函数成功,返回值是原背景色的COLORREF值。如果函数失败,则返回CLR_INVALID。想要获得更多信息,请调用GetLastError函数。
速查:
Windows NT/2000: 需要 Windows NT 3.1 或更高版本.
Windows 95/98: 需要 Windows 95 或更高版本.
Header: 在头文件Wingdi.h中声明;请包含 Windows.h.
Library: Use Gdi32.lib.
程序例:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* select a driver and mode that supports */
/* multiple background colors. */
int gdriver = EGA, gmode = EGAHI, errorcode;
int bkcol, maxcolor, x, y;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s
", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
/* maximum color index supported */
maxcolor = getmaxcolor();
/* for centering text messages */
settextjustify(CENTER_TEXT, CENTER_TEXT);
x = getmaxx() / 2;
y = getmaxy() / 2;
/* loop through the available colors */
for (bkcol=0; bkcol<=maxcolor; bkcol++)
{
/* clear the screen */
cleardevice();
/* select a new background color */
setbkcolor(bkcol);
/* output a messsage */
if (bkcol == WHITE)
setcolor(EGA_BLUE);
sprintf(msg, "Background color: %d", bkcol);
outtextxy(x, y, msg);
getch();
}
/* clean up */
closegraph();
return 0;
}