Clipboard data in MDI

This is a discussion on Clipboard data in MDI within the Windows Programming forums, part of the Platform Specific Boards category; ok i have this procedure that will get a bitmap from the clipboard and display it in this child MDI ...

  1. #1
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120

    Clipboard data in MDI

    ok i have this procedure that will get a bitmap from the clipboard and display it in this child MDI window but the image will not show up until i move the window off the screen or if i resize it, does anyone see any flaws in this code. I am using Visual C++ 6 and Windows API on a windows XP box.

    PHP Code:
    LRESULT CALLBACK ClipProcHWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam )
    {
        
    HDC hdc;
        
    HDC memhdc;
        
    PAINTSTRUCT ps;
        
    HWND hGetClip;
        
    RECT rect;
        
    BITMAP bm;
        static 
    HBITMAP hBitmap ;
        
    HBITMAP hBitmapClip;
        switch(
    Msg)
        {
            case 
    WM_CREATE:
            {        
                
    hGetClip CreateWindowEx(0,"BUTTON","Get Image",WS_CHILD WS_VISIBLE,
                    
    0,0,100,20,hWnd,(HMENU)HBUTTGETCLIP,NULL0);
                break;
            }
            case 
    WM_COMMAND:
            {
                switch(
    LOWORD(wParam))
                {
                    case 
    HBUTTGETCLIP:
                    {                    
                        if (
    hBitmap)
                        {
                            
    DeleteObject (hBitmap) ;
                            
    hBitmap NULL ;
                        }
                        
    OpenClipboard (copyhwnd);
                        
    hBitmapClip = (HBITMAP)GetClipboardData (CF_BITMAP) ;
                        
                        if (
    hBitmapClip)
                        {                    
                            
    hBitmap CopyBitmap (hBitmapClip) ;
                        }

                        
    CloseClipboard () ;
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                break;
            }
            case 
    WM_PAINT:
            {            
                
    hdc BeginPaint(hWnd,&ps);
                if (
    hBitmap)
                {
                    
    GetClientRect (hWnd, &rect) ;

                    
    memhdc CreateCompatibleDC (hdc) ;
                    
    SelectObject (memhdchBitmap) ;
                    
    GetObject (hBitmapsizeof (BITMAP), (PSTR) &bm);                    
                    
    SetStretchBltMode (hdcCOLORONCOLOR) ;
                    
    StretchBlt (hdc,    00rect.rightrect.bottom,
                        
    memhdc00bm.bmWidthbm.bmHeightSRCCOPY) ;

                    
    DeleteDC (memhdc) ;
                }
                
    EndPaint(copyhwnd,&ps);
                break;
            }
            default:
            {            
                break;
            }
        }

        return 
    DefMDIChildProc(hWndMsgwParamlParam);

    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Windows will not post a WM_PAINT message until the window or a portion of it is invalidated. This happens when the window is restored, uncovered, resized or can be done explicitly with the InvalidateRect() function.

    When you DeleteDC the device context must be in its original state. Note the following additions:

    Code:
                    memhdc = CreateCompatibleDC (hdc) ;
                    /* Save old bitmap so it can be restored later. */
                    HBITMAP hOldBitmap = (HBITMAP) SelectObject (memhdc, hBitmap) ;
                    GetObject (hBitmap, sizeof (BITMAP), (PSTR) &bm);                    
                    SetStretchBltMode (hdc, COLORONCOLOR) ;
                    StretchBlt (hdc,    0, 0, rect.right, rect.bottom,
                        memhdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY) ;
    
                    /* Return DC to its original state before deleting. */
                    SelectObject(memhdc, hOldBitmap);
                    DeleteDC (memhdc) ;

  3. #3
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    I have tried this and it still has the same outcome. it will only show the clipboard data if i resize teh child window or if i move the window so that the body is not visible in the parent, like move it so you can only see the title bar that you are dragging it by and then bringing it back up.
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Where is your call to InvalidateRect()? It should look something like:

    Code:
                    case HBUTTGETCLIP:
                    {                    
                        if (hBitmap)
                        {
                            DeleteObject (hBitmap) ;
                            hBitmap = NULL ;
                        }
                        OpenClipboard (copyhwnd);
                        hBitmapClip = (HBITMAP)GetClipboardData (CF_BITMAP) ;
                        
                        if (hBitmapClip)
                        {                    
                            hBitmap = CopyBitmap (hBitmapClip) ;
                        }
    
                        CloseClipboard () ;
                        /* Tell Windows to invalidate the entire client area and post a WM_PAINT message. */
                        InvalidateRect(hwnd, NULL, TRUE);
                        break;
                    }

  5. #5
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    ahh, ok. I was putting the wrong hwnd into the call. i had tried something before i posted and was using that hwnd instead. it works now, thanks.
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Also

    watch the uninitialised variables (ie hBitmap being tested for NULL)

    and non static vars ie the edits hwnd will be lost.

    The paints have a different hwnd used.

    I recomend after

    InvalidateRect(hwnd, NULL, TRUE);

    you tell the app to bypass the OS msg que and draw your window immediately with UpdateWindow()
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  2. Replies: 3
    Last Post: 04-18-2008, 10:06 AM
  3. Inserting text into MDI program
    By Rutabega in forum Windows Programming
    Replies: 0
    Last Post: 12-23-2005, 10:25 AM
  4. Dynamic data members?
    By confusalot in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 10:15 AM
  5. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 02:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21