【发布时间】:2021-03-25 02:20:03
【问题描述】:
我显示6个圆圈(每个圆圈中的文字)和6颗星,当你点击某个圆圈时,某个星号应该会亮起,圆圈本身应该消失。 第一个问题 - 每个圆圈中应该有一个随机字母。我好像做了实现,乍一看是正确的,但是总是出现“M”而不是随机字母
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;
} ```
【问题讨论】: