DrawDibDraw__ZC测试

ZC: 先把 自己尝试成功的代码 记录下来,不要 弄没了之后 又忘了怎么弄...

ZC: 代码 有点乱,没整理。没写 哪些是 原来MFC里面的 哪些是我自己写的,参考上一篇文章来看吧

 

1、VC6 的一个随便的MFC程序

2、class CDrawDibDraw_MFCDlg : public CDialog 

  添加成员:

public:
    HDRAWDIB m_hDrawDib; // 视频函数
    HANDLE mhBitsSrc; // 位图文件句柄(内存)
    LPSTR mpBitsSrc; // 位图文件地址(内存)
    BITMAPINFOHEADER *mpBitmapInfo; // 位图信息头

    CPoint mPos;
    CRect mDispR;
    int mFullViewTog;
    int mDispMethod;

    void GraphicDraw();
public:
    HANDLE FhFileBMP;
    //char FbufBmpMem[1024 * 1024 * 5];
    char* FbufBmpMem;
    DWORD FdwRead;

  2.1、

#include <windows.h>
#include <Vfw.h>
#pragma comment(lib,"Vfw32.lib")

  2.2、

CDrawDibDraw_MFCDlg::CDrawDibDraw_MFCDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CDrawDibDraw_MFCDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CDrawDibDraw_MFCDlg)
        // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

    m_hDrawDib= DrawDibOpen();

    mDispMethod = 10;
    mFullViewTog = 0;
// ***
//*
    DWORD dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
    DWORD dwShareMode = 0;
    LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL;
    DWORD dwCreationDisposition = OPEN_EXISTING;//OPEN_ALWAYS;
    DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
    HANDLE hTemplateFile = NULL;
    FhFileBMP = CreateFile("C:\\24bmp.bmp", dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
    if (FhFileBMP == NULL)
    {
        char bufErr[128] = {0};
        sprintf(bufErr, "CreateFile failed : (%ld)", GetLastError());
        printf("CreateFile fail(%ld)", GetLastError());
        MessageBox(bufErr, "1", 0);
        return;
    }

    DWORD dwReadBufLen = 1024 * 1024 * 5;
    FbufBmpMem = new char[dwReadBufLen];
    FdwRead = 0;
    int iRtn = ReadFile(FhFileBMP, FbufBmpMem, dwReadBufLen, &FdwRead, NULL);
    if (iRtn == 0)
    {
        printf("ReadFile fail(%ld)\n", GetLastError());
        char bufErr[128] = {0};
        sprintf(bufErr, "ReadFile failed : (%ld)", GetLastError());
        MessageBox(bufErr, "2", 0);
        return;
    }
    else
    {
        //凡是从文件流中读出来的字符串都不会自动补\0,需要手动填充
        FbufBmpMem[FdwRead / sizeof(char)] = '\0';
        printf("ReadFile -> len=%ld, buf=%ls\n", FdwRead, FbufBmpMem);

        char bufErr[128] = {0};
        sprintf(bufErr, "ReadFile --> len =%ld, buf=%ls\n", FdwRead, FbufBmpMem);
        MessageBox(bufErr, "3", 0);

        mpBitsSrc = &FbufBmpMem[0];

        mpBitmapInfo = (BITMAPINFOHEADER*)&FbufBmpMem[sizeof(BITMAPFILEHEADER)];
    }
//*/
}

  2.3、

void CDrawDibDraw_MFCDlg::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
    {
        //CDialog::OnPaint();

        CPaintDC dc(this); // device context for painting
        GraphicDraw();
    }
}

  2.4、

void CDrawDibDraw_MFCDlg::GraphicDraw()
{
    GetWindowRect(&mDispR);
    

    CClientDC dc(this); // device context for painting
    BITMAPFILEHEADER *pBitmapFileHeader;
    ULONG bfoffBits= 0;
    CPoint Wid;
    // 图形文件名有效 (=0 BMP)
//    if( mBitmapFileType < ID_BITMAP_BMP )
//        return;
    // 图形文件名有效 (=0 BMP)
    // 准备显示真彩位图
    pBitmapFileHeader = (BITMAPFILEHEADER *) mpBitsSrc;
    bfoffBits = pBitmapFileHeader->bfOffBits;
    // 使用普通函数显示位图
    if( m_hDrawDib == NULL || mDispMethod == 0)
    {
        HBITMAP hBitmap = ::CreateDIBitmap(dc.m_hDC,
            mpBitmapInfo, CBM_INIT, mpBitsSrc+bfoffBits,
            (LPBITMAPINFO) mpBitmapInfo,DIB_RGB_COLORS); 
        // 建立位图
        HDC hMemDC = ::CreateCompatibleDC(dc.m_hDC);// 建立内存
        //HBITMAP hBitmapOld = SelectBitmap(hMemDC, hBitmap); // 选择对象
        HBITMAP hBitmapOld = (HBITMAP)SelectObject(hMemDC, hBitmap); // 选择对象
        // 成员CRect mDispR用于指示图形显示区域的大小.
        // 成员CPoint mPos用于指示图形显示起始位置坐标.
        if( mPos.x > (mpBitmapInfo->biWidth - mDispR.Width()) )
            mPos.x= mpBitmapInfo->biWidth - mDispR.Width() ;
        if( mPos.y > (mpBitmapInfo->biHeight- mDispR.Height()) )
            mPos.y= mpBitmapInfo->biHeight- mDispR.Height();
        if( mPos.x < 0 ) mPos.x= 0;
        if( mPos.y < 0 ) mPos.y= 0;
        if( mFullViewTog == 0)
        {
            // 显示真彩位图
            ::BitBlt(dc.m_hDC, 0, 0, mDispR.Width(), mDispR.Height(),
                hMemDC, mPos.x, mPos.y, SRCCOPY);
        }
        else
        {
            ::StretchBlt(dc.m_hDC,0,0, mDispR.Width(), mDispR.Height(),
                hMemDC,0,0, mpBitmapInfo->biWidth, mpBitmapInfo->biHeight, SRCCOPY);
        }
        // 结束显示真彩位图
        ::DeleteObject(SelectObject(hMemDC,hBitmapOld)); 
        // 删 除 位 图
    } 
    else
    {
        // 使用视频函数显示位图
        if( mPos.x > (mpBitmapInfo->biWidth - mDispR.Width() ))
            mPos.x= mpBitmapInfo->biWidth - mDispR.Width() ;
        if( mPos.y > (mpBitmapInfo->biHeight- mDispR.Height()))
            mPos.y= mpBitmapInfo->biHeight- mDispR.Height();
        if( mPos.x < 0 )
            mPos.x= 0;
        if( mPos.y < 0 )
            mPos.y= 0;
        // 显示真彩位图
        DrawDibRealize( m_hDrawDib, dc.GetSafeHdc(), TRUE);
        if( mFullViewTog == 0)
        {
            Wid.x= mDispR.Width();
            Wid.y= mDispR.Height();
            // 1:1 显示时, 不能大于图形大小
            if( Wid.x > mpBitmapInfo->biWidth )
                Wid.x = mpBitmapInfo->biWidth;
            if( Wid.y > mpBitmapInfo->biHeight)
                Wid.y = mpBitmapInfo->biHeight;
            DrawDibDraw( m_hDrawDib, dc.GetSafeHdc(), 0, 0, Wid.x, Wid.y,
                mpBitmapInfo, (LPVOID) (mpBitsSrc+bfoffBits),
                mPos.x, mPos.y, Wid.x, Wid.y, DDF_BACKGROUNDPAL);
        }
        else
        {
            DrawDibDraw( m_hDrawDib, dc.GetSafeHdc(),
                0, 0, mDispR.Width(), mDispR.Height(),
                mpBitmapInfo, (LPVOID) (mpBitsSrc+bfoffBits),
                0, 0, mpBitmapInfo->biWidth, mpBitmapInfo->biHeight,
                DDF_BACKGROUNDPAL);
        }
    }
    return;
}

 

3、

4、

5、

 

posted @ 2017-08-22 12:20  CppSkill  阅读(714)  评论(0编辑  收藏  举报