进程通讯(自定义消息)

类别:编程语言 点击:0 评论:0 推荐:

还是比较懒点,这个从一个朋友给的delphi版本的改过来的,本来不准备贴的,无奈问的朋友多~~~~~~~:(

分两部分
接收和发送,测试过了

发送
.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>

#define WM_COMM WM_APP+100
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
        TButton *Button1;
        TButton *Button2;
        TButton *Button3;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall Button2Click(TObject *Sender);
        void __fastcall Button3Click(TObject *Sender);
private:
        bool ShowProcess(HWND Whandle); // User declarations
public:  // User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

.cpp
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"

#include "Windows.h"
#include "Messages.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
        HWND whd;
        whd = FindWindow(NULL,"Receiver");
        ShowProcess(whd);
        ::SendMessage(whd,WM_COMM,0,0);
}
//---------------------------------------------------------------------------
bool TForm1::ShowProcess(HWND Whandle)
{
        //TODO: Add your source code here
        bool rtn = false;
        try{
                ShowWindow(Whandle,SW_RESTORE);
                SetForegroundWindow(Whandle);
                SetActiveWindow(Whandle);

                rtn = true;
        }
        catch(...)
        {
        }
        return rtn;
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
        HWND whd;
        whd = FindWindow(NULL,"Receiver");
        ShowProcess(whd);
        ::SendMessage(whd,WM_COMM,0,1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
        HWND whd;
        whd = FindWindow(NULL,"Receiver");
        ShowProcess(whd);
        ::SendMessage(whd,WM_COMM,1,0);       
}
//---------------------------------------------------------------------------

接收的

.h
//---------------------------------------------------------------------------

#ifndef ReceiverH
#define ReceiverH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>

#define WM_COMM WM_APP+100
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
        TEdit *Edit1;
private:
        void userpro(TMessage &msg); // User declarations
public:  // User declarations
        __fastcall TForm1(TComponent* Owner);
protected:
        BEGIN_MESSAGE_MAP
                VCL_MESSAGE_HANDLER(WM_COMM, TMessage, userpro)
        END_MESSAGE_MAP(TForm)
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Receiver.h"

#include "Windows.h"
#include "Messages.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void TForm1::userpro(TMessage &msg)
{
        //TODO: Add your source code here
        if(msg.WParam == 0 &&msg.LParam == 0)
                Edit1->Text = "命令一";
        if(msg.WParam == 0 && msg.LParam == 1)
                Edit1->Text = "命令二";
        if(msg.WParam == 1 && msg.LParam == 0)
                Edit1->Text = "命令三";
}

本文地址:http://com.8s8s.com/it/it22727.htm