Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm using the MFC ribbon template to create a SDI application.
I add a new class(Cchildview) which is inherit from CWnd to mainframe.cpp.


in my mainframe.h
CChildView m_wndView

C++
// create a view to occupy the client area of the frame
	if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
		CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
	{
		TRACE0("Failed to create view window\n");
		return -1;
	}


in my childview.cpp, it will load the img and display in onpaint function as below.

C++
void CChildView::OnPaint()
{
	CString szFilename("baseimg.bmp");
	HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, szFilename,
		IMAGE_BITMAP, 0, 0,
		LR_LOADFROMFILE | LR_CREATEDIBSECTION);

	CBitmap bmp;
	bmp.Attach(hBmp);

	CClientDC dc(this);
	CDC bmDC;
	bmDC.CreateCompatibleDC(&dc);
	CBitmap *pOldbmp = bmDC.SelectObject(&bmp);

	BITMAP  bi;
	bmp.GetBitmap(&bi);

	dc.BitBlt(0, 0, bi.bmWidth, bi.bmHeight, &bmDC, 0, 0, SRCCOPY);

	bmDC.SelectObject(pOldbmp);
}


My image is able to show at screen. But when I adjust the separator boarder, My image is disappear, and if i readjust again, it able to display, then if i adjust again, it will show again @@.
Any idea why will this happen?

What I have tried:

use other method to load the image. result still same.
Posted
Updated 4-Mar-20 15:49pm

Instead of

C++
CClientDC dc(this);


try

C++
CPaintDC dc(this);
 
Share this answer
 
Comments
Member 12275258 4-Mar-20 9:52am    
I had try below method, but still having same problem.

I load the bitmap at precreatewindows
BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs)
{
	if (!CWnd::PreCreateWindow(cs))
		return FALSE;

	cs.dwExStyle |= WS_EX_CLIENTEDGE;
	cs.style &= ~WS_BORDER;
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS,
		::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW + 1), NULL);

	//if (!ggtest)
	//{
	ggtest = true;
	CImage image;
	image.Load(_T("baseimg.bmp"));
	//CBitmap bitmap;
	m_Bitmap.Attach(image.Detach());
	//}


	return TRUE;
}


and OnPaint change to this.
void CChildView::OnPaint()
{

	CPaintDC PaintDC(this);
	HDC hDC = ::GetDC(NULL);
	// Bitmap for drawing.
	CBitmap Drawing;
	Drawing.CreateCompatibleBitmap(&PaintDC, 1600, 1200);
	COLORREF m_BkColor;
	m_BkColor = BLACK;
	// Double buffering.
	CDC MemDC1;
	CDC MemDC2;
	MemDC1.CreateCompatibleDC(&PaintDC);
	MemDC2.CreateCompatibleDC(&PaintDC);
	MemDC2.SetStretchBltMode(HALFTONE);
	MemDC2.SetBkColor(m_BkColor);
	CBitmap* pBitmap1 = MemDC1.SelectObject(&m_Bitmap);
	CBitmap* pBitmap2 = MemDC2.SelectObject(&Drawing);

	// Copy scale bitmap.
	MemDC2.StretchBlt(0, 0, 1600, 1200, &MemDC1, 0, 0, 1600, 1200, SRCCOPY);
	
	// Copy memory buffer to display.
	PaintDC.BitBlt(0, 0, 1600, 1200, &MemDC2, 0, 0, SRCCOPY);

	// Release buffers.
	MemDC1.SelectObject(pBitmap1);
	MemDC2.SelectObject(pBitmap2);
}


but the problem still persist...
Shao Voon Wong 4-Mar-20 19:16pm    
You can try overriding your WM_SIZE handler so that it redraws your View after resize is done.

BEGIN_MESSAGE_MAP(CChildView, CView)
	ON_WM_SIZE() // add this line to your message map
END_MESSAGE_MAP()

void CChildView::OnSize(UINT nType, int cx, int cy)
{
    CView::OnSize(nType, cx, cy);
    Invalidate(TRUE); // add this line to redraw your CChildView
}
Member 12275258 4-Mar-20 21:50pm    
Hi,

Thx you, but still having the same problem if apply OnSize().
Hi,

I already solved it by just load and display (Ondraw function) the image in view class.

Thank You.
 
Share this answer
 
Comments
Shao Voon Wong 4-Mar-20 22:55pm    
By right, you only load the image once and display it in OnDraw().

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900