ynanay 发表于 2015-6-16 22:31:43

(自定义消息)

还是比较懒点,这个从一个朋友给的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);      
}
//---------------------------------------------------------------------------

接收的
页: [1]
查看完整版本: (自定义消息)