【问题标题】:after clicking on one rectangle, make the other rectangle change color单击一个矩形后,使另一个矩形改变颜色
【发布时间】:2021-03-25 02:20:03
【问题描述】:

我显示6个圆圈(每个圆圈中的文字)和6颗星,当你点击某个圆圈时,某个星号应该会亮起,圆圈本身应该消失。 第一个问题 - 每个圆圈中应该有一个随机字母。我好像做了实现,乍一看是正确的,但是总是出现“M”而不是随机字母

image

case WM_CREATE: //the message will appear when creating a window
        char arr[strSize1];

        for (int i = 0; i < strSize1;i++)
        {
            char ch = rand() % 127;
            
                arr[i] = ch;
        }

第二个问题,其实我无法意识到,当你点击圆圈时,某个星号会亮起。星号只有在我点击时才会亮起。

#include <ctime>
#include <cmath>


struct Cell {
    int x, y;
    
    struct rgb {
        int r, g, b,z;
    }
    mainColor,
        borderColor,
        circleColor,
        textColor,
        circleSize,
        starSize,
        starColor,
        circleBorderColor;
    
};

HWND hWnd;
HDC hdc;
HPALETTE hPal;
BOOL bForceBkgd;
COLORREF color;
HBRUSH brush;




const int rectSize = 90;
const int circleSize = 40;
const int starSize = 40;

int y = 0, x = 0, yCoord, xCoord;
char buf[50];

bool flag = 0;
Cell cells[7][7];
HINSTANCE hInst; //Дескриптор програми
LPCTSTR szWindowClass = "QWERTY";
LPCTSTR szTitle = "  ";
// Попередній опис функцій
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);


bool isInCircle(int x, int y, int x0, int y0, int R) {

    if (pow((x - x0), 2) + pow((y - y0), 2) <= pow(R, 2)) {
        return true;
    }
    return false;

}

bool isInStar(int x, int y, int x0, int y0, int R) {

    if (pow((x - x0), 2) + pow((y - y0), 2) <= pow(R, 2)) {
        return true;
    }
    return false;

}


