C++窗口中球拍的边距错误

C++ Wrong margin of the rackets in a window

本文关键字:错误 窗口 C++      更新时间:2023-10-16
int width = 800;
int height = 600;
int interval = 1000 / 60;
int score_player1 = 0;
int score_player2 = 0;
int racket_width = 10;
int racket_height = 80;
int racket_speed = 8;
int racket_left_x = 10;
int racket_left_y = 50;
int racket_right_x = width - racket_width - 10;
int racket_right_y = 50;

完整代码(无球类(:http://pastebin.com/TA9NkV5c

从右球拍到窗口右侧的距离小于左侧。这些变量的计算是正确的,但仍然不平等。

https://i.stack.imgur.com/OjqtZ.png链接到图像

您已经将窗口设置为800像素的宽度,但其他所有内容都是相对于客户端区域定位的,客户端区域比窗口宽度窄边框的厚度。

使用AdjustWindowRect计算窗口的大小,以便使客户端区域达到所需的大小。

// Initialize a RECT with the size you want the client area to be.
RECT rc = { 0, 0, width, height };
// Now adjust the rectangle to the size the window would need to be.
AdjustWindowRect(&rc, my_style_flags, FALSE);
// Now create the window using the sizes in rc.  Make sure you use
// consistent style flags or the adjustment may not be correct.
const int window_width = rc.right - rc.left;
const int window_height = rc.bottom - rc.top;
my_hwnd = CreateWindow(..., my_style_flags, x, y, window_width, window_height, ...);