Carefully crafted: When using easyx as an interface, you want the background color to change when the mouse touches a button.

When we use easyx to develop small applications (such as writing confession program doge), it feels very advanced, but we still need to pay attention to some details to make our program more perfect and let customers (or your girlfriend) I feel that you are careful and pay attention to details, and I have to face some detailed problems.

What I want to talk about here is how to make the mouse interact with the interface, including the following contents (mainly to change the background. In order to give you confidence, I will also show you some previous basic contents): 1. How to use easyx Establish an operation interface; 2. How to create buttons; 3. If there is only one button, how to make the background change when the mouse moves over the button; 4. How to make changes if there are many buttons.

#1. How to create an operation interface of a specified size

#include <easyx.h>
#include <graphics.h> 

initgraph(1100,800);

First of all, the first step is to apply two header files, and then reference the initgraph function. The two parameters are the coordinates of the x and y axes respectively. The interface established is the positive x direction to the right and the positive y direction downward.

#2. Create button

IMAGE button;
loadimage(&button, L"按钮.jpg", 20, 50);
putimage(100, 20, &button, SRCINVERT);

First, you need to find the picture you like as a button on the Internet, and then you need to import the photo into the program. The method is as follows: Download the photo (remember to change it so that you can remember the name) -> Transfer the picture to the folder where the program source file is located - >Perform program operation. The program operates as follows: first quote the keyword IMAGE and define a space where the image is saved named button; then use the loadimage function to download the image into the program. There are 4 parameters, which are called a, b, c, d below. a is a The address is the memory location where the image is saved; b is the name of the image to be downloaded (there may be a warning in c++, you can try adding L in front of it); cd is the size of the image to be saved (represented by xy coordinates). Next, we need to quote the putimage function to place the image anywhere on the operation interface. There are also 4 parameters, let’s call them a1, b1, c1, d1, a1. b1 is the position coordinate of the upper left corner of the image; c1 is the address of the image; d1 is a If you are interested in this mode, you can search it.

#3. How a single button interacts with the mouse

MOUSEMSG m;
m = GetMouseMsg();
bool c1 = 1, now = 0;
if (m.uMsg == WM_MOUSEMOVE)
        {
            if (m.x > 800 && m.x < 1000 && m.y < 250 && m.y > 200 && !c1)
            {
                now = 1;c1 = 1;
                fillrectangle(800, 200, 1000, 250);
            }
            if ((m.x < 800 || m.x > 1000 || m.y > 250 || m.y <200) && now)
            {
                c1 = 0;now = 0;
                fillrectangle(800, 200, 1000, 250);
            }
        }

First reference MOUSEMSG and create a mouse object m; then assign the properties of m using the GetMouseMsg function to obtain the coordinates of the mouse on the interface; WM_MOUSEMOVE represents mouse movement, m.uMsg is the status of the mouse, mx is the x coordinate of the mouse, and my is the mouse's Y coordinate; this program indicates that the color of the rectangle will be filled when the mouse moves to the x coordinate 800-1000 and the y coordinate 200-250. When the mouse moves out of the button, fill the rectangle again so that it returns to its original color.

The variable now c1 can make its performance more stable and prevent it from flickering all the time.

The following introduces the fillrectangle function, which uses the current line shape and the current fill style to draw a filled rectangle with an outer frame.

void fillrectangle{
int left,
int top,
int right,
int bottom
};

#4. How multiple buttons can improve the program

You will find that when there are multiple buttons, the buttons in the above program will flash randomly, but the program needs to be improved, as follows.

MOUSEMSG m;
    int now = 0, now2 = 0, now3 = 0, now4 = 0;
    int c1 = 1, c2 = 1, c3 = 1, c4 = 1, k1 = 1, k2 = 1, k3 = 1, k4 = 1;
    while (1)
    {
        m = GetMouseMsg();
        setrop2(R2_XORPEN);
        if (m.uMsg == WM_MOUSEMOVE)
        {
            if (m.x > 800 && m.x < 1000 && m.y < 250 && m.y > 200 && (c1 == 0 || k1 == 1))
            {
                now = 1; k1 = 0; c1 = 1;
                fillrectangle(800, 200, 1000, 250);
            }
            if (now != 0 && (m.x < 800 || m.x > 1000 || m.y > 250 || m.y < 200))
            {
                now = 0; c1 = 0;
                fillrectangle(800, 200, 1000, 250);
            }
            if (m.x > 800 && m.x < 950 && m.y < 367 && m.y > 317 && (c2 == 0 || k2 == 1))
            {
                now2 = 1; k2 = 0; c2 = 1;
                fillrectangle(800, 317, 950, 367);
            }
            if (now2 != 0 && (m.x < 800 || m.x > 950 || m.y > 367 || m.y < 317))
            {
                now2 = 0; c2 = 0;
                fillrectangle(800, 317, 950, 367);
            }
            if (m.x > 800 && m.x < 1000 && m.y < 484 && m.y > 434 && (c3 == 0 || k3 == 1))
            {
                now3 = 1; k3 = 0; c3 = 1;
                fillrectangle(800, 434, 1000, 484);
            }
            if (now3 != 0 && (m.x < 800 || m.x > 1000 || m.y > 484 || m.y < 434))
            {
                now3 = 0; c3 = 0;
                fillrectangle(800, 434, 1000, 484);
            }
            if (m.x > 800 && m.x < 900 && m.y < 601 && m.y > 551 && (c4 == 0 || k4 == 1))
            {
                now4 = 1; k4 = 0; c4 = 1;
                fillrectangle(800, 551, 900, 601);
            }
            if (now4 != 0 && (m.x < 800 || m.x > 900 || m.y > 601 || m.y < 551))
            {
                now4 = 0; c4 = 0;
                fillrectangle(800, 551, 900, 601);
            }
        }

There are 4 buttons here, and k-series variables are added to make the performance more stable.

*Conclusion:

These are the tips I want to share. I hope it will be successful (doge). This method may be more troublesome, but it is simple and easy to understand. I hope you will let it go. Ha ha ha ha

Guess you like

Origin blog.csdn.net/m0_73747975/article/details/128687664