编程技巧20法之八:将窗体复制到剪切板

发表于:2007-06-17来源:作者:点击数: 标签:
17.如何将指定的窗体,以位图形式复制到系统剪切板上 voidCScreenSnapDlg::toClipboard_Bio(CWnd*wnd,BOOLFullWnd) { CDC*dc; if(FullWnd) { /*抓取整个窗口*/ dc=newCWindowDC(wnd); } else { /*仅抓取客户区时*/ dc=new CC lientDC(wnd); } CDCmemDC; memD

          
17.如何将指定的窗体,以位图形式复制到系统剪切板上 

void CScreenSnapDlg::toClipboard_Bio(CWnd * wnd, BOOL FullWnd)
{
     CDC *dc;

     if(FullWnd)
     { 
          /* 抓取整个窗口 */
          dc = new CWindowDC(wnd);
     }      
     else
     { 
          /* 仅抓取客户区时 */
          dc = new CClientDC(wnd);
     } 

     CDC memDC;
     memDC.CreateCompatibleDC(dc);

     CBitmap bm;
     CRect r;
     if(FullWnd)
          wnd->GetWindowRect(&r);
     else
          wnd->GetClientRect(&r);

     CString s;
     wnd->GetWindowText(s);
     CSize sz(r.Width(), r.Height());
     bm.CreateCompatibleBitmap(dc, sz.cx, sz.cy);

     CBitmap * oldbm = memDC.SelectObject(&bm);
     memDC.BitBlt(0, 0, sz.cx, sz.cy, dc, 0, 0, SRCCOPY);
     //直接调用OpenClipboard(),而不用wnd->GetParent()->OpenClipboard();
          wnd->OpenClipboard();

     ::EmptyClipboard();
     ::SetClipboardData(CF_BITMAP, bm.m_hObject);
     CloseClipboard();
     //恢复原始环境
     memDC.SelectObject(oldbm);
     bm.Detach();  
     delete dc;
}
 
18.如何替换HBITMAP中的颜色值 

#define COLORREF2RGB(Color) (Color & 0xff00) | ((Color >> 16) & 0xff) 
                                             | ((Color << 16) & 0xff0000)
HBITMAP ReplaceColor (HBITMAP hBmp,COLORREF cOldColor,COLORREF cNewColor)
{
    HBITMAP RetBmp=NULL;
    if (hBmp)
    {    
        HDC BufferDC=CreateCompatibleDC(NULL);     // 源位图DC
        if (BufferDC)
        {
            SelectObject(BufferDC,hBmp);   // 选入DC中
            
            HDC DirectDC=CreateCompatibleDC(NULL);      // 目标DC
            if (DirectDC)
            {
                // 获取源位图大小
                BITMAP bm;
                GetObject(hBmp, sizeof(bm), &bm);
                
                // 初始化BITMAPINFO信息,以便使用CreateDIBSection
                BITMAPINFO RGB32BitsBITMAPINFO; 
                ZeroMemory(&RGB32BitsBITMAPINFO,sizeof(BITMAPINFO));
                RGB32BitsBITMAPINFO.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
                RGB32BitsBITMAPINFO.bmiHeader.biWidth=bm.bmWidth;
                RGB32BitsBITMAPINFO.bmiHeader.biHeight=bm.bmHeight;
                RGB32BitsBITMAPINFO.bmiHeader.biPlanes=1;
                RGB32BitsBITMAPINFO.bmiHeader.biBitCount=32;
                UINT * ptPixels;    
                HBITMAP DirectBitmap= CreateDIBSection(DirectDC,
                                              (BITMAPINFO *)&RGB32BitsBITMAPINFO, 
                                              DIB_RGB_COLORS,(void **)&ptPixels, NULL, 0);
                if (DirectBitmap)
                {
                    HGDIOBJ PreviousObject=SelectObject(DirectDC, DirectBitmap);
                    BitBlt(DirectDC,0,0,bm.bmWidth,bm.bmHeight,BufferDC,0,0,SRCCOPY);

                    // 转换 COLORREF 为 RGB
                    cOldColor=COLORREF2RGB(cOldColor);
                    cNewColor=COLORREF2RGB(cNewColor);

                    // 替换颜色
                    for (int i=((bm.bmWidth*bm.bmHeight)-1);i>=0;i--)
                    {
                        if (ptPixels[i]==cOldColor) 
                              ptPixels[i]=cNewColor;
                    }
                    // 修改位图 DirectBitmap
                    SelectObject(DirectDC,PreviousObject); // 完成
                    RetBmp=DirectBitmap;
                }
                // 释放DC
                DeleteDC(DirectDC);
            }
            // 释放DC
            DeleteDC(BufferDC);
        }
    }
    return RetBmp;
}
 
用法: 

HBITMAP hBmp2 = LoadBitmap(g_hinstance,MAKEINTRESOURCE(IDB_SAMPLEBITMAP));
HBITMAP hBmp = ReplaceColor(hBmp2,0xff0000,0x00ff00); // 替换蓝色为绿色
......
DeleteObject(hBmp2);
DeleteObject(hBmp);

原文转自:http://www.ltesting.net