• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 29,comments - 10,trackbacks - 0
               事實上,有兩種不同的剪貼板接口機制。第一種機制是使用Windows剪貼板API,第二種機制則是使用OLE。以為剪貼板API是迄今為止最常用的方法,一次本章中主要介紹使用Windows剪貼板API。
            1、相關(guān)函數(shù)的介紹
            1)為數(shù)據(jù)分配內(nèi)存空間:HGLOBAL GlobalAlloc(UINT uFlags,SIZE_T dwBytes),uFlags參數(shù)用來說明Windows如何分配內(nèi)存(有GHND、GMEM_FIXED(默認值,可以用0或NULL代替)、GMEM_MOVEABLE和GPTR)。參數(shù)dwBytes是一個雙字節(jié)(DWORD)值,用來指定分配的緩沖區(qū)的大小。
            2)鎖定全局內(nèi)存:LPVOID GlobalLock(HGLOBAL hMem)
            3)解除對全局內(nèi)存的鎖定:BOOL GlobalUnlock(HGLOBAL hMem)
            4)打開剪貼板:BOOL OpenClipboard(HWND hWndNewOwner)
            5)清空剪貼板:BOOL EmptyClipboard()
            6)設(shè)置剪貼板數(shù)據(jù):HANDLE SetClipboardData(UINT uFormat,HANDLE hMem),uFormat值常見的有(CF_BITMAP、CF_TEXT、CF_OMETEXT)
            7)關(guān)閉剪貼板:BOOL CloseClipboard()
            2、示例程序
                  相關(guān)變量沒給出,自己看程序創(chuàng)建
              
            1)傳遞純文本
            //傳遞純文本
            void CSimpleTextTransferDlg::OnButton2() //按下Copy按鈕事件
            {
                
            // TODO: Add your control notification handler code here
                if(UpdateData())
                {
                    CString strData;
                    m_edtToClipboard.GetWindowText(strData);
                    
            if(OpenClipboard())
                    {
                        EmptyClipboard();
                        HGLOBAL hClipboardData;
                        hClipboardData
            =GlobalAlloc(GMEM_DDESHARE,strData.GetLength()+1);
                        
            char* pchData;
                        pchData
            =(char*)GlobalLock(hClipboardData);
                        strcpy(pchData,LPCSTR(strData));
                        GlobalUnlock(hClipboardData);
                        SetClipboardData(CF_TEXT,hClipboardData);
                        CloseClipboard();
                    }
                }        
            }

            void CSimpleTextTransferDlg::OnButton3() //按下cut按鈕事件
            {
                
            // TODO: Add your control notification handler code here
                OnButton2();
                m_edtToClipboard.SetWindowText(
            "");
            }

            void CSimpleTextTransferDlg::OnButton4()//按下Paste按鈕事件
            {
                
            // TODO: Add your control notification handler code here
                if (OpenClipboard())
                {
                    HANDLE hClipboardData
            =GetClipboardData(CF_TEXT);
                    
            char* pchData=(char*)GlobalLock(hClipboardData);
                    CString StrFromClipboard
            =pchData;
                    m_edtFromClipboard.SetWindowText(StrFromClipboard);
                    GlobalUnlock(hClipboardData);
                    CloseClipboard();
                }
            }
            2)轉(zhuǎn)移位圖圖像
            //按Alt+Print Screen組合鍵,可以把當前窗口的位圖圖像復(fù)制到剪貼板上
            void CSimpleTextTransferDlg::OnButton7()//按下Paste Bitmap按鈕事件,粘帖剪貼板中的位圖圖像
            {
                
            // TODO: Add your control notification handler code here
                if(OpenClipboard())
                {
                    
            if(::IsClipboardFormatAvailable(CF_BITMAP))
                    {
                        HBITMAP handle
            =(HBITMAP)GetClipboardData(CF_BITMAP);
                        CBitmap
            * bm=CBitmap::FromHandle(handle);
                        CClientDC cdc(
            this);
                        CDC dc;
                        dc.CreateCompatibleDC(
            &cdc);
                        dc.SelectObject(bm);
                        RECT rect;
                        m_grpBitmapFromClipboard.GetWindowRect(
            &rect);
                        ScreenToClient(
            &rect);
                        cdc.BitBlt(rect.left
            +10,rect.top+20,(rect.right-rect.left)-10,(rect.bottom-rect.top)-40,&dc,0,0,SRCCOPY);            
                    }
                    
            else
                    {
                        AfxMessageBox(
            "There is no BITMAP data on the Clipboard");
                    }    
                    CloseClipboard();
                }
            }

            void CSimpleTextTransferDlg::OnButton6()//按下Copy Test Bitmap按鈕事件,吧位圖圖像復(fù)制到剪貼板上 
            {
                
            // TODO: Add your control notification handler code here
                if(OpenClipboard())
                {
                    EmptyClipboard();
                    CBitmap
            * pNewBitmap=new CBitmap();
                    CClientDC cdc(
            this);
                    CDC dc;
                    dc.CreateCompatibleDC(
            &cdc);
                    CRect client(
            0,0,200,200);
                    pNewBitmap
            ->CreateCompatibleBitmap(&cdc,client.Width(),client.Height());
                    dc.SelectObject(pNewBitmap);
                    CString strBitmapText;
                    m_edtBitmapText.GetWindowText(strBitmapText);
                    DrawImage(
            &dc,strBitmapText);
                    SetClipboardData(CF_BITMAP,pNewBitmap
            ->m_hObject);
                    CloseClipboard();
                    delete pNewBitmap;
                }
                
            //MessageBox("The bitmap has been copied to clipboard.haha",NULL,MB_OK);
            }

            void CSimpleTextTransferDlg::DrawImage(CDC *pDC, CString pText)//進行GDI調(diào)用進行做圖
            {
                CRect rectDrawingArea(
            0,0,200,200);
                pDC
            ->FillSolidRect(rectDrawingArea,RGB(0,208,200));
                CPen pen;
                pen.CreatePen(PS_SOLID,
            3,RGB(0,0,255));
                CPen 
            * oldpen=pDC->SelectObject(&pen);
                pDC
            ->MoveTo(70,10);
                pDC
            ->LineTo(70,190);
                pDC
            ->MoveTo(130,10);
                pDC
            ->LineTo(130,190);
                pDC
            ->MoveTo(10,70);
                pDC
            ->LineTo(190,70);
                pDC
            ->MoveTo(10,130);
                pDC
            ->LineTo(190,130);
                pDC
            ->SelectObject(oldpen);
                pen.DeleteObject();

                pDC
            ->TextOut(10,10,pText);
                pDC
            ->TextOut(20,50,pText);
                pDC
            ->TextOut(50,100,pText);
            }
            3)轉(zhuǎn)移自定義數(shù)據(jù)
               在把自定義數(shù)據(jù)復(fù)制到剪貼板上之前,首先要把該數(shù)據(jù)注冊為Windows可以識別的格式。UINT RegisterClipboardFormat(LPCTSTR lpszFormat),lpszFormat是要被注冊的格式的名稱,該名稱可以用任何合法的字符串來表示。
               把自定義數(shù)據(jù)復(fù)制到剪貼板上首先,在頭文件中添加CCustomer類,接著為Copy Data按鈕添加按下事件。
            class CCustomer
            {
            public:
                
            int iCustomerNumber;
                
            float dCustomerBalance;
            };

            void CSimpleTextTransferDlg::OnButton5() 
            {
                
            // TODO: Add your control notification handler code here
                if(UpdateData())
                {
                    UINT uiCUstomerDataFormat
            =RegisterClipboardFormat("CUSTOMER_DATA");
                    
            if(OpenClipboard())
                    {
                        EmptyClipboard();
                        CCustomer customer;
                        customer.iCustomerNumber
            =m_iCustomerNumber;
                        customer.dCustomerBalance
            =m_dCustomerBalance;
                        HGLOBAL hClipboardData;
                        hClipboardData
            =GlobalAlloc(GMEM_DDESHARE,sizeof(CCustomer));
                        CCustomer 
            * pchCustomerData=(CCustomer*)GlobalLock(hClipboardData);
                        
            *pchCustomerData=customer;
                        GlobalUnlock(hClipboardData);
                        SetClipboardData(uiCUstomerDataFormat,hClipboardData);
                        CloseClipboard();
                    }
                }
            }
               從剪貼板上粘貼自定義數(shù)據(jù)
            void CSimpleTextTransferDlg::OnButton8() 
            {
                
            // TODO: Add your control notification handler code here
                UINT uiCUstomerDataFormat=RegisterClipboardFormat("CUSTOMER_DATA");
                
            if(OpenClipboard())
                {
                    
            if(::IsClipboardFormatAvailable(uiCUstomerDataFormat))
                    {
                        HANDLE hData
            =GetClipboardData(uiCUstomerDataFormat);
                        CCustomer 
            * pchCustomerData=(CCustomer*)GlobalLock(hData);
                        CCustomer customer
            =*pchCustomerData;
                        m_iCustomerNumber
            =customer.iCustomerNumber;
                        m_dCustomerBalance
            =customer.dCustomerBalance;
                        UpdateData(FALSE);
                        GlobalUnlock(hData);
                        CloseClipboard();
                    }
                }
            }
            4)接收剪貼板上內(nèi)容已被修改的通知消息
               添加DispalyClipboardData函數(shù)來顯示剪貼板中的內(nèi)容
            void CSimpleTextTransferDlg::DisplayClipboardData()
            {
                m_strClipboardText
            ="";
                
            if(OpenClipboard())
                {
                    
            if(::IsClipboardFormatAvailable(CF_TEXT)||::IsClipboardFormatAvailable(CF_OEMTEXT)||::IsClipboardFormatAvailable(CF_BITMAP))
                    {
                        m_strNotExt.ShowWindow(SW_HIDE);
                        HANDLE hClipboardData
            =GetClipboardData(CF_TEXT);
                        
            char *pchData=(char*)GlobalLock(hClipboardData);
                        m_strClipboardText
            =pchData;
                        GlobalUnlock(hClipboardData);
                    }
                    
            else
                    {
                        m_strNotExt.ShowWindow(SW_SHOW);
                    }
                    CloseClipboard();
                    UpdateData(FALSE);
                }
            }
               初始化兩個編輯欄的內(nèi)容
            CSimpleTextTransferDlg::CSimpleTextTransferDlg(CWnd* pParent /*=NULL*/)
                : CDialog(CSimpleTextTransferDlg::IDD, pParent)
                ,m_strLastUpdated(
            "<Unknown>")
                ,m_strClipboardText(
            "")
            {
                m_hIcon 
            = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
            }
               在OnInitDialog函數(shù)中添加DisplayClipboardData函數(shù)和SetClipboardViewer函數(shù)(每當剪貼板的內(nèi)容發(fā)生變化時,這個函數(shù)將窗口加入被通知的窗口鏈(通過WM_DRAWCLIPBOARD消息))
            BOOL CSimpleTextTransferDlg::OnInitDialog()
            {
                   

                DisplayClipboardData();
                SetClipboardViewer();
            //每當剪貼板的內(nèi)容發(fā)生變化時,這個函數(shù)將窗口加入被通知的窗口鏈(通過WM_DRAWCLIPBOARD消息)。
                
                
            return TRUE; 
            }
               手工添加WM_DRAWCLIPBOARD事件,相關(guān)函數(shù)為OnClipboardChange。
            afx_msg LRESULT OnClipboardChange(WPARAM,LPARAM);

            ON_MESSAGE(WM_DRAWCLIPBOARD,OnClipboardChange)

            LRESULT CSimpleTextTransferDlg::OnClipboardChange(WPARAM,LPARAM)
            {
                CTime time
            =CTime::GetCurrentTime();
                m_strLastUpdated
            =time.Format("%a, %b %d, %Y -- %H:%M:%S");
                DisplayClipboardData();
                
            return 0L;
            }
            posted on 2009-07-24 10:55 The_Moment 閱讀(5361) 評論(2)  編輯 收藏 引用 所屬分類: VC實踐

            FeedBack:
            # re: 使用Windows剪貼板API
            2014-04-23 22:10 | yyy
            class CCustomer
            {
            public:
            int iCustomerNumber;
            float dCustomerBalance;
            };
            如果CCustomer中包含指針對象,那怎么處理哪?  回復(fù)  更多評論
              
            # re: 使用Windows剪貼板API
            2016-01-17 12:45 | test
            贊一個 比看書好理解 還快多了  回復(fù)  更多評論
              
            亚洲午夜福利精品久久| 国产欧美一区二区久久| 999久久久免费国产精品播放| 四虎国产精品成人免费久久| 久久久精品久久久久久| 精品国产91久久久久久久a| 久久777国产线看观看精品| 久久精品国产亚洲AV麻豆网站| 久久人人爽人人爽人人爽| 国内精品伊人久久久久妇| 模特私拍国产精品久久| 99久久国产亚洲综合精品| 狠狠色丁香久久婷婷综合图片| 欧美亚洲国产精品久久久久| 亚洲综合伊人久久综合| 久久国产免费观看精品3| 国产精品视频久久| 国产三级观看久久| 久久夜色精品国产亚洲| 日产精品99久久久久久| 丁香狠狠色婷婷久久综合| 国产激情久久久久影院小草| 久久精品国产亚洲一区二区三区| 久久综合视频网站| 亚洲AV无码成人网站久久精品大| 国产精品99精品久久免费| 国产香蕉97碰碰久久人人| 久久亚洲精品国产精品婷婷 | 综合久久给合久久狠狠狠97色| 内射无码专区久久亚洲| 一本久久a久久精品亚洲| 99国内精品久久久久久久| 国产成人无码精品久久久性色| 久久香蕉国产线看观看99| 久久亚洲高清综合| 99久久99这里只有免费费精品| 7777久久久国产精品消防器材| 久久精品国产免费| 国产精品成人久久久| 亚洲国产精久久久久久久| 国产精品久久久久久久人人看|