15,979
社区成员
发帖
与我相关
我的任务
分享
// 方法一 移植到更高分辨率上,出现不在居中对齐
CEdit *pEdit = (CEdit *)GetDlgItem(nID);
CRect rcEdit;
pEdit->GetClientRect(&rcEdit);
TEXTMETRIC tm;
pEdit->GetDC()->GetTextMetrics(&tm);
int nFontHeight = tm.tmHeight + tm.tmExternalLeading;
int nOffY = (rcEdit.Height() - nFontHeight) / 2;
::OffsetRect(&rcEdit, 0, nOffY);
pEdit->SendMessage(EM_SETRECT, 0, (LPARAM)&rcEdit);
//方法二 使用MoveWindow 出现不居中对齐
void CVEdit::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
{
CEdit::OnNcCalcSize(bCalcValidRects, lpncsp);
CRect rectWnd, rectClient;
//calculate client area height needed for a font
CFont *pFont = GetFont();
CRect rectText;
rectText.SetRectEmpty();
CDC *pDC = GetDC();
CFont *pOld = pDC->SelectObject(pFont);
pDC->DrawText("Ky", rectText, DT_CALCRECT | DT_LEFT);
UINT uiVClientHeight = rectText.Height();
pDC->SelectObject(pOld);
ReleaseDC(pDC);
//calculate NC area to center text.
GetClientRect(rectClient);
GetWindowRect(rectWnd);
ClientToScreen(rectClient);
UINT uiCenterOffset = (rectClient.Height() - uiVClientHeight) / 2;
UINT uiCY = (rectWnd.Height() - rectClient.Height()) / 2;
UINT uiCX = (rectWnd.Width() - rectClient.Width()) / 2;
rectWnd.OffsetRect(-rectWnd.left, -rectWnd.top);
m_rectNCTop = rectWnd;
m_rectNCTop.DeflateRect(uiCX, uiCY, uiCX, uiCenterOffset + uiVClientHeight + uiCY);
m_rectNCBottom = rectWnd;
m_rectNCBottom.DeflateRect(uiCX, uiCenterOffset + uiVClientHeight + uiCY, uiCX, uiCY);
lpncsp->rgrc[0].top += uiCenterOffset;
lpncsp->rgrc[0].bottom -= uiCenterOffset;
lpncsp->rgrc[0].left += uiCX;
lpncsp->rgrc[0].right -= uiCY;
}
void CVEdit::OnNcPaint()
{
Default();
CWindowDC dc(this);
CBrush Brush(GetSysColor(COLOR_WINDOW));
dc.FillRect(m_rectNCBottom, &Brush);
dc.FillRect(m_rectNCTop, &Brush);
}
HBRUSH CVEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
if (m_rectNCTop.IsRectEmpty())
{
SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOOWNERZORDER | SWP_NOSIZE | SWP_NOMOVE | SWP_FRAMECHANGED);
}
return NULL;
}
BOOL CEdit::OnEraseBkgnd(CDC* pDC) //发现并不调用。设置不了背景颜色
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//CEdit::OnEraseBkgnd(pDC);
RECT rc, rc1;
GetClientRect(&rc);
// Get the color from the parent window
COLORREF crBkgnd = RGB(0,0,0);
//Fill the rect to overcome the black background issue
pDC->FillSolidRect(&rc, crBkgnd);
return TRUE;
}
//对刷新非客户区域颜色还存在问题,因为对MFC 并不了解。第一次写界面
//居中这样可以实现。
//可以判断 lpncsp[0]和lpncsp[3]是否一致,来判断是否使用 MoveWindow
//没有使用MoveWindow 可以使用这段代码
void CVEdit::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
{
CEdit::OnNcCalcSize(bCalcValidRects, lpncsp);
CRect rectWnd, rectClient;
//calculate client area height needed for a font
CFont *pFont = GetFont();
CRect rectText;
rectText.SetRectEmpty();
CDC *pDC = GetDC();
CFont *pOld = pDC->SelectObject(pFont);
pDC->DrawText("Ky", rectText, DT_CALCRECT | DT_LEFT);
UINT uiVClientHeight = rectText.Height();
pDC->SelectObject(pOld);
ReleaseDC(pDC);
//calculate NC area to center text.
GetClientRect(rectClient);
GetWindowRect(rectWnd);
ClientToScreen(rectClient);
UINT uiCenterOffset = (rectClient.Height() - uiVClientHeight) / 2;
UINT uiCY = (rectWnd.Height() - rectClient.Height()) / 2;
UINT uiCX = (rectWnd.Width() - rectClient.Width()) / 2;
rectWnd.OffsetRect(-rectWnd.left, -rectWnd.top);
m_rectNCTop = rectWnd;
m_rectNCTop.DeflateRect(uiCX, uiCY, uiCX, uiCenterOffset + uiVClientHeight + uiCY);
m_rectNCBottom = rectWnd;
m_rectNCBottom.DeflateRect(uiCX, uiCenterOffset + uiVClientHeight + uiCY, uiCX, uiCY);
lpncsp->rgrc[0].top += uiCenterOffset;
lpncsp->rgrc[0].bottom -= uiCenterOffset;
lpncsp->rgrc[0].left += uiCX;
lpncsp->rgrc[0].right -= uiCY;
}
// 如果使用MoveWindow 则通过下面这段代码来实现。
void CVEdit::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
{
CEdit::OnNcCalcSize(bCalcValidRects, lpncsp);
CRect rectWnd, rectClient;
//calculate client area height needed for a font
CFont *pFont = GetFont();
CRect rectText;
rectText.SetRectEmpty();
CDC *pDC = GetDC();
CFont *pOld = pDC->SelectObject(pFont);
pDC->DrawText("Ky", rectText, DT_CALCRECT | DT_LEFT);
UINT uiVClientHeight = rectText.Height();
pDC->SelectObject(pOld);
ReleaseDC(pDC);
//calculate NC area to center text.
GetClientRect(rectClient);
GetWindowRect(rectWnd);
ClientToScreen(rectClient);
//ScreenToClient(rectWnd);
//UINT uiCenterOffset = (rectClient.Height() - uiVClientHeight) / 2;
UINT uiCY = (rectWnd.Height() - rectClient.Height()) / 2;
UINT uiCX = (rectWnd.Width() - rectClient.Width()) / 2;
CRect newClinet(lpncsp->rgrc[0].left, lpncsp->rgrc[0].top, lpncsp->rgrc[0].right, lpncsp->rgrc[0].bottom);
UINT ucf = (newClinet.Height() - uiVClientHeight) / 2;
CRect newWindow(lpncsp->rgrc[0].left- uiCX, lpncsp->rgrc[0].top- uiCY, lpncsp->rgrc[0].right + uiCX, lpncsp->rgrc[0].bottom + uiCY);
CRect rect = rectWnd;
/*
rectWnd.OffsetRect(-rectWnd.left, -rectWnd.top);
m_rectNCTop = rectWnd;
m_rectNCTop.DeflateRect(uiCX, uiCY, uiCX, uiCenterOffset + uiVClientHeight + uiCY);
m_rectNCBottom = rectWnd;
m_rectNCBottom.DeflateRect(uiCX, uiCenterOffset + uiVClientHeight + uiCY, uiCX, uiCY);
*/
newWindow.OffsetRect(-newWindow.left, -newWindow.top);
m_rectNCTop = newWindow;
m_rectNCTop.DeflateRect(uiCX, uiCY, uiCX, ucf + uiVClientHeight + uiCY);
m_rectNCBottom = newWindow;
m_rectNCBottom.DeflateRect(uiCX, ucf + uiVClientHeight + uiCY, uiCX, uiCY);
lpncsp->rgrc[0].top += ucf;
lpncsp->rgrc[0].bottom -= ucf;
//lpncsp->rgrc[0].left += uiCX;
//lpncsp->rgrc[0].right -= uiCY;
}