截获剪贴板消息

通过剪贴板的消息变化。 目的是想两个软件通过剪贴板的变化进行通信。
目前我试过可以通过消息对两个软件进行通信,但是弊端就是消息传递字符串参数始终不能成功。没有办法,只有改为剪贴板的消息,剪贴板是windows的全局消息,任何应用软件都可以获得其消息变化和内容。

示例代码如下:
h

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Clipbrd.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
    TLabel *lbl_1;
    TMemo *mmo_1;
    TLabel *lbl_2;
    void __fastcall FormCreate(TObject *Sender);
    void __fastcall FormDestroy(TObject *Sender);
private: // User declarations
public:  // User declarations
    __fastcall TForm1(TComponent* Owner);

    void __fastcall WndProc(TMessage &message);
    void __fastcall GetClipbrd(char *buff);

    HWND NextViewerHandle;
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

//===============================================================
//                                       海嵌测试程序
//===============================================================

CPP

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
//拦截消息
void __fastcall TForm1::WndProc(TMessage &message)
{
    if(message.Msg == WM_DRAWCLIPBOARD){            // 剪贴板变化
        SendMessage(NextViewerHandle,WM_DRAWCLIPBOARD,0,0);

        if(Clipboard()->HasFormat(CF_TEXT)){
            char buff[0xffff];
            Clipboard()->GetTextBuf(buff,0xffff);

            if(StrLComp(buff,"hanqian",7)==0){
                Clipboard()->Clear();
                GetClipbrd(buff);
            }
        }
    }
    else if(message.Msg == WM_CHANGECBCHAIN){       // 剪贴链有变化
        if(message.WParam == (int)NextViewerHandle)     // TODO: 察看链中窗口发生变动
            NextViewerHandle = (HWND)message.LParam;
        else if (NextViewerHandle != 0)
            SendMessage(NextViewerHandle,message.Msg,message.WParam,message.LParam);
                //传WM_CHANGECBCHAIN给下一个察看窗口
    }

    TForm::WndProc(message);
}
//--------------------------------------------------------------
void __fastcall TForm1::GetClipbrd(char *buff)
{
    String str = String(&buff[7]);
    lbl_1->Caption = str;
}
//--------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    Clipboard()->Clear();
    NextViewerHandle = SetClipboardViewer(Handle);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
    ChangeClipboardChain(Handle,NextViewerHandle);   
}

//===========================================
//                             效果图如下
//===========================================

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值