Sunday 3 February 2013


Painting in C:

#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#include <alloc.h>
#include <graphics.h>
union REGS i, o;
get_mouse_pos(int*,int*,int*);
hide_mouse();
show_mouse();

void restrictmouseptr(int x1,int y1,int x2,int y2);
paintBox(int left,int top, int right,int bottom, int color,int style,int bordercolor,int xa,int ya);
borderFigure(int left,int top, int right,int bottom, int color,int style,int bordercolor,int xa,int ya);
int main()
{
int gdriver = DETECT, gmode;
int x1,y1,x2,y2,button;
unsigned area;
initgraph(&gdriver, &gmode, "");
setbkcolor(7);
i.x.ax = 0;
int86 (0X33,&i,&o);

borderFigure(0,0,getmaxx(),50,14,1,15,0,0); //up
borderFigure(0,0,50,getmaxx(),14,1,15,0,0);
borderFigure(getmaxx()-50,0,getmaxx(),getmaxy(),14,1,15,0,0);
borderFigure(0,getmaxy()-50,getmaxx(),getmaxy(),14,1,15,0,0);

paintBox(0,0,300,60,14,8,4,1,1);

// paintBox(1,20,20,40,0,1,0,1,1);
paintBox(20,20,40,40,1,1,0,1,1);
paintBox(40,20,60,40,2,1,0,1,1);
paintBox(60,20,80,40,3,1,0,1,1);
paintBox(80,20,100,40,4,1,0,1,1);
paintBox(100,20,120,40,5,1,0,1,1);
paintBox(120,20,140,40,6,1,0,1,1);
paintBox(140,20,160,40,8,1,0,1,1);
paintBox(160,20,180,40,9,1,0,1,1);
paintBox(180,20,200,40,10,1,0,1,1);
paintBox(200,20,220,40,11,1,0,1,1);
paintBox(220,20,240,40,12,1,0,1,1);
paintBox(240,20,260,40,13,1,0,1,1);
paintBox(240,20,260,40,14,1,0,1,1);
paintBox(260,20,280,40,15,1,0,1,1);

//restrictmouseptr(100,100,550,400);

while(!kbhit())
{
outtextxy(200,450,"Right click to clear the screen");
show_mouse();
get_mouse_pos(&x1,&y1,&button);
x2=x1;
y2=y1;
while(button==1)
{
//hide_mouse();
//setcolor(random(15));
setcolor(x1);
line(x1,y1,x2,y2);

borderFigure(0,0,getmaxx(),50,14,1,15,0,0); //up
borderFigure(0,0,50,getmaxx(),14,1,15,0,0);
borderFigure(getmaxx()-50,0,getmaxx(),getmaxy(),14,1,15,0,0);
borderFigure(0,getmaxy()-50,getmaxx(),getmaxy(),14,1,15,0,0);
setcolor(4);

get_mouse_pos(&x2,&y2,&button);
}
if (o.x.bx == 2) //bx=mouse click button 0=not pressed 1=left 2=right 3=center

clearviewport();
// clrscr();
// cleardevice();
}

getch ();
closegraph ();
return 0;
}
show_mouse()
{
i.x.ax=1;
int86(0x33,&i,&o);
}

hide_mouse()
{
i.x.ax=2;
int86(0x33,&i,&o);
}

get_mouse_pos(int *x,int *y,int *button)
{
i.x.ax=3; ///ax=3 get mouse pointer
int86(0x33,&i,&o);

*x=o.x.cx; ///x-cordinates
*y=o.x.dx; ///y-cordinates
*button=o.x.bx;
}

paintBox(int left,int top, int right,int bottom, int color,int style,int bordercolor,int xa,int ya){
setfillstyle(style,color);
setcolor(bordercolor);
bar3d(left,top,right,bottom,xa,ya);
setcolor(5);
}
/* notes:
ax=7
cx=min x
dx=max x

ax=8
cx=min y
dx=max y
*/

void restrictmouseptr(int x1,int y1,int x2,int y2)
{
rectangle(100,100,550,400);
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(51,&i,&o);
i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(51,&i,&o);
}
borderFigure(int left,int top, int right,int bottom, int color,int style,int bordercolor,int xa,int ya){
setfillstyle(style,color);
setcolor(bordercolor);
bar3d(left,top,right,bottom,xa,ya);
setcolor(5);
}

No comments:

Post a Comment