C language mouse Creation
I have been studying the C language for four or five years. I used to feel helpless for a very small problem. When I was a beginner, even a simple for statement would have to be depressed for a long time, I always want to know when. But now the taste has changed, and the more you think, the more excited you get. From windows programming with win-TC to programming with Emacs and VI in Linux, I feel that my feelings for C language are getting deeper and deeper, just as I love this child, she worked hard, failed, and excited ......
In, I saw a lot of friends around me making mouse work in win-TC. However, when I was in high school, my hardware equipment could not be fully utilized, what I understand is that I read books, read statements, and write this code ...... It was a very distant task for me to create a mouse in win-TC, so I did not dare to think about it, I didn't have any contact with books at the time (I saw tan haoqiang ). But it is also because of the high school, the mouse program of my friends is not successful and it is over (there is no place to consult ).
One year later, I went to college and chose my favorite software engineering major. I also had enough hardware facilities to use, hold books, chew on statements, write this code, debug this code ...... Gradually, I got away from the teacher's step. I started my kitchen directly. Because of freedom and time, I learned a lot of things and technologies that I didn't think. Now you have been in college for 1 time. I have used this resource to bring my hobbies for C language to another level, I found a better platform-for C language enthusiasts-Linux operating system. Although I have not been familiar with Linux for a long time, I understand that it is only a matter of time. I will cheer up.
Recently, I have been working with several buddies in the dormitory to develop a pure win-TC calculator that supports simultaneous interaction with the mouse and keyboard. This is already a complete project, although it is not very difficult for us to do something, it is already a kind of transcendence for ourselves. below is the source code of a mouse program we developed:
(Here we will explain:
The principle of making a mouse in win-TC is to use the 0x33 Soft Interrupt of DOS (or the mouse interrupt ). After setting the mouse's horizontal and column's size --> place the mouse --> draw the mouse ----> Save the pixels covered by the mouse ----> move the coordinates of the mouse ----> override by the mouse. Keep repeating like this.
The two functions putpixel () and getpixel () for drawing are used to save the pixel points covered by the mouse and re-draw the pixels covered by the mouse. For their details, go to the Internet and check them.
# Include <dos. h>
# Include <conio. h>
# Include <graphics. h>
# Include <bios. h>
# Define mousestatusmouseon 1
/* # Include <E:/win-TC/projects/desk. c> */
/* # Include <E:/win-TC/projects/shortname. c> * // * contains the file header */
Struct mouse
{
Int top;
Int botton;
Int left;
Int rigoal;
} Smouse;
Extern void dosk ();
Unsigned int size;
Void * ball;
Int mousex = 0;/* store the horizontal direction of the mouse */
Int Mousey = 0;
Char mousetype = 1;
Enum mouse_messages_code
{Nothing = 0,
Lbuttonclk = 1,
};
Int arrowmousepixsave [16] [10];
Int arrowmousedraw [16] [10] = {/* design the shape of the mouse, where 1 indicates the border of the mouse, 2 indicates the area surrounded by the border of the mouse, 3: area other than the mouse */
{, 3, 3, 3, 3, 3}, {, 3, 3, 3}, {, 3, 3, 3, },
{, 3, 2, 2, 2, 2, 2, 1, 3 },
, 1, 3, 1, 2, 1, 3 },
{,}, {, 3, 3, 3,}, {, 3, 3, 3, 3, 3, 3 }};
Int editmousepixsave [15] [5];
Int editmousedraw [15] [5] = {
{, 3, 1, 1}, {3, 3, 3}, {3, 3, 3}, {3, 3, 3}, {3, 3}, {3, 3, 3 }, {3, 3, 3}, {3, 3, 3, 3}, {3, 3, 3 },
{3, 3, 3}, {3, 3, 3, 3}, {3, 3, 3}, {3, 3, 3}, {3, 3}, {3, 3, 3 }, {, 3}, {, 1}
};
Void getmousexy ();
Void mouseon (INT mousex, int Mousey)
{
Int I, J;
Int color;
If (mousetype = 1)
{
For (I = 0; I <16; I ++)
For (j = 0; j <10; j ++)
{
Arrowmousepixsave [I] [J] = getpixel (mousex + J, Mousey + I);/* Save the original color */
If (arrowmousedraw [I] [J] = 1)
Putpixel (mousex + J, Mousey + I, 0 );
Else if (arrowmousedraw [I] [J] = 2)/* draw the mouse */
Putpixel (mousex + J, Mousey + I, 15 );
}
}
Else if (mousetype = 2)
{
For (I = 0; I <15; I ++)
For (j = 0; j <5; j ++)
{
Editmousepixsave [I] [J] = getpixel (mousex-2 + J, mousey-7 + I );
If (editmousedraw [I] [J] = 1)/* because the hotspot for editing the mouse is in the center of the mouse */
Putpixel (mousex-2 + J, mousey-7 + I, 0 );
}
}
}
Void setmousehori (INT Minx, int Maxx)
{
_ AX = 0x07;
_ Cx = Minx;
_ Dx = Maxx;
Geninterrupt (0x33 );
}
Void setmousevert (INT miny, int Maxy)
{
_ AX = 0x08;
_ Cx = miny;
_ Dx = Maxy;
Geninterrupt (0x33 );
}
/* Hide the mouse */
Void mouseoff (INT mousex, int Mousey)
{
Int I, j, X, Y, color;
X = mousex;
Y = Mousey;
If (mousetype = 1)
{
For (I = 0; I <16; I ++)
For (j = 0; j <10; j ++)
{
If (arrowmousedraw [I] [J] = 3)
Continue;
Color = getpixel (x + J, Y + I );
Putpixel (x + J, Y + I, color ^ color);/* The original location is different or clear */
Putpixel (x + J, Y + I, arrowmousepixsave [I] [J]);/* restore the original background */
}
}
Else if (mousetype = 2)
{
For (I = 0; I <15; I ++)
For (j = 0; j <5; j ++)
{
If (editmousedraw [I] [J] = 3)
Continue;
Color = getpixel (X-2 + J, y-7 + I );
Putpixel (X-2 + J, y-7 + I, color ^ color);/* original location exclusive or clear */
Putpixel (X-2 + J, y-7 + I, editmousepixsave [I] [J]);/* restore the original background */
}
}
}
Void setmousexy (int x, int y)
{
_ AX = 0x04;
_ Cx = X;
_ Dx = y;
Geninterrupt (0x33 );
}
Void getmousexy ()
{
_ AX = 0x03;
Geninterrupt (0x33 );
Mousex = _ CX;/* store the cursor horizontally to mousex */
Mousey = _ DX;/* store the cursor vertically to Mousey */
}
Void initgr (void)/* BGI initialization */
{
Int GD = detect, GM = 0;/* and Gd = VGA, GM = vgahi are the same effect */
Registerbgidriver (egavga_driver);/* you do not need to run the. BGI file after registering the BGI driver */
Initgraph (& GD, & GM ,"");
Setmousexy (0, 0 );
Setmousehori (0,600 );
Setmousevert (0,450 );
Setfillstyle (solid_fill, 7 );
Bar (100,280,140,300 );
Size = imagesize (100,280,141,301 );
Ball = malloc (size );
Getimage (100,280,141,301, ball );
}
Void main ()
{Int q = 0;
Float p = 0;
Int temp1 = 0;
Int X, Y, mousemsg, M;
Int tempx, Tempy, mousemsg;
Char success;
Tempx = Tempy = 100;
Initgr ();
Getmousexy ();
X = mousex;
Y = Mousey;
Putimage (100,280, ball, xor_put );
While (! Kbhit ())
{
Getmousexy ();/* Get current mouse position */
/* Update the mouse position */
If (tempx! = Mousex | Tempy! = Mousey)
{
Mouseoff (tempx, Tempy );
Mouseon (mousex, Mousey);/* display the mouse, but save the area to be covered by the mouse */
Tempx = mousex;/* cache current mouse POS */
Tempy = Mousey;
}
_ AX = 0x03;/* read the mouse button status */
Geninterrupt (0x33);/* generate a Soft Interrupt of the 33 mouse */
If (_ BX & 1 )&&! (_ BX & 2)/* the left mouse button is pressed and the right mouse button is not pressed */
{
Mousemsg = 4;
M = mousemsg;
}
If (_ BX & 2 )&&! (_ BX & 1)/* The right-click button is pressed while the left-click button is not pressed */
{
Mousemsg = 5;
M = mousemsg;
}
If (_ BX & 1 & _ BX & 2)/* The left and right mouse buttons are simultaneously pressed */
{
Mousemsg = 3;
M = mousemsg;
}
_ AX = 0x06;/* read the mouse button release information */
_ BX = 0x00;/* specify to read the release information of the left mouse button */
Geninterrupt (0x33);/* generate a Soft Interrupt of the 33 mouse */
If (_ BX = 1)/* if the number of left mouse buttons to be released is 1 */
Mousemsg = 2;/* generate a left-click message */
If (_ BX = 0 & M = 4)
If (tempx> = 100 & tempx <= 140 & Tempy> = 280 & Tempy <= 300)
{
Setcolor (red );
/* Outtextxy (100,100, "111111111111111111111 ");*/
Getmousexy ();
If (tempx! = Mousex | Tempy! = Mousey)
{
/* Outtextxy (200,200, "2222222222222 ");*/
Putimage (300,270, ball, xor_put );
Delay (100 );
}
}
/* If (_ BX = 1 & M = 4 )*/
_ AX = 0x06;/* read the mouse button release information */
_ BX = 0x01;/* specifies to read the release information from the right-click */
Geninterrupt (0x33);/* generate a Soft Interrupt of the 33 mouse */
If (_ BX = 1)/* If the left mouse button is released for 1 */
Mousemsg = 1;/* generate a right-click message */
}
}
I don't know if you can understand it, but I think that when you learn the interrupt programming in C language, you should understand the Soft Interrupt of DOS 0x33, because the mouse interruption of DOS 0x33 is also the predecessor of the mouse interruption in windows.
Because the use of interrupt technology can improve the quality of your code, it is also a very important technology in the operating system.
I believe this annotation is very detailed. Here I hope that students and netizens who are interested in the C language can cheer up and contribute the recent strength to the Linux Business.
Good luck!