📄 王大刚--c语言编程宝典--s.htm
字号:
<P> /* output some messages */ <BR> sprintf(msg,
"Graphics buffer size: %d", BUFSIZE); <BR>
settextjustify(CENTER_TEXT, CENTER_TEXT); <BR> outtextxy(x, y,
msg); <BR> sprintf(msg, "Old graphics buffer size: %d",
oldsize); <BR> outtextxy(x, y+textheight("W"), msg); <BR>
<P> /* clean up */ <BR> getch(); <BR>
closegraph(); <BR> return 0; <BR>} <BR> <BR>
<BR> <BR>
<P>函数名: setgraphmode <BR>功 能: 将系统设置成图形模式且清屏 <BR>用 法: void far
setgraphmode(int mode); <BR>程序例: <BR>
<P>#include <graphics.h> <BR>#include <stdlib.h> <BR>#include
<stdio.h> <BR>#include <conio.h> <BR>
<P>int main(void) <BR>{ <BR> /* request auto detection */
<BR> int gdriver = DETECT, gmode, errorcode; <BR>
int x, y; <BR>
<P> /* initialize graphics and local variables */
<BR> initgraph(&gdriver, &gmode, ""); <BR>
<P> /* read result of initialization */ <BR>
errorcode = graphresult(); <BR> if (errorcode != grOk)
/* an error occurred */ <BR> {
<BR> printf("Graphics error: %s\n",
grapherrormsg(errorcode)); <BR>
printf("Press any key to halt:"); <BR>
getch(); <BR> exit(1); /* terminate with an
error code */ <BR> } <BR>
<P> x = getmaxx() / 2; <BR> y = getmaxy() / 2;
<BR>
<P> /* output a message */ <BR>
settextjustify(CENTER_TEXT, CENTER_TEXT); <BR> outtextxy(x, y,
"Press any key to exit graphics:"); <BR> getch(); <BR>
<P> /* restore system to text mode */ <BR>
restorecrtmode(); <BR> printf("We're now in text mode.\n");
<BR> printf("Press any key to return to graphics mode:");
<BR> getch(); <BR>
<P> /* return to graphics mode */ <BR>
setgraphmode(getgraphmode()); <BR>
<P> /* output a message */ <BR>
settextjustify(CENTER_TEXT, CENTER_TEXT); <BR> outtextxy(x, y,
"We're back in graphics mode."); <BR> outtextxy(x,
y+textheight("W"), "Press any key to halt:"); <BR>
<P> /* clean up */ <BR> getch(); <BR>
closegraph(); <BR> return 0; <BR>} <BR> <BR>
<BR> <BR>
<P>函数名: setjmp <BR>功 能: 非局部转移 <BR>用 法: int setjmp(jmp_buf
env); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <process.h> <BR>#include
<setjmp.h> <BR>
<P>void subroutine(void); <BR>
<P>jmp_buf jumper; <BR>
<P>int main(void) <BR>{ <BR> int value; <BR>
<P> value = setjmp(jumper); <BR> if (value != 0)
<BR> { <BR> printf("Longjmp with
value %d\n", value); <BR> exit(value);
<BR> } <BR> printf("About to call subroutine ...
\n"); <BR> subroutine(); <BR> return 0; <BR>} <BR>
<P>void subroutine(void) <BR>{ <BR> longjmp(jumper,1); <BR>}
<BR> <BR> <BR>
<P>函数名: setlinestyle <BR>功 能: 设置当前画线宽度和类型 <BR>用 法: void far
setlinestyle(int linestype, unsigned upattern); <BR>程序例: <BR>
<P>#include <graphics.h> <BR>#include <stdlib.h> <BR>#include
<string.h> <BR>#include <stdio.h> <BR>#include <conio.h>
<BR>
<P>/* the names of the line styles supported */ <BR>char *lname[] = {
<BR> "SOLID_LINE", <BR> "DOTTED_LINE",
<BR> "CENTER_LINE", <BR> "DASHED_LINE",
<BR> "USERBIT_LINE" <BR> }; <BR>
<P>int main(void) <BR>{ <BR> /* request auto detection */
<BR> int gdriver = DETECT, gmode, errorcode; <BR>
<P> int style, midx, midy, userpat; <BR> char
stylestr[40]; <BR>
<P> /* initialize graphics and local variables */
<BR> initgraph(&gdriver, &gmode, ""); <BR>
<P> /* read result of initialization */ <BR>
errorcode = graphresult(); <BR> if (errorcode != grOk)
/* an error occurred */ <BR> {
<BR> printf("Graphics error: %s\n",
grapherrormsg(errorcode)); <BR>
printf("Press any key to halt:"); <BR>
getch(); <BR> exit(1); /* terminate with an
error code */ <BR> } <BR>
<P> midx = getmaxx() / 2; <BR> midy = getmaxy() /
2; <BR>
<P> /* a user defined line pattern */ <BR> /*
binary: "0000000000000001" */ <BR> userpat = 1; <BR>
<P> for (style=SOLID_LINE; style<=USERBIT_LINE; style++)
<BR> { <BR> /* select the line
style */ <BR> setlinestyle(style, userpat,
1); <BR>
<P> /* convert style into a string */
<BR> strcpy(stylestr, lname[style]); <BR>
<P> /* draw a line */
<BR> line(0, 0, midx-10, midy); <BR>
<P> /* draw a rectangle */
<BR> rectangle(0, 0, getmaxx(), getmaxy());
<BR>
<P> /* output a message */
<BR> outtextxy(midx, midy, stylestr); <BR>
<P> /* wait for a key */
<BR> getch();
<BR> cleardevice(); <BR> } <BR>
<P> /* clean up */ <BR> closegraph();
<BR> return 0; <BR>} <BR> <BR> <BR> <BR>
<P>函数名: setmem <BR>功 能: 存值到存储区 <BR>用 法: void setmem(void
*addr, int len, char value); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <alloc.h> <BR>#include
<mem.h> <BR>
<P>int main(void) <BR>{ <BR> char *dest; <BR>
<P> dest = calloc(21, sizeof(char)); <BR>
setmem(dest, 20, 'c'); <BR> printf("%s\n", dest); <BR>
<P> return 0; <BR>} <BR> <BR> <BR> <BR>
<P>函数名: setmode <BR>功 能: 设置打开文件方式 <BR>用 法: int setmode(int
handle, unsigned mode); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <fcntl.h> <BR>#include
<io.h> <BR>
<P>int main(void) <BR>{ <BR> int result; <BR>
<P> result = setmode(fileno(stdprn), O_TEXT); <BR>
if (result == -1) <BR> perror("Mode not
available\n"); <BR> else <BR>
printf("Mode successfully switched\n"); <BR> return 0; <BR>}
<BR> <BR> <BR> <BR>
<P>函数名: setpalette <BR>功 能: 改变调色板的颜色 <BR>用 法: void far
setpalette(int index, int actural_color); <BR>程序例: <BR>
<P>#include <graphics.h> <BR>#include <stdlib.h> <BR>#include
<stdio.h> <BR>#include <conio.h> <BR>
<P>int main(void) <BR>{ <BR> /* request auto detection */
<BR> int gdriver = DETECT, gmode, errorcode; <BR>
int color, maxcolor, ht; <BR> int y = 10; <BR>
char msg[80]; <BR>
<P> /* initialize graphics and local variables */
<BR> initgraph(&gdriver, &gmode, ""); <BR>
<P> /* read result of initialization */ <BR>
errorcode = graphresult(); <BR> if (errorcode != grOk)
/* an error occurred */ <BR> {
<BR> printf("Graphics error: %s\n",
grapherrormsg(errorcode)); <BR>
printf("Press any key to halt:"); <BR>
getch(); <BR> exit(1); /* terminate with an
error code */ <BR> } <BR>
<P> maxcolor = getmaxcolor(); <BR> ht = 2 *
textheight("W"); <BR>
<P> /* display the default colors */ <BR> for
(color=1; color<=maxcolor; color++) <BR> {
<BR> setcolor(color);
<BR> sprintf(msg, "Color: %d", color);
<BR> outtextxy(1, y, msg);
<BR> y += ht; <BR> } <BR>
<P> /* wait for a key */ <BR> getch(); <BR>
<P> /* black out the colors one by one */ <BR> for
(color=1; color<=maxcolor; color++) <BR> {
<BR> setpalette(color, BLACK);
<BR> getch(); <BR> } <BR>
<P> /* clean up */ <BR> closegraph();
<BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: setrgbpalette <BR>功 能: 定义IBM8514图形卡的颜色 <BR>用 法: void
far setrgbpalette(int colornum, int red, int green, int blue); <BR>程序例:
<BR>
<P>#include <graphics.h> <BR>#include <stdlib.h> <BR>#include
<stdio.h> <BR>#include <conio.h> <BR>
<P>int main(void) <BR>{ <BR> /* select a driver and mode that
supports the use */ <BR> /* of the setrgbpalette
function.
*/ <BR> int gdriver = VGA, gmode = VGAHI, errorcode;
<BR> struct palettetype pal; <BR> int i, ht, y,
xmax; <BR>
<P> /* initialize graphics and local variables */
<BR> initgraph(&gdriver, &gmode, ""); <BR>
<P> /* read result of initialization */ <BR>
errorcode = graphresult(); <BR> if (errorcode != grOk)
/* an error occurred */ <BR> {
<BR> printf("Graphics error: %s\n",
grapherrormsg(errorcode)); <BR>
printf("Press any key to halt:"); <BR>
getch(); <BR> exit(1); /* terminate with an
error code */ <BR> } <BR>
<P> /* grab a copy of the palette */ <BR>
getpalette(&pal); <BR>
<P> /* create gray scale */ <BR> for (i=0;
i<pal.size; i++) <BR>
setrgbpalette(pal.colors[i], i*4, i*4, i*4); <BR>
<P> /* display the gray scale */ <BR> ht =
getmaxy() / 16; <BR> xmax = getmaxx(); <BR> y = 0;
<BR> for (i=0; i<pal.size; i++) <BR> {
<BR> setfillstyle(SOLID_FILL, i);
<BR> bar(0, y, xmax, y+ht);
<BR> y += ht; <BR> } <BR>
<P> /* clean up */ <BR> getch(); <BR>
closegraph(); <BR> return 0; <BR>} <BR> <BR>
<BR> <BR>
<P>函数名: settextjustify <BR>功 能: 为图形函数设置文本的对齐方式 <BR>用 法: void
far settextjustify(int horiz, int vert); <BR>程序例: <BR>
<P>#include <graphics.h> <BR>#include <stdlib.h> <BR>#include
<stdio.h> <BR>#include <conio.h> <BR>
<P>/* function prototype */ <BR>void xat(int x, int y); <BR>
<P>/* horizontal text justification settings */ <BR>char *hjust[] = {
"LEFT_TEXT",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -