我在::OnDraw(CDC * pDC)中加入下代码,编译通过,一执行就出错
CRect    rectbk;  //客户区大小
CDC      dcMen;   //内存设备描述表
CBitmap  bmbk;    //位图对象 bmbk.LoadBitmap (IDB_BACKGROUND); //得到位图的大小 BITMAP * pstBitmap = new BITMAP;->>> bmbk.GetObject (sizeof(BITMAP),pstBitmap);
CSize  bmsize(pstBitmap->bmWidth ,pstBitmap->bmHeight ); dcMen.CreateCompatibleDC (pDC);
CBitmap * poldBitmap = dcMen.SelectObject (&bmbk);

GetClientRect(&rectbk); //从内存向屏幕复制位图对象
pDC->StretchBlt (rectbk.left ,rectbk.top ,rectbk.Width (),
rectbk.Height(),&dcMen,0,0,bmsize.cx ,bmsize.cy,SRCCOPY); dcMen.SelectObject (poldBitmap); dcMen.DeleteDC ();
请高手指点.....

解决方案 »

  1.   

    A solution is :
    #include "MainFrm.h"
    #include "ChildFrm.h"
    #include "BackgroundImageDoc.h"
    #include "BackgroundImageView.h"#include <windows.h> // for messages#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CBackgroundImageAppBEGIN_MESSAGE_MAP(CBackgroundImageApp, CWinApp)
    //{{AFX_MSG_MAP(CBackgroundImageApp)
    ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //    DO NOT EDIT what you see in these blocks of generated code!
    //}}AFX_MSG_MAP
    // Standard file based document commands
    ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
    ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
    END_MESSAGE_MAP()// winapi sectionHBITMAP hBmp;// old wnd proc
    WNDPROC pfnOldWndProc;// new oneLRESULT CALLBACK pfnNewWndProc(HWND hwnd, UINT uMsg, WPARAM wParam,LPARAM lParam)
    {
    HDC hdc = (HDC)wParam;
    HDC hcompdc;
    RECT rect; switch (uMsg) {
    case WM_SIZE :
    SendMessage(hwnd, WM_ERASEBKGND, (WPARAM)GetDC(hwnd), 0);
    return 1;
    case WM_ERASEBKGND :
    //CallWindowProc(pfnOldWndProc, hwnd, uMsg, wParam, lParam);
    hcompdc = CreateCompatibleDC(hdc);
    SelectObject(hcompdc, hBmp);
    GetClientRect(hwnd, &rect); // bitmap dimensions are static for better performance (I assume
    // background image is statically loaded)
    if (0 == StretchBlt(hdc, rect.left, rect.top, rect.right, rect.bottom,
    hcompdc, 0, 0, 1152, 864, SRCCOPY))
    MessageBox(hwnd, "error", "while StretchBlt", MB_OK); DeleteDC(hcompdc);
    return 1;
    default :
    return CallWindowProc(pfnOldWndProc, hwnd, uMsg, wParam, lParam);
    }
    }/////////////////////////////////////////////////////////////////////////////
    // CBackgroundImageApp constructionCBackgroundImageApp::CBackgroundImageApp()
    {
    // TODO: add construction code here,
    // Place all significant initialization in InitInstance
    }/////////////////////////////////////////////////////////////////////////////
    // The one and only CBackgroundImageApp objectCBackgroundImageApp theApp;/////////////////////////////////////////////////////////////////////////////
    // CBackgroundImageApp initializationBOOL CBackgroundImageApp::InitInstance()
    {
    // Standard initialization
    // If you are not using these features and wish to reduce the size
    //  of your final executable, you should remove from the following
    //  the specific initialization routines you do not need.#ifdef _AFXDLL
    Enable3dControls(); // Call this when using MFC in a shared DLL
    #else
    Enable3dControlsStatic(); // Call this when linking to MFC statically
    #endif // Change the registry key under which our settings are stored.
    // TODO: You should modify this string to be something appropriate
    // such as the name of your company or organization.
    SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings();  // Load standard INI file options (including MRU) // Register the application's document templates.  Document templates
    //  serve as the connection between documents, frame windows and views. CMultiDocTemplate* pDocTemplate;
    pDocTemplate = new CMultiDocTemplate(
    IDR_BACKGRTYPE,
    RUNTIME_CLASS(CBackgroundImageDoc),
    RUNTIME_CLASS(CChildFrame), // custom MDI child frame
    RUNTIME_CLASS(CBackgroundImageView));
    AddDocTemplate(pDocTemplate); // create main MDI Frame window
    CMainFrame* pMainFrame = new CMainFrame;
    if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
    return FALSE;
    m_pMainWnd = pMainFrame; // Parse command line for standard shell commands, DDE, file open
    CCommandLineInfo cmdInfo;
    ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line
    if (!ProcessShellCommand(cmdInfo))
    return FALSE; hBmp = LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP1));
    if (hBmp == NULL)
    MessageBox(NULL, "Error", "Bitmap could not be loaded !!!", MB_OK); HWND hMain = pMainFrame->GetWindow(GW_CHILD)->GetSafeHwnd();
    pfnOldWndProc = (WNDPROC)GetWindowLong(hMain, GWL_WNDPROC);
    SetWindowLong(hMain, GWL_WNDPROC, (long)pfnNewWndProc); // The main window has been initialized, so show and update it.
    pMainFrame->ShowWindow(m_nCmdShow);
    pMainFrame->UpdateWindow(); return TRUE;
    }
      

  2.   

    CRect recBKGD(0,0,0,0) ;
    CDC *pDC = GetDC() ;
    CDC *pMemDC = new CDC ;
    CBitmap *pOldBmp = NULL ;
    GetClientRect(&recBKGD) ;
        pMemDC->CreateCompatibleDC(pDC) ;
    pOldBmp = pMemDC->SelectObject(&m_bkgdBmp) ;
    pDC->StretchBlt(0,0,
           recBKGD.Width(),recBKGD.Height(),
           pMemDC,
           0,0,
              m_bkgdBmpInfo.bmWidth,
           m_bkgdBmpInfo.bmHeight,
           SRCCOPY) ;
    pMemDC->SelectObject(pOldBmp) ;
    ReleaseDC(pDC) ;
    delete pMemDC ;