我做了一個圖片查看程式,打開一張圖片后,再打開另一個圖片,前面的圖片殘影還在客戶區,只有重刷視窗大小才會重繪視窗。
我嘗試了各種方法都失敗了。
R = GetClientRect(hWnd, &rect);
if (R)
InvalidateRect(hWnd, &rect, FALSE);
FlashWindow(hWnd, TRUE);
HRGN hgn = CreateRectRgn(0, 0, rect.right, rect.bottom);
RedrawWindow(hWnd, &rect, hgn, RDW_UPDATENOW);
InvalidateRgn(hWnd, hgn, FALSE);
UpdateWindow(hWnd);
PostMessage(hWnd, WM_PAINT, 0, 0);
SendMessage(hWnd, WM_SIZE, 0, 0);
怎么才能重繪啊。
uj5u.com熱心網友回復:
關鍵看你的圖片是怎么畫的?
// SDI1Doc.h : CSDI1Doc 類的介面
#pragma once
#include <atlimage.h>
class CSDI1Doc : public CDocument
{
public:
CImage m_Img;
virtual BOOL OnOpenDocument(LPCTSTR lpszPathName)
{
if (! __super::OnOpenDocument(lpszPathName))
return FALSE;
//銷毀之前的影像
if(!m_Img.IsNull()) m_Img.Destroy();
//加載影像
return m_Img.Load(lpszPathName) == S_OK;
}
……
}
// SDI1View.cpp : CSDI1View 類的實作
void CSDI1View::OnDraw(CDC* pDC)
{
CSDI1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
//客戶區大小
CRect rcRect;
GetClientRect(&rcRect);
//填充背景色
pDC->FillSolidRect(&rcRect, RGB(200, 200, 200));
//繪制影像
if(!pDoc->m_Img.IsNull())
{
//影像大小
int imgCx = pDoc->m_Img.GetWidth();
int imgCy = pDoc->m_Img.GetHeight();
//計算縮放比例
double fZoom = min((double)rcRect.Width()/(double)imgCx,
(double)rcRect.Height() / (double)imgCy);
//計算居中顯示的尺寸&坐標
int drawCx = (int)(imgCx * fZoom);
int drawCy = (int)(imgCy * fZoom);
int drawSx = rcRect.left + (rcRect.Width() - drawCx)/2;
int drawSy = rcRect.top + (rcRect.Height() - drawCy)/2;
//繪制影像
pDoc->m_Img.Draw(pDC->m_hDC, drawSx, drawSy, drawCx, drawCy);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/114970.html
標籤:基礎類
