图片是文件外来的jpg格式
1。在对话框上加载背景图片
2。在文档上加载背景图片

解决方案 »

  1.   

    Re: How to use JPEG instead BMP 
    I used the source code from loadpic.exe, which is somewhere on the codeguru site, and use it in my own class. What I did was create a function that will load a jpg, gif, etc. into a bmp file. You call this function like this:CBitmap alfPicture;
    load("alf.jpg", alfPicture); or
    load("/graphics/alf.jpg", alfPicture);
    then you can use something like bitblt to draw it to the window. Here is the function:load(CString archivePath, CBitmap& picture)
    {
    TCHAR dir[MAX_PATH + 1]; 
    GetCurrentDirectory(MAX_PATH,dir);for (int i = 0; i < MAX_PATH; i++)
    {
    if (dir[i] == '\\')
    dir[i] = '/';
    }static LPPICTURE gpPicture;
    // open file
    HANDLE hFile = CreateFile((LPCTSTR)(dir + archivePath), GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
    _ASSERTE(INVALID_HANDLE_VALUE != hFile);// get file size
    DWORD dwFileSize = GetFileSize(hFile, NULL);
    _ASSERTE(-1 != dwFileSize);LPVOID pvData = NULL;
    // alloc memory based on file size
    HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
    _ASSERTE(NULL != hGlobal);pvData = GlobalLock(hGlobal);
    _ASSERTE(NULL != pvData);DWORD dwBytesRead = 0;
    // read file and store in global memory
    BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
    _ASSERTE(FALSE != bRead);
    GlobalUnlock(hGlobal);
    CloseHandle(hFile);LPSTREAM pstm = NULL;
    // create IStream* from global memory
    HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
    _ASSERTE(SUCCEEDED(hr) && pstm);// Create IPicture from image file
    if (gpPicture)
    gpPicture->Release();
    hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture);
    _ASSERTE(SUCCEEDED(hr) && gpPicture); 
    pstm->Release();CClientDC dcScreen(AfxGetMainWnd());
    CDC dcMem;
    dcMem.CreateCompatibleDC(&dcScreen);
    //get width and height of picture
    long hmWidth;
    long hmHeight;
    gpPicture->get_Width(&hmWidth);
    gpPicture->get_Height(&hmHeight);
    //convert himetric to pixels
    int nWidth = MulDiv(hmWidth, GetDeviceCaps(dcScreen, LOGPIXELSX), HIMETRIC_INCH);
    int nHeight = MulDiv(hmHeight, GetDeviceCaps(dcScreen, LOGPIXELSY), HIMETRIC_INCH);
    //create compatible bitmap
    picture.CreateCompatibleBitmap(&dcScreen, nWidth, nHeight);
    dcMem.SelectObject(&picture);
    //get the client rectangle
    RECT rc;
    GetClientRect((HWND)AfxGetMainWnd(), &rc);
    //display picture using IPicture::Render
    gpPicture->Render(dcMem, 0, 0, nWidth, nHeight, 0, hmHeight, hmWidth, -hmHeight, &rc);
    }What is at the center of this code is the LPPICTURE object which the picture file is loaded into, and from which the render function is called.
      

  2.   

    See the sample below, FYI: http://www.codeproject.com/bitmap/cximage.asp
      

  3.   

    给你粘个源文件,是一个BmpDialog:
    .cpp
    =====================
    // ***********************************************************************************
    //
    // BmpDlg.cpp : implementation file
    //
    // ************************************************************************************#include "stdafx.h"
    #include "BmpDlg.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif// In order to ease use, these values have been hard coded in bmpdlg.rc
    // This avoids the need for another header file.#define IDC_PREVIEW         (5000)
    #define IDC_PREVIEWBTN     (5001)
    #define IDC_WIDTH           (5002)
    #define IDC_HEIGHT          (5003)
    #define IDC_DEPTH           (5004)
    #define IDC_FSIZE           (5005)
    #define IDC_SHOWPREVIEW     (5006)HBITMAP CBmpDialog::hpreview = NULL;
    BOOL CBmpDialog::m_showpreview = TRUE;// Proprietary Hook function for open dialogUINT APIENTRY OFNHookProc( HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam )
    {
        
    LPDRAWITEMSTRUCT lpdis;
    BITMAP bm;
    LPNMHDR pnmh;
    char filename[1024],str[255];
    int height,height2,width,width2;
    NMHDR nmh;
        
        switch (uiMsg)
        {
            case WM_COMMAND:
                
                if (LOWORD(wParam) == IDC_SHOWPREVIEW)
                {
                    CBmpDialog::m_showpreview = IsDlgButtonChecked(hdlg,IDC_SHOWPREVIEW);                if (!CBmpDialog::m_showpreview)
                    {                    
                        if (CBmpDialog::hpreview)
                            DeleteObject(CBmpDialog::hpreview);                    CBmpDialog::hpreview = NULL;                    HWND wnd = GetDlgItem(hdlg,IDC_PREVIEWBTN);                    InvalidateRect(wnd,NULL,TRUE);                        SetDlgItemText(hdlg,IDC_WIDTH,"");
                        SetDlgItemText(hdlg,IDC_HEIGHT,"");
                        SetDlgItemText(hdlg,IDC_DEPTH,"");
                        SetDlgItemText(hdlg,IDC_FSIZE,"");                }
                    else
                    {
                        
                        nmh.code = CDN_SELCHANGE;                    OFNHookProc(hdlg, WM_NOTIFY, 0, (LPARAM)&nmh);
                    }
                }            break;        case WM_DRAWITEM:            if (CBmpDialog::hpreview)
                {
                    lpdis = (LPDRAWITEMSTRUCT)lParam;
                            
                    GetObject(CBmpDialog::hpreview,sizeof(BITMAP),&bm);                CPoint size(bm.bmWidth,bm.bmHeight);
                                                    
                    HDC dcmem = CreateCompatibleDC(lpdis->hDC);
          
                    HBITMAP old = (HBITMAP)SelectObject(dcmem,CBmpDialog::hpreview);                if (bm.bmWidth > bm.bmHeight)
                    {                    height = lpdis->rcItem.bottom - lpdis->rcItem.top;                    float ratio = (float)bm.bmHeight/(float)bm.bmWidth;                 
                        
                        lpdis->rcItem.bottom  = (long) (lpdis->rcItem.top + (lpdis->rcItem.right-lpdis->rcItem.left)*ratio);                    height2 = (height - (lpdis->rcItem.bottom - lpdis->rcItem.top))/2;                    lpdis->rcItem.top += height2;
                        lpdis->rcItem.bottom += height2;                }
                    else
                    {                    width = lpdis->rcItem.right - lpdis->rcItem.left;                    float ratio = (float)bm.bmWidth/(float)bm.bmHeight;                 
                        
                        lpdis->rcItem.right  = (long) (lpdis->rcItem.left + (lpdis->rcItem.bottom-lpdis->rcItem.top)*ratio);                    width2 = (width - (lpdis->rcItem.right - lpdis->rcItem.left))/2;                    lpdis->rcItem.left += width2;
                        lpdis->rcItem.right += width2;                }
                        
                    StretchBlt(lpdis->hDC,lpdis->rcItem.left,lpdis->rcItem.top,lpdis->rcItem.right-lpdis->rcItem.left,lpdis->rcItem.bottom-lpdis->rcItem.top,dcmem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);                SelectObject(dcmem,old);                DeleteDC(dcmem);
                }            break;
      

  4.   

    case WM_NOTIFY:
            
                pnmh = (LPNMHDR) lParam;            if (pnmh->code == CDN_FILEOK)
                {
                    if (CBmpDialog::hpreview)
                            DeleteObject(CBmpDialog::hpreview);                // This avoids an assert                _AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
                pThreadState->m_pAlternateWndInit = NULL;
                    return 0;
                }
                if (pnmh->code ==  CDN_INITDONE)
                {
                   if (CBmpDialog::hpreview)
                        DeleteObject(CBmpDialog::hpreview);                CheckDlgButton(hdlg,IDC_SHOWPREVIEW,CBmpDialog::m_showpreview);
                    return 0;
                }            if (pnmh->code == CDN_SELCHANGE)
                {                if (!IsDlgButtonChecked(hdlg,IDC_SHOWPREVIEW))
                    {                
                        if (CBmpDialog::hpreview)
                            DeleteObject(CBmpDialog::hpreview);                    CBmpDialog::hpreview = NULL;                    return 0;
                    }
                    
                    SendMessage(GetParent(hdlg),CDM_GETFILEPATH ,1024,(LPARAM)filename);                                if (CBmpDialog::hpreview)
                        DeleteObject(CBmpDialog::hpreview);
                   
                    CBmpDialog::hpreview = (HBITMAP)LoadImage(AfxGetInstanceHandle(),filename,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);                HWND wnd = GetDlgItem(hdlg,IDC_PREVIEWBTN);                InvalidateRect(wnd,NULL,TRUE);                if (CBmpDialog::hpreview)
                    {
                        GetObject(CBmpDialog::hpreview,sizeof(BITMAP),&bm);
            
                        wsprintf(str,"Width: %d pixels",bm.bmWidth);
                        SetDlgItemText(hdlg,IDC_WIDTH,str);                    wsprintf(str,"Height: %d pixels",bm.bmHeight);
                        SetDlgItemText(hdlg,IDC_HEIGHT,str);
                        
                        switch (bm.bmBitsPixel)
                        {
                            case 1:
                                SetDlgItemText(hdlg,IDC_DEPTH,"Colors: 2 (monocromatic)");
                                break;
                            case 4:
                                SetDlgItemText(hdlg,IDC_DEPTH,"Colors: 16");
                                break;                        case 8:
                                SetDlgItemText(hdlg,IDC_DEPTH,"Colors: 256");
                                break;                        case 16:
                                SetDlgItemText(hdlg,IDC_DEPTH,"Colors: 65,536");
                                break;
                            case 24:
                                SetDlgItemText(hdlg,IDC_DEPTH,"Colors: 16 millions");
                                break;                        default:
                                SetDlgItemText(hdlg,IDC_DEPTH,"");                    }                    OFSTRUCT o;                    HFILE f = OpenFile(filename,&o,OF_READ);                    wsprintf(str,"Size: %ld Kb",GetFileSize((HANDLE)f,NULL)/1024);                    SetDlgItemText(hdlg,IDC_FSIZE,str);
                        
                        _lclose(f);
                    }
                    else
                    {
                        SetDlgItemText(hdlg,IDC_WIDTH,"");
                        SetDlgItemText(hdlg,IDC_HEIGHT,"");
                        SetDlgItemText(hdlg,IDC_DEPTH,"");
                        SetDlgItemText(hdlg,IDC_FSIZE,"");
                    }
                                }    }
        return 0;
    }/////////////////////////////////////////////////////////////////////////////
    // CBmpDialogIMPLEMENT_DYNAMIC(CBmpDialog, CFileDialog)CBmpDialog::CBmpDialog(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
    DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
    CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)
    {    m_ofn.lpstrFilter = "Bitmaps (*.bmp)\0*.bmp\0"\
                            "All Files (*.*)\0*.*\0\0";                                m_ofn.Flags |= (OFN_HIDEREADONLY |OFN_ENABLEHOOK| OFN_EXPLORER  |OFN_ENABLETEMPLATE);    m_ofn.hInstance = AfxGetInstanceHandle();
        m_ofn.lpTemplateName = MAKEINTRESOURCE(IDC_PREVIEW);
        m_ofn.lpfnHook = OFNHookProc;          
    }
    BEGIN_MESSAGE_MAP(CBmpDialog, CFileDialog)
    //{{AFX_MSG_MAP(CBmpDialog)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
      

  5.   

    .h
    ===================
    /*************************************************************************BmpDlg.h header fileCBmpDialog: A bitmap open dialog with preview. 
                by The GremlinCompatibility: Visual C++ 4.0 and up.**************************************************************************/#ifndef _BMPDIALOG_H_
    #define _BMPDIALOG_H_
    // CBmpDialog dialogclass CBmpDialog : public CFileDialog
    {
    DECLARE_DYNAMIC(CBmpDialog)public:
    CBmpDialog(BOOL bOpenFileDialog = TRUE, // TRUE for FileOpen, FALSE for FileSaveAs. Who would want a save with preview?
    LPCTSTR lpszDefExt = NULL,
    LPCTSTR lpszFileName = NULL,
    DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
    LPCTSTR lpszFilter = NULL,
    CWnd* pParentWnd = NULL);    static BOOL m_showpreview;  // Store this variable in the registry if you want the "Show Preview" setting to be persistent    static HBITMAP hpreview;protected:
    //{{AFX_MSG(CBmpDialog)
    // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };
    #endif
      

  6.   

    在对话框上加背景图片代码:BOOL CBaozhan1Dlg::OnEraseBkgnd(CDC* pDC) 
    {
        CRect rect;
    GetWindowRect( &rect );
    CBitmap bmp;
        bmp.LoadBitmap(IDB_BITMAP1);
        CDC *pdc,memdc;
        pdc=GetDC();
        memdc.CreateCompatibleDC(pdc);
        memdc.SelectObject(&bmp);

    pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &memdc, 0, 0, SRCCOPY);

        return TRUE;

    }
      

  7.   

    OnEraseBkgnd(CDC* pDC) 是自己添加的吗
      

  8.   

    是父类的,请看:
    CWnd::OnEraseBkgnd