如何使用纯winbgi设置BGI窗口大小?(不含graphics.h、winbgim)
解决WinBGI窗口大小设置问题
纯WinBGI未提供直接的窗口初始化大小配置函数,你可以借助它封装的Windows底层窗口句柄,调用原生Windows API来调整窗口尺寸:
- 获取WinBGI窗口句柄
WinBGI实例对象提供了获取底层窗口句柄的方法,通过getHWND()即可拿到:
HWND hWnd = win.getHWND();
- 调用Windows API调整窗口
使用SetWindowPos或MoveWindow函数设置窗口宽高与位置。以下示例将窗口设为800x600并居中显示:
#include <windows.h> // 需引入Windows API头文件 // 计算居中位置 int screenWidth = GetSystemMetrics(SM_CXSCREEN); int screenHeight = GetSystemMetrics(SM_CYSCREEN); int targetWidth = 800; int targetHeight = 600; int xPos = (screenWidth - targetWidth) / 2; int yPos = (screenHeight - targetHeight) / 2; // 用SetWindowPos设置窗口参数 SetWindowPos(hWnd, NULL, xPos, yPos, targetWidth, targetHeight, SWP_NOZORDER | SWP_NOACTIVATE); // 也可使用MoveWindow实现相同效果 // MoveWindow(hWnd, xPos, yPos, targetWidth, targetHeight, TRUE);
- 关键注意点
- 需在WinBGI窗口初始化完成后调用这些API,比如
win.begin()执行之后。 SWP_NOZORDER和SWP_NOACTIVATE参数用于保持窗口原有层级与激活状态,可根据需求调整。- 若仅需调整大小无需改变位置,可先用
GetWindowRect获取当前窗口位置,再传入对应坐标参数。
内容的提问来源于stack exchange,提问作者JSP
相关产品推荐
相关产品推荐

