C language to implement drawing program

Source: Internet
Author: User
Tags include

In the process of developing county power grid dispatching automation system, the author encounters a problem to draw the primary wiring diagram of power system. As the power system wiring diagram is more complex, there are some special symbols, such as transformer symbols, switch symbols, and so on, with general programming methods to draw more difficult, therefore, the author uses C language to develop a hand-drawn power system wiring diagram of the keyboard drawing program, Using this program to draw a wiring diagram to meet the needs of users, the effect is good.

2. How to use

Several special keys are defined in the program:

"V": Brush lift

"W": Start drawing

"R": Start wiping the picture

"S": Current graphics saved to file

"E": Pull Up existing files

"C": Draw a circle

When the program is running, a yellow border appears to set the area of the paint, and the brush symbol is lifted in the middle of the area. When the "W" key is pressed, the brush symbol changes, at which point the arrow keys (top, bottom, left, right, top left, bottom left, right, bottom right) are drawn, and the brush symbol changes when the "R" key is pressed, At this point can move the arrow keys to wipe the map; In the drawing process, press the "C" key, you can draw a radius of 20 pixel points of the circle, when the end of paint, press the "s" key, the drawing of a good picture to save, press the "E" key to pull out the existing graphics for editing.

3. List of source programs

# include ' Graphics.h '
# include ' Stdio.h '
# include ' Fcntl.h '
# include ' Stdlib.h '
Main ()
void Save (), load ();
void *wg,*rg,*vg,*fy;
int Driver,mode;
int c=red;
int x=320,y=225;
int x1,y1,x2,y2;
int k,k1,k2;
/* Initialize Grapher * *
Detectgraph (&driver,&mode);
Initgraph (&driver,&mode, "C:\TC");
/* Write the pen * *
Bar (200,10,206,16);
Line (203,7,200,10);
Line (203,7,206,10);
Line (243,7,240,16);
Line (243,7,246,16);
Line (283,7,280,10);
Line (283,7,286,10);
Line (283,7,283,16);
* Save the pen * *
Wg=malloc (ImageSize (200,7,206,16));
Rg=malloc (ImageSize (240,7,246,16));
Vg=malloc (ImageSize (280,7,286,16));
Fy=malloc (ImageSize (200,7,206,16));
GetImage (200,7,206,16,WG);
GetImage (240,7,246,16,RG);
GetImage (280,7,286,16,VG);
Cleardevice ();
/* Write the box * *
SetColor (yellow);
Rectangle (4,19,637,447);
x1=x-3;
y1=y+1;
x2=x+3;
y2=y+10;
GetImage (X1,Y1,X2,Y2,FY);
Putimage (X1,y1,vg,xor_put);
/* Receive the command * *
for (;;)
while (Bioskey (1) ==0);
K=bioskey (0);
Putimage (X1,y1,fy,and_put);
if (((K&0X00FF) |0x00) ==0)
k1=k&0xff?0:k>>8; /* K1 is the Specialkey value */
Else
k2=k&0x00ff; /* K2 is the Non-specialkey value */
if (((K&0X00FF) |0x00) ==0)/* Special Key * *
Switch (K1)
Case 45:
Restorecrtmode ();
Exit (0);
Case 72:
if (y>20)
Y=y-1;
Break
Case 75:
if (x>5)
X=x-1;
Break
Case 77:
if (x<636)
x=x+1;
Break
Case 80:
if (y<446)
y=y+1;
Break
Case 71:
if ((x>5) && (y>20)
X=x-1;
Y=y-1;
Break
Case 79:
if ((x>5) && (y<446)
X=x-1;
y=y+1;
Break
Case 73:
if ((x<636) && (y>20)
x=x+1;
Y=y-1;
Break
Case 81:
if ((x<636) && (y<446)
x=x+1;
y=y+1;
Break
x1=x-3;
y1=y+1;
x2=x+3;
y2=y+10;
GetImage (X1,Y1,X2,Y2,FY);
/* Non-special Key * *
Switch (K2)
Case 118:/* ' V ' * *
Case:/* ' V ' * *
Putimage (X1,y1,vg,or_put);
Break
Case 119:/* ' W ' * *
Case:/* ' W ' * *
Putimage (X1,y1,wg,or_put);
Putpixel (X,Y,C);
Break
Case 114:/* ' r ' *
Case:/* ' R ' *
Putimage (X1,y1,rg,or_put);
Putpixel (X,y,black);
Break
Case:/* ' s ' * *
Case:/* ' S ' * *
Save ("Pic.dat");
Break
Case:/* ' e ' *
Case:/* ' E ' *
Load ("Pic.dat");
Break
Case:/* ' C ' * *
Case:/* ' C ' * *
SetColor (RED);
Circle (x,y,20);
Break
Default:continue;
/* Function for screens picture save
*/
void Save (char *fname)
FILE *FP;
int i;
Register Long J;
char far *ptr;
Fp=fopen (fname, "WB");
for (i=0;i<4;i++)
OUTPORTB (0x3ce,4);
OUTPORTB (0x3cf,i);
Ptr= (char FAR *) 0xa0000000l;
for (j=0;j<38400l;j++)
PUTC (*PTR,FP);
ptr++;
Fclose (FP);
OUTPORTB (0x3cf,0);
/* Function for screens picture display
*/
void load (char *fname)
FILE *FP;
register int i;
int k4=1;
Register Long J;
char far *ptr;
Fp=fopen (fname, "RB");
for (i=0;i<4;i++)
OUTPORTB (0x3c4,2);
OUTPORTB (0X3C5,K4);
Ptr= (char FAR *) 0xa0000000l;
for (j=0;j<38400l;j++)
*PTR=GETC (FP);
ptr++;
k4*=2;
Fclose (FP);
OUTPORTB (0X3C5,0XF);

4. Concluding remarks

The program runs through the Turbo C 2.0 environment and has a good working effect. The program can be expanded to increase the drawing function according to the specific needs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.