OnEraseBkgnd删除绘制的图片. [英] OnEraseBkgnd erases drawn picture.

查看:89
本文介绍了OnEraseBkgnd删除绘制的图片.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对话框和选项卡控件.我需要在Tab控件的页面上的确定矩形中绘制图片.因此,我将负责位图工作的类的对象声明为CDIBitmap m_bmpBackground.位图文件加载有:

I have a dialog and Tab Control on it. I need draw a picture in the definite rectangle on a page of the Tab Control. So I declare an object of a class that is responsible for bitmap work for example as CDIBitmap m_bmpBackground. Bitmap file is loaded with:

BOOL CDIBitmap :: Load( CFile* pFile ) {
    ASSERT( pFile );
    BOOL fReturn = TRUE;
    try {
        delete [] (BYTE*)m_pInfo;
        delete [] m_pPixels;
        m_pInfo = 0;
        m_pPixels = 0;
        DWORD       dwStart = pFile->GetPosition();
        //
        // Check to make sure we have a bitmap. The first two bytes must
        // be ''B'' and ''M''.
        BITMAPFILEHEADER fileHeader;
        pFile->Read(&fileHeader, sizeof(fileHeader));
        if( fileHeader.bfType != 0x4D42 )
            throw TEXT("Error:Unexpected file type, not a DIB\n");

        BITMAPINFOHEADER infoHeader;
        pFile->Read( &infoHeader, sizeof(infoHeader) );
        if( infoHeader.biSize != sizeof(infoHeader) )
            throw TEXT("Error:OS2 PM BMP Format not supported\n");

        // Store the sizes of the DIB structures
        int cPaletteEntries = GetPalEntries( infoHeader );
        int cColorTable = 256 * sizeof(RGBQUAD);
        int cInfo = sizeof(BITMAPINFOHEADER) + cColorTable;
        int cPixels = fileHeader.bfSize - fileHeader.bfOffBits;
        //
        // Allocate space for a new bitmap info header, and copy
        // the info header that was loaded from the file. Read the
        // the file and store the results in the color table.
        m_pInfo = (BITMAPINFO*)new BYTE[cInfo];
        memcpy( m_pInfo, &infoHeader, sizeof(BITMAPINFOHEADER) );
        pFile->Read( ((BYTE*)m_pInfo) + sizeof(BITMAPINFOHEADER),
                     cColorTable );
        //
        // Allocate space for the pixel area, and load the pixel
        // info from the file.
        m_pPixels = new BYTE[cPixels];
        pFile->Seek(dwStart + fileHeader.bfOffBits, CFile::begin);
        pFile->Read( m_pPixels, cPixels );
		CreatePalette();
		m_bIsPadded = TRUE;
#ifdef _DEBUG
    } catch( TCHAR * psz ) {
		TRACE( psz );
#else
    } catch( TCHAR * ) {
#endif
        fReturn = FALSE;
    }
    return fReturn;
}



然后,我调用Invalidate()函数并进入实际绘制图片的OnEraseBkgnd:



Then I call Invalidate() function and get into OnEraseBkgnd where a picture is actually being drawn:

BOOL CAtmNcr::OnEraseBkgnd(CDC* pDC) 
{
	if(m_bmpBackground.GetPixelPtr() != 0) 
	{
		CRect rc;
		
		m_myWnd.GetWindowRect(rc);
		ScreenToClient( &rc );
		int x = 0, y = 0;

		// center the bitmap
		/*
		CDialog::OnEraseBkgnd(pDC);
		x = (rc.Width() - m_bmpBackground.GetWidth()) / 2;
		y = (rc.Height() - m_bmpBackground.GetHeight()) / 2;
		m_bmpBackground.DrawDIB(pDC, x, y);
		*/

		// stretch
		m_bmpBackground.DrawDIB(pDC, rc.left, rc.top, rc.Width(), rc.Height());
	} 
	else
		// no bitmap set. behave like a normal dialog
		return CDialog::OnEraseBkgnd(pDC);

	return true;
}



DrawDIB的定义如下:



DrawDIB is defined as follows:

// DrawDib uses StretchDIBits to display the bitmap.
void CDIBitmap :: DrawDIB( CDC* pDC, int x, int y, int width, int height ) {
    ASSERT( pDC );
    HDC     hdc = pDC->GetSafeHdc();

    if( m_pInfo )
        StretchDIBits( hdc,
                       x,
                       y+GetHeight(),
                       width,
                       -height,
                       0,
                       0,
                       GetWidth(),
                       GetHeight(),
                       GetPixelPtr(),
                       GetHeaderPtr(),
                       DIB_RGB_COLORS,
                       SRCCOPY );
	
}



加载的图片肯定是在矩形中绘制的,您可以看到它,直到剩下OnEraseBkgnd为止.怎么会?如果您将相同的代码应用于父对话框窗口,则它可以正常工作.



The loaded picture is definitely drawn in the rectangle and you see it but untill OnEraseBkgnd is left. How come? If you apply the same code to parent dialog window then it works perfectly.

推荐答案

我猜您的图像正在绘制,但是在WM_PAINT上被擦除了.尝试在WM_PAINT上绘制图像-如果在整个父窗口(选项卡ctl)上绘图,则是个好主意.否则,我建议您对尺寸和尺寸保持静态控制.您想要的位置利用它.

如果要维护它们,还请确保在页面控件/窗口上绘制,而不是在标签控件本身上绘制,因为实际的标签控件将在它们后面.
I guess your image is being drawn, but erased on WM_PAINT. Try blitting the image on WM_PAINT - a good idea if you''re drawing on the whole parent window (your tab ctl). Or else, i suggest you keep a static control of the size & pos you want & draw on that.

Also make sure you''re drawing on the page control/window if you''re maintaining them and not the tab control itself because the actual tab control will be behind them.


谢谢,strogg.你是对的. WM_PAINT删除了我绘制的图片,因为我在绘制之前确实使用了静态控件来获取矩形的大小,并且该静态控件最后放置在了我的图片上.因此,当定义了静态控件的尺寸并仅绘制图片时,我将其隐藏.感谢您的帮助,并感谢您的宝贵时间.
Thank you, strogg. You''re right. WM_PAINT erased my drawn picture as I really used static control to get size of a rectangle before drawing and the static control was put over my picture in the last. So I hide static control when its dimensions are defined and only then a picture is drawn. I appreciate your help and thank you for your time.


这篇关于OnEraseBkgnd删除绘制的图片.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