16,548
社区成员




void CMyButton::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CButton::OnPaint() for painting messages
CDC *pDC = &dc;
//保存设备现场
int nSaveDC = pDC->SaveDC();
//获取客户区位置&大小
CRect rcRect;
GetClientRect(&rcRect);
//获取位图
HBITMAP hBmp = GetBitmap();
BITMAP bmpInfo = {0};
if(hBmp && GetObject(hBmp, sizeof(bmpInfo), &bmpInfo))
{
//创建兼容DC
CDC memDC;
memDC.CreateCompatibleDC(pDC);
//选人位图
HGDIOBJ hOldBmp = memDC.SelectObject(hBmp);
//设置缩放模式
pDC->SetStretchBltMode(HALFTONE);
//缩放贴图
pDC->StretchBlt(rcRect.left, rcRect.top, rcRect.Width(), rcRect.Height(),
&memDC,
0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight,
SRCCOPY);
//恢复内存DC位图
memDC.SelectObject(hOldBmp);
}
//恢复现场
pDC->RestoreDC(nSaveDC);
}
//#define USE_DEFER
void CCenterDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
CRect rcTmp;
// LockWindowUpdate(); has to be in OnSysCommand();
int iControlCount = 1;
#ifdef USE_DEFER
HDWP hdwp = BeginDeferWindowPos(11);
#endif
#define Border 5
int iCaptionHeight = GetSystemMetrics(SM_CYCAPTION);
if(!IsWindow(m_btMax.m_hWnd)) return;
if (nType == SIZE_RESTORED)// =0
{
m_btMax.SetWindowText("Maximize");
iCaptionHeight +=Border;// not 3; 28+5=33
CWnd* pWnd = GetTopWindow();
while(pWnd != NULL)
{
rcTmp=rcOrig[iControlCount];
rcTmp.top -= (rcOrig[0].top+iCaptionHeight);
rcTmp.bottom-= (rcOrig[0].top+iCaptionHeight);
rcTmp.left -= (rcOrig[0].left+Border);
rcTmp.right -= (rcOrig[0].left+Border);
#ifdef USE_DEFER
DeferWindowPos(hdwp, pWnd->m_hWnd, NULL,
rcTmp.left, rcTmp.top,rcTmp.Width(),rcTmp.Height(),
SWP_NOZORDER);
#else
pWnd->MoveWindow(&rcTmp,FALSE);
#endif
pWnd = pWnd->GetNextWindow();
iControlCount++;
}
}
//
if (nType == SIZE_MAXIMIZED)// =2
{
m_btMax.SetWindowText("Restore");
CWnd* pWnd = GetTopWindow();
while(pWnd != NULL)
{
rcTmp=rcOrig[iControlCount];
rcTmp.top -= iCaptionHeight;
rcTmp.bottom -= iCaptionHeight;
#ifdef USE_DEFER
DeferWindowPos(hdwp, pWnd->m_hWnd, NULL,
rcTmp.left, rcTmp.top,rcTmp.Width(),rcTmp.Height(),
SWP_NOZORDER);
#else
pWnd->MoveWindow(&rcTmp,FALSE);
#endif
pWnd = pWnd->GetNextWindow();
iControlCount++;
}
}
// redraw all the windows
#ifdef USE_DEFER
EndDeferWindowPos(hdwp);
#endif
// try it ? there are 2 pictures !
// 1st is a picture in which no ctrols moves.
// 2nd is a picture in which all ctrols moved.
// that's why the window be flickering.
// However with LockWindowUpdate(), you will not see 2 pictures !
// Sleep(1500);// to see the 2 pictures.
UnlockWindowUpdate();
}