通过剪贴板的消息变化。 目的是想两个软件通过剪贴板的变化进行通信。
目前我试过可以通过消息对两个软件进行通信,但是弊端就是消息传递字符串参数始终不能成功。没有办法,只有改为剪贴板的消息,剪贴板是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);
}
//===========================================
// 效果图如下
//===========================================