• <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(默認(rèn)值,可以用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組合鍵,可以把當(dāng)前窗口的位圖圖像復(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ù)(每當(dāng)剪貼板的內(nèi)容發(fā)生變化時,這個函數(shù)將窗口加入被通知的窗口鏈(通過WM_DRAWCLIPBOARD消息))
            BOOL CSimpleTextTransferDlg::OnInitDialog()
            {
                   

                DisplayClipboardData();
                SetClipboardViewer();
            //每當(dāng)剪貼板的內(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ù)  更多評論
              
            国产亚洲成人久久| 亚洲综合久久夜AV | 国内精品久久久久久久影视麻豆| 免费观看成人久久网免费观看| 久久亚洲精品无码aⅴ大香| 国内精品久久久久久久涩爱| 欧美亚洲国产精品久久高清| 国产精品对白刺激久久久| 日韩久久久久久中文人妻| 中文精品99久久国产| 国产精品免费福利久久| 性做久久久久久免费观看 | 日韩av无码久久精品免费| 2020久久精品国产免费| 一本色综合网久久| 久久天天婷婷五月俺也去| 亚洲午夜福利精品久久| 久久亚洲精品成人AV| 最新久久免费视频| 国产日韩久久久精品影院首页| 国产精品免费久久| 久久国产精品77777| 青青青伊人色综合久久| 亚洲综合久久夜AV | 国产精品99久久久久久董美香| 久久午夜无码鲁丝片| 久久婷婷五月综合国产尤物app| 一本久久a久久精品vr综合| 久久久久女教师免费一区| 看全色黄大色大片免费久久久 | 亚洲AⅤ优女AV综合久久久| 久久香蕉综合色一综合色88| 久久久免费精品re6| 国内精品综合久久久40p| 午夜精品久久久久久| 久久久久亚洲AV成人网人人网站| 91久久婷婷国产综合精品青草 | 久久精品久久久久观看99水蜜桃| 中文字幕无码久久久| 中文字幕精品久久久久人妻| 合区精品久久久中文字幕一区|