我正在尝试绘制一个4x4网格的100x100矩形,其间有50px,用于2048游戏。我已经按照教程并尝试在窗口创建时调用drawCases()函数。如果我注释掉以下行,则会创建窗口并且一切正常(除了不绘制矩形):
FillRect(hdc,&rectangles[i][j],(HBRUSH)GetStockObject(LTGRAY_BRUSH));
否则程序崩溃。 这是整个功能:
void drawCases(HWND hwnd){
HDC hdc = GetDC(hwnd);
// Error Check
if(!hdc)
return;
RECT clientRect;
RECT rectangles[4][4];
GetClientRect(hwnd,&clientRect); // Get the window's client area RECT
FillRect(hdc,&clientRect,(HBRUSH)GetStockObject(BLACK_BRUSH));
int leftStart = (clientRect.right)/2 - 200;
int topStart = (clientRect.bottom)/2 - 200;
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; i++){
int k = j * 150;
int n = i * 150;
rectangles[i][j].left = k + leftStart;
rectangles[i][j].right = k + leftStart + 100;
rectangles[i][j].top = topStart + n;
rectangles[i][j].bottom = topStart + n + 100;
FillRect(hdc,&rectangles[i][j],(HBRUSH)GetStockObject(LTGRAY_BRUSH));
}
}
ReleaseDC(hwnd,hdc);
}
感谢您的帮助!
答案 0 :(得分:5)
这可能是错误:
for (int j = 0; j < 4; i++){
将i++
替换为j++