Professional Documents
Culture Documents
Union Int Int Union: Return
Union Int Int Union: Return
h>
2. #include<dos.h>
3. #include<math.h>
4. #include<stdlib.h>
5. #include<stdio.h>
6. #include<conio.h>
7.
8. union REGS i, o;
9. int leftcolor[15];
10.
11. int get_key()
12. {
13. union REGS i,o;
14.
15. i.h.ah = 0;
16. int86(22, &i, &o);
17.
18. return ( o.h.ah );
19. }
20.
21. void draw_color_panel()
22. {
23. int left, top, c, color;
24.
25. left = 100;
26. top = 436;
27.
28. color = getcolor();
29. setcolor(GREEN);
30. rectangle(4,431,635,457);
31. setcolor(RED);
32. settextstyle(TRIPLEX_FONT,0,2);
33. outtextxy(10,431,"Colors : ");
34.
35. for( c = 1 ; c <= 15 ; c++ )
36. {
37. setfillstyle(SOLID_FILL, c);
38. bar(left, top, left+16, top+16);
39. leftcolor[c-1] = left;
40. left += 26;
41. }
42.
43. setcolor(color);
44. }
45.
46. void draw_shape_panel()
47. {
48. int left, top, c, color;
49.
50. left = 529;
51. top = 45;
52.
53. color = getcolor();
54. setcolor(GREEN);
55. rectangle(525,40,633,255);
56.
57. for( c = 1 ; c <= 7 ; c++ )
58. {
59. rectangle(left, top, left+100,top+25);
60. top += 30;
61. }
62. setcolor(RED);
63. outtextxy(530,45,"Bar");
64. outtextxy(530,75,"Line");
65. outtextxy(530,105,"Pixel");
66. outtextxy(530,135,"Ellipse");
67. outtextxy(530,165,"Freehand");
68. outtextxy(530,195,"Rectangle");
69. outtextxy(530,225,"Clear");
70. setcolor(color);
71. }
72.
73. void change_color(int x, int y)
74. {
75. int c;
76.
77. for( c = 0 ; c <= 13 ; c++ )
78. {
79. if( x > leftcolor[c] && x <leftcolor[c+1] && y > 437 && y < 453 )
80. setcolor(c+1);
81. if( x > leftcolor[14] && x < 505 && y > 437 && y < 453 )
82. setcolor(WHITE);
83. }
84. }
85.
86. char change_shape(int x, int y)
87. {
88. if ( x > 529 && x < 625 && y > 45 && y <70 )
89. return 'b';
90. else if ( x > 529 && x < 625 && y > 75&& y < 100 )
91. return 'l';
92. else if ( x > 529 && x < 625 && y > 105&& y < 130 )
93. return 'p';
94. else if ( x > 529 && x < 625 && y > 135&& y < 160 )
95. return 'e';
96. else if ( x > 529 && x < 625 && y > 165&& y < 190 )
97. return 'f';
98. else if ( x > 529 && x < 625 && y > 195&& y < 220 )
99. return 'r';
100. else if ( x > 529 && x < 625 && y > 225&& y < 250 )
101. return 'c';
102. }
103.
104. void showmouseptr()
105. {
106. i.x.ax = 1;
107. int86(0x33, &i, &o);
108. }
109.
110. void hidemouseptr()
111. {
112. i.x.ax = 2;
113. int86(0x33, &i, &o);
114. }
115.
116. void restrictmouseptr( int x1, int y1, intx2, int y2)
117. {
118. i.x.ax = 7;
119. i.x.cx = x1;
120. i.x.dx = x2;
121. int86(0x33, &i, &o);
122.
123. i.x.ax = 8;
124. i.x.cx = y1;
125. i.x.dx = y2;
126. int86(0x33, &i, &o);
127. }
128.
129. void getmousepos(int *button,int *x,int *y)
130. {
131. i.x.ax = 3;
132. int86(0x33, &i, &o);
133.
134. *button = o.x.bx;
135. *x = o.x.cx;
136. *y = o.x.dx;
137. }
138.
139. main()
140. {
141. int gd = DETECT,gm;
142.
143. intmaxx,maxy,x,y,button,prevx,prevy,temp1,temp2,key,color;
144. char ch = 'f' ; // default free-hand drawing
145.
146. initgraph(&gd,&gm,"C:\\TC\\BGI");
147.
148. maxx = getmaxx();
149. maxy = getmaxy();
150.
151. setcolor(BLUE);
152. rectangle(0,0,maxx,maxy);
153.
154. setcolor(WHITE);
155. settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
156. outtextxy(maxx/2-180,maxy-28,"<a
href="http://www.programmingsimplified.com"">www.programmingsimplified.com"</a>);
157.
158. draw_color_panel();
159. draw_shape_panel();
160.
161. setviewport(1,1,maxx-1,maxy-1,1);
162.
163. restrictmouseptr(1,1,maxx-1,maxy-1);
164. showmouseptr();
165. rectangle(2,2,518,427);
166. setviewport(1,1,519,428,1);
167.
168. while(1)
169. {
170. if(kbhit())
171. {
172. key = get_key();
173.
174. if( key == 1 )
175. {
176. closegraph();
177. exit(0);
178. }
179. }
180.
181. getmousepos(&button,&x,&y);
182.
183. if( button == 1 )
184. {
185. if( x > 4 && x < 635 && y > 431 &&y < 457 )
186. change_color( x, y );
187. else if ( x > 529 && x < 625 && y > 40 && y < 250 )
188. ch = change_shape( x, y );
189.
190. temp1 = x ;
191. temp2 = y ;
192.
193. if ( ch == 'f' )
194. {
195. hidemouseptr();
196. while( button == 1 )
197. {
198. line(temp1,temp2,x,y);
199. temp1 = x;
200. temp2 = y;
201. getmousepos(&button,&x,&y);
202. }
203. showmouseptr();
204. }
205.
206. while( button == 1)
207. getmousepos(&button,&x,&y);
208.
209. /* to avoid interference of mouse while drawing */
210. hidemouseptr();
211.
212. if( ch == 'p')
213. putpixel(x,y,getcolor());
214.
215. else if ( ch == 'b' )
216. {
217. setfillstyle(SOLID_FILL,getcolor());
218. bar(temp1,temp2,x,y);
219. }
220. else if ( ch == 'l')
221. line(temp1,temp2,x,y);
222. else if ( ch == 'e')
223. ellipse(temp1,temp2,0,360,abs(x-temp1),abs(y-temp2));
224. else if ( ch == 'r' )
225. rectangle(temp1,temp2,x,y);
226. else if ( ch == 'c' )
227. {
228. ch = 'f'; // setting to freehand drawing
229. clearviewport();
230. color = getcolor();
231. setcolor(WHITE);
232. rectangle(2,2,518,427);
233. setcolor(color);
234. }
235.
236. showmouseptr();
237. }
238. }
239. }