我使用下面的代码获取与Windows MFC View窗口的客户区DC绑定(bind)的位图的大小:
void CView::OnDraw(CDC* )
{
CDC *pDc = GetDC();
BITMAP bmpHeader;
memset( &bmpHeader, 0, sizeof(BITMAP));
HGDIOBJ hbmp = GetCurrentObject(pDc->m_hDC, OBJ_BITMAP);
GetObject(hbmp,sizeof(BITMAP), &bmpHeader);
int bmpWidth = bmpHeader.bmWidth;
int bmpHeight = bmpHeader.bmHeight;
...
}
根据MSDN GetDC()获取客户区dc:
Retrieves a pointer to a common, class, or private device context for the client area depending on the class style specified for the CWnd
所以我想 bmpWidth 和 bmpHeight 应该与客户区矩形的大小相同。但事实并非如此。它似乎是整个窗口的大小,包括工具栏区域和菜单区域。我在这里做错了什么吗?
最佳答案
使用GetClientRect
找到客户区的宽度和高度。这是不包括标题栏和边框的区域。不要调用 GetDC()
,而是使用已经提供的 CDC*
参数,或者使用具有自动清理功能的 CClientDC dc(this)
.在这种情况下,绘图应该是这样的:
void CMyView::OnDraw(CDC* dc)
{
CRect rc;
GetClientRect(&rc);
dc->FillSolidRect(rc, RGB(0, 0, 255));
}
使用Window Functions获取有关 Windows 的信息。
大多数 Window 函数在 MFC 中都有等效项。例如,
在 WinApi 中:GetClientRect(HWND hwnd, LPRECT rc);
在 MFC 中:CWnd::GetClientRect(LPRECT rc);
关于c++ - Windows api获取客户端dc位图大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30251248/