Copy an image in a dialog to clipboard - Programmer

This is a discussion on Copy an image in a dialog to clipboard - Programmer ; Hi, Hi, I am creating a dialog based application in MFC and trying to copy an image present in a bitmap dlg item rectangle to clipboard. I am encountering a problem when used the following source code. When only this ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Copy an image in a dialog to clipboard

  1. Copy an image in a dialog to clipboard

    Hi,
    Hi,

    I am creating a dialog based application in MFC and trying to copy
    an image present in a bitmap dlg item rectangle to clipboard.
    I am encountering a problem when used the following source code.
    When only this application is executed then it copies the correct
    image. But when this application runs in background and any other
    application(say MS Word) is opened on top of this, a region of MS Word
    is copied into clipboard instead of the image from the dialog.
    But My aim is to copy the image only from the dialog , even though it
    runs in back ground.
    This is the segment of code I am using.


    void CopyGraph()
    {
    CBitmap cBmp;
    CClientDC cWndDC(this); // View is an hWnd, so we can use "this"
    CDC cMemDC; // Handle to a memory DC
    CRect rect, MapRect;//For storing the size of the window

    cMemDC.CreateCompatibleDC(&cWndDC); // Create the memory DC.
    CWnd* pWnd;


    pWnd = (CWnd*) GetDlgItem(IDC_2D_CIRC_GRAPH_BMP2);

    pWnd->GetClientRect(rect);
    pWnd->GetClientRect(&MapRect);

    MapWindowPoints( pWnd,&MapRect);

    rect.left += -MapRect.left;
    rect.top += -MapRect.top;
    rect.right += rect.left;
    rect.bottom += rect.top;

    cBmp.CreateCompatibleBitmap(&cWndDC, rect.Width(), rect.Height() );

    // Keep the old bitmap
    CBitmap* pOldBitmap = cMemDC.SelectObject(&cBmp);

    cMemDC.BitBlt(0, 0, rect.Width(),rect.Height(), &cWndDC,
    rect.left, rect.top,
    SRCCOPY);

    // here are the actual clipboard functions.
    AfxGetApp()->m_pMainWnd->OpenClipboard() ;
    EmptyClipboard() ;
    SetClipboardData (CF_BITMAP, cBmp.GetSafeHandle() ) ;

    CloseClipboard () ;
    cMemDC.SelectObject(pOldBitmap);
    cBmp.Detach();
    return;
    }

    Any inputs on this will be of great help.

    Thanks and Regards,
    Srinivas

  2. Re: Copy an image in a dialog to clipboard

    Two things I notice with this are:

    1) Since you are getting a ClientDC (which has coords from 0, 0 through cx, cy), you don't need to convert them to any other system. Just use the 'rect' structure you have to bltblt. Ignore what you are doing with the MapRect struct.

    2) Before you put the HBITMAP handle on the clipboard, you need to deselect it out of the DC first. Go ahead and then detach it (which returns a HBITMAP) and use the returned handle in the SetClipboardData call.

    Here's the modified code:

    void CopyGraph()
    {
    CBitmap cBmp;
    CClientDC cWndDC(this); // View is an hWnd, so we can use "this"
    CDC cMemDC; // Handle to a memory DC
    CRect rect; //For storing the size of the window

    cMemDC.CreateCompatibleDC(&cWndDC); // Create the memory DC.
    CWnd* pWnd = (CWnd*) GetDlgItem(IDC_2D_CIRC_GRAPH_BMP2);
    if (pWnd)
    {
    pWnd->GetClientRect(rect);

    cBmp.CreateCompatibleBitmap(&cWndDC, rect.Width(), rect.Height() );
    // Keep the old bitmap
    CBitmap* pOldBitmap = cMemDC.SelectObject(&cBmp);
    cMemDC.BitBlt(0, 0, rect.Width(),rect.Height(), &cWndDC, 0, 0, SRCCOPY);
    cMemDC.SelectObject(pOldBitmap);

    // detatch the bitmap handle from the object so it won't get destroyed
    // after putting it in the clipboard
    HBITMAP hBitmap = cBmp.Detach();

    // here are the actual clipboard functions.
    AfxGetApp()->m_pMainWnd->OpenClipboard() ;
    EmptyClipboard() ;
    SetClipboardData (CF_BITMAP, hBitmap ) ;
    CloseClipboard () ;
    }
    }

    Relvinian

    "Srinivas" wrote in message news:872a40d6.0408182112.5ebeafe3@posting.google.c om...
    > Hi,
    > Hi,
    >
    > I am creating a dialog based application in MFC and trying to copy
    > an image present in a bitmap dlg item rectangle to clipboard.
    > I am encountering a problem when used the following source code.
    > When only this application is executed then it copies the correct
    > image. But when this application runs in background and any other
    > application(say MS Word) is opened on top of this, a region of MS Word
    > is copied into clipboard instead of the image from the dialog.
    > But My aim is to copy the image only from the dialog , even though it
    > runs in back ground.
    > This is the segment of code I am using.
    >
    >
    > void CopyGraph()
    > {
    > CBitmap cBmp;
    > CClientDC cWndDC(this); // View is an hWnd, so we can use "this"
    > CDC cMemDC; // Handle to a memory DC
    > CRect rect, MapRect;//For storing the size of the window
    >
    > cMemDC.CreateCompatibleDC(&cWndDC); // Create the memory DC.
    > CWnd* pWnd;
    >
    >
    > pWnd = (CWnd*) GetDlgItem(IDC_2D_CIRC_GRAPH_BMP2);
    >
    > pWnd->GetClientRect(rect);
    > pWnd->GetClientRect(&MapRect);
    >
    > MapWindowPoints( pWnd,&MapRect);
    >
    > rect.left += -MapRect.left;
    > rect.top += -MapRect.top;
    > rect.right += rect.left;
    > rect.bottom += rect.top;
    >
    > cBmp.CreateCompatibleBitmap(&cWndDC, rect.Width(), rect.Height() );
    >
    > // Keep the old bitmap
    > CBitmap* pOldBitmap = cMemDC.SelectObject(&cBmp);
    >
    > cMemDC.BitBlt(0, 0, rect.Width(),rect.Height(), &cWndDC,
    > rect.left, rect.top,
    > SRCCOPY);
    >
    > // here are the actual clipboard functions.
    > AfxGetApp()->m_pMainWnd->OpenClipboard() ;
    > EmptyClipboard() ;
    > SetClipboardData (CF_BITMAP, cBmp.GetSafeHandle() ) ;
    >
    > CloseClipboard () ;
    > cMemDC.SelectObject(pOldBitmap);
    > cBmp.Detach();
    > return;
    > }
    >
    > Any inputs on this will be of great help.
    >
    > Thanks and Regards,
    > Srinivas


+ Reply to Thread