// main program
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    srand(time(NULL));

    MSG msg;
    // window class register
    MyRegisterClass(hInstance);
    // creating a program window
    if (!InitInstance(hInstance, nCmdShow))
    {
        return FALSE;
    }
    // messages processing cycle
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW; 
    wcex.lpfnWndProc = (WNDPROC)WndProc; 
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance; 
    wcex.hIcon = LoadIcon(NULL, IDI_SHIELD); 
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wcex.hbrBackground = GetSysColorBrush(COLOR_WINDOW + 3); 
    wcex.lpszMenuName = NULL; 
    wcex.lpszClassName = szWindowClass; 
    wcex.hIconSm = NULL;
    return RegisterClassEx(&wcex); 
}
// FUNCTION: InitInstance (HANDLE, int)

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    HWND hWnd;
    hInst = hInstance; 
    hWnd = CreateWindow(szWindowClass, // window class name
        szTitle, // program name
        WS_OVERLAPPED | WS_SYSMENU, 
        400, // position on x
        0, // position on y
        718, 
        790, 
        NULL, 
        NULL,
        hInstance, 
        NULL); 
    if (!hWnd) 
    {
        return FALSE;
    }

    ShowWindow(hWnd, nCmdShow); 
    UpdateWindow(hWnd); 
    return TRUE;
}
// FUNCTION: WndProc (HWND, unsigned, WORD, LONG)
void DrawRectangle(HWND hWnd)
{
    HBRUSH hBrush;
    HDC hdc;
    RECT rect;
    
    SetRect(&rect, cells[2][6].x * rectSize, cells[2][6].y * rectSize + circleSize, (cells[2][6].x + 1) * rectSize, (cells[2][6].y + 1) * rectSize + circleSize);
    hBrush = CreateSolidBrush(RGB(rand() % 256, rand() % 256, rand() % 256));
    hdc = GetDC(hWnd);
    FillRect(hdc, &rect, hBrush);
    ReleaseDC(hWnd, hdc);
    DeleteObject(hBrush);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    
    PAINTSTRUCT ps;
    
    RECT rt;
    HPEN pen;
    
    static unsigned short index = 0, index1 = 0, strSize = 257;
    static char* str = new char[strSize];
    static unsigned short const strSize1 = 6;


    switch (message)
    {
    case WM_CREATE: //the message will appear when creating a window
        char arr[strSize1];

        for (int i = 0; i < strSize1;i++)
        {
            char ch = rand() % 127;
            
                arr[i] = ch;
                
            
        }
        
        for (int i = 0; i < 7; i++)
        {
            for (int j = 0; j < 7; j++)
            {
                //serial numberoftheelement
                cells[i][j].x = j;
                cells[i][j].y = i;
                
                cells[i][j].mainColor.r = 0;
                cells[i][j].mainColor.g = 0;
                cells[i][j].mainColor.b = 0;

                //setting the grid color
                cells[i][j].borderColor.r = 40;
                cells[i][j].borderColor.g = 40;
                cells[i][j].borderColor.b = 40;
                
                //setting the colorofthe circle
                cells[i][j].circleColor.r = rand() % 256;
                cells[i][j].circleColor.g = rand() % 256;
                cells[i][j].circleColor.b = rand() % 256;
                //setting textcolor
                cells[i][j].textColor.r = rand() % 256;
                cells[i][j].textColor.g = rand() % 256;
                cells[i][j].textColor.b = rand() % 256;
                //setting circleSize
                cells[i][j].circleSize.z = 1 + rand() % (30 - 1);
                
                cells[i][j].starColor.r= 0 ;
                cells[i][j].starColor.g= 0 ;
                cells[i][j].starColor.b= 0 ;
                
            }
        }
        break;

    case WM_PAINT: //redraw the widow
        hdc = BeginPaint(hWnd, &ps); //start graphical output
        GetClientRect(hWnd, &rt);
        SetBkMode(hdc, TRANSPARENT);
        SetPolyFillMode(hdc, WINDING);
        
        
        
        
        //cycle of creating a grid with circles
        for (int i = 0; i < 7; i++)
        {
            for (int j = 0; j < 7; j++)
            {
                //Setting a custom pen and fill for the grid
                pen = CreatePen(PS_SOLID, 5, RGB(cells[i][j].borderColor.r, cells[i][j].borderColor.g, cells[i][j].borderColor.b));
                brush = CreateSolidBrush(RGB(cells[i][j].mainColor.r, cells[i][j].mainColor.g, cells[i][j].mainColor.b));
                SelectObject(hdc, brush);
                SelectObject(hdc, pen);

                //Meshing
                Rectangle(hdc, cells[i][j].x * rectSize, cells[i][j].y * rectSize + circleSize, (cells[i][j].x + 1) * rectSize, (cells[i][j].y + 1) * rectSize + circleSize);

                
                //Remove Penand Mesh Fill
                DeleteObject(pen);
                DeleteObject(brush);

                //Setting a custom pen and fill for circles
                
                if (i < 1 && j < 6) {
                    //int z = 1 + rand() % (30 - 1);
                    //int z = 10;
                    pen = CreatePen(PS_SOLID, 5, RGB(cells[i][j].circleBorderColor.r, cells[i][j].circleBorderColor.g, cells[i][j].circleBorderColor.b));
                    brush = CreateSolidBrush(RGB(cells[i][j].circleColor.r, cells[i][j].circleColor.g, cells[i][j].circleColor.b));
                    SelectObject(hdc, brush);
                    SelectObject(hdc, pen);
                    
                    //Making circles
                    Ellipse(hdc, cells[i][j].x * rectSize + cells[i][j].circleSize.z, cells[i][j].y * rectSize + cells[i][j].circleSize.z + circleSize, (cells[i][j].x + 1) * rectSize - cells[i][j].circleSize.z, (cells[i][j].y + 1) * rectSize - cells[i][j].circleSize.z + circleSize);

                    //Remove pen and circle fill
                    DeleteObject(pen);
                    DeleteObject(brush);
                    

                    
                    // text output
                    SetTextColor(hdc, RGB(cells[i][j].textColor.r, cells[i][j].textColor.g, cells[i][j].textColor.b));
                    TextOut(hdc, cells[i][j].x * rectSize + 40, cells[i][j].y * rectSize + circleSize + 39, &arr[i], 1);
                    
                }

                if (i>0 && j>5) {
                    //int z = 1 + rand() % (30 - 1);
                    
                    pen = CreatePen(PS_SOLID, 5, RGB(cells[i][j].starColor.r, cells[i][j].starColor.g, cells[i][j].starColor.b));
                    brush = CreateSolidBrush(RGB(cells[i][j].starColor.r, cells[i][j].starColor.g, cells[i][j].starColor.b));
                    SelectObject(hdc, brush);
                    

                    //Making circles
                    
                    POINT poly[6];

                    poly[0].x = cells[i][6].x * rectSize + 60;
                    poly[0].y = cells[i][6].y * rectSize + circleSize + 28;
                    poly[2].x = cells[i][6].x * rectSize + 20;
                    poly[2].y = cells[i][6].y * rectSize + circleSize + 40;
                    poly[2].x = cells[i][6].x * rectSize + 60;
                    poly[2].y = cells[i][6].y * rectSize + circleSize + 52;
                    poly[3].x = cells[i][6].x * rectSize + 35;
                    poly[3].y = cells[i][6].y * rectSize + circleSize + 20;
                    poly[4].x = cells[i][6].x * rectSize + 35;
                    poly[4].y = cells[i][6].y * rectSize + circleSize + 60;
                    poly[5].x = cells[i][6].x * rectSize + 60;
                    poly[5].y = cells[i][6].y * rectSize + circleSize + 28;
                    Polygon(hdc, poly, 6);
                    
                    
                    //Remove pen and circle fill
                    DeleteObject(pen);
                    DeleteObject(brush);


                    

                }
                //Displaying text on the screen
                

                
            }
        }

        
            
        
        EndPaint(hWnd, &ps);
        break;

    case WM_MOUSEMOVE:

        y = HIWORD(lParam);
        x = LOWORD(lParam);
        rt.top = 7;
        rt.left = 0;
        rt.right = 7 * rectSize;
        rt.bottom = circleSize;
        

        break;

    case WM_LBUTTONDOWN:

        yCoord = y;
        xCoord = x;

        yCoord -= circleSize;
        yCoord /= rectSize;
        xCoord /= rectSize;

        
        
        //Checking if coordinates are included in circle coordinates
        if (xCoord <7 && yCoord <7 && isInCircle(x, y - circleSize, xCoord * rectSize + rectSize / 2, yCoord * rectSize + rectSize / 2, (rectSize - 20) / 2)) {
        
            
            
                cells[yCoord][xCoord].starColor.r =50 ;
                cells[yCoord][xCoord].starColor.g=70 ;
                cells[yCoord][xCoord].starColor.b=200 ;
            
            cells[yCoord][xCoord].circleColor.r = 0;
            cells[yCoord][xCoord].circleColor.g = 0;
            cells[yCoord][xCoord].circleColor.b = 0;
            cells[yCoord][xCoord].circleSize.z = 1 + rand() % (30 - 1);

            cells[yCoord][xCoord].textColor.r = 0;
            cells[yCoord][xCoord].textColor.g = 0;
            cells[yCoord][xCoord].textColor.b = 0;
            //InvalidateRect(hWnd, NULL, TRUE);
            //brush = CreateSolidBrush(RGB(5,5,5));
            SetRectEmpty(&rt);
            InvalidateRect(hWnd, NULL, TRUE);
            
            
            //DeleteObject(brush);
            DrawRectangle(hWnd);
            GetPixel(hdc, x, y);
            
            flag = 1;
        }
        
        break;
    

    case WM_DESTROY:                //completion of work
        PostQuitMessage(0);
        break;
    default:
        //Processing messages that are not processed by the user
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
} ```

【问题讨论】:

    标签: c++ api winapi gdi


    【解决方案1】:

    随机坐标不出现的原因有两个:

    首先是你的arr数组在WM_PAINT消息触发时再次初始化,而不是你设置的随机值。所以你需要修改为静态:

    static char arr[strSize1];
    

    第二个原因是调用TextOut函数时,传递的参数是&amp;arr[i],语句中i总是0,所以总是显示同一个字母。你应该修改为&amp;arr[j]:

    TextOutA(hdc, cells[i][j].x * rectSize + 40, cells[i][j].y * rectSize + circleSize + 39, &arr[j], 1);
    

    WM_PAINT中,你错误地修改了两次poly[2]的值,却没有设置poly[1]的值,你应该修改为:

    poly[0].x = cells[i][6].x * rectSize + 60;
    poly[0].y = cells[i][6].y * rectSize + circleSize + 28;
    poly[1].x = cells[i][6].x * rectSize + 20;
    poly[1].y = cells[i][6].y * rectSize + circleSize + 40;
    poly[2].x = cells[i][6].x * rectSize + 60;
    poly[2].y = cells[i][6].y * rectSize + circleSize + 52;
    poly[3].x = cells[i][6].x * rectSize + 35;
    poly[3].y = cells[i][6].y * rectSize + circleSize + 20;
    poly[4].x = cells[i][6].x * rectSize + 35;
    poly[4].y = cells[i][6].y * rectSize + circleSize + 60;
    poly[5].x = cells[i][6].x * rectSize + 60;
    poly[5].y = cells[i][6].y * rectSize + circleSize + 28;
    

    当你按下左键触发WM_LBUTTONDOWN消息时,你正在处理的圆的下标与星星的下标相同,但它们的坐标映射关系不一样。而且应该判断是在圆圈的下标范围内,而不是xCoord &lt;7 &amp;&amp; yCoord &lt;7

    修改后的代码如下:

    if (xCoord < 6 && yCoord == 0 && isInCircle(x, y - circleSize, xCoord * rectSize + rectSize / 2, yCoord * rectSize + rectSize / 2, (rectSize - 20) / 2)) {
        cells[xCoord + 1][yCoord + 6].starColor.r = 50;
        cells[xCoord + 1][yCoord + 6].starColor.g = 70;
        cells[xCoord + 1][yCoord + 6].starColor.b = 200;
        cells[yCoord][xCoord].circleColor.r = 0;
        cells[yCoord][xCoord].circleColor.g = 0;
        cells[yCoord][xCoord].circleColor.b = 0;
        cells[yCoord][xCoord].circleSize.z = 1 + rand() % (30 - 1);
        cells[yCoord][xCoord].textColor.r = 0;
        cells[yCoord][xCoord].textColor.g = 0;
        cells[yCoord][xCoord].textColor.b = 0;
        //InvalidateRect(hWnd, NULL, TRUE);
        //brush = CreateSolidBrush(RGB(5,5,5));
        SetRectEmpty(&rt);
        InvalidateRect(hWnd, NULL, TRUE);
        //DeleteObject(brush);
        DrawRectangle(hWnd);
        GetPixel(hdc, x, y);
        flag = 1;
    }
    

    最后它成功地为我工作:

    【讨论】:

    • 感谢您的回答,我非常感激
    • @Маlnyy 嗨,如果这个答案对你有帮助,请随时标记它以帮助有同样问题的人,如果你有任何问题,请告诉我。谢谢。
    猜你喜欢
    • 2020-07-31
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 2014-12-20
    相关资源
    最近更新 更多