上次问过一次:http://bbs.bc-cn.net/dispbbs.asp?boardid=55&replyid=372186&id=176725&page=1&skin=0&star=1
上次的代码在后面看过后,觉得还有问题,改了下,主要是注释掉了OnOpen()中的那个DrawBMP()
void CBmpViewDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
DrawBmp();
CDialog::OnPaint();
}
}
void CBmpViewDlg::OnOpen()
{
// TODO: Add your control notification handler code here
CFileDialog pFiledlg(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "位图文件(*.bmp)|*.bmp|",NULL);
if(pFiledlg.DoModal())
{
m_strFile = pFiledlg.GetPathName();
}
if(m_strFile == "")
{
//没选择文件
return;
}
//刷新
//Invalidate();
hbitmap = (HBITMAP)::LoadImage (::AfxGetInstanceHandle(), m_strFile,
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
CRect rect;
CStatic* p_bitmap = (CStatic*)GetDlgItem(IDC_IMAGE);
p_bitmap->GetClientRect(&rect);
InvalidateRect(rect, TRUE);
UpdateWindow(); //刷新时会调用OnPaint()里的DrawBmp()
//DrawBmp();
}
void CBmpViewDlg::DrawBmp()
{
CRect rect;
CStatic* p_bitmap = (CStatic*)GetDlgItem(IDC_IMAGE);
p_bitmap->GetClientRect(&rect);
CWnd *pWnd = GetDlgItem(IDC_IMAGE);
CDC *pDC = pWnd->GetDC();
BITMAP m_Bm;
HBITMAP hOldBmp;
::GetObject(hbitmap, sizeof(m_Bm), &m_Bm);
CDC dcImage;
dcImage.CreateCompatibleDC(pDC);
hOldBmp = (HBITMAP)dcImage.SelectObject(hbitmap);
int x, y;
x = (rect.right - m_Bm.bmWidth) / 2;
y = (rect.bottom - m_Bm.bmHeight) / 2;
pDC->BitBlt(x, y, x + m_Bm.bmWidth, y + m_Bm.bmHeight, &dcImage, 0, 0, SRCCOPY);
dcImage.SelectObject(hOldBmp);
}
这次发现窗口每次最小化后恢复,位图是一闪就没了.感觉上面的代码应该是没问题了,可能是我对WINDOWS刷新机制认识有问题,如何改上面的代码才能不让位图一闪就消失呢?
[此贴子已经被作者于2007-10-15 16:17:09编辑过]