从MFC应用程序向GTK应用程序发送消息

问题描述:

亲爱的所有人,

我有两个单独的应用程序,一个在MFC中,另一个在GTK中.我需要向GTK发送一些事件(消息)(在GTK处理MFC事件).

我已经使用CygWin来构建GTK应用程序.请在下面的代码中找到.
我无法在网上获得更多信息.
请让我知道如何实现这一目标.因为我是GTK的新手,所以示例会更有帮助.

等待回复.

谢谢,
迪帕克

MFC应用程序

Dear All,

I have two separate applications, one in MFC and other in GTK. I need to send some events (messages) to GTK (handling MFC event at GTK).

I have used CygWin to build the GTK application. Please find below the code.
I am not able to get more info on this on the net.
Please let me know how to achieve this. Examples would be more helpful as I am new to GTK.

Awaiting reply.

Thanks,
Dipak

MFC Application

// MfcGtkDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MfcGtk.h"
#include "MfcGtkDlg.h"
#define WM_MINE (WM_USER+1)
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
// Dialog Data
	enum { IDD = IDD_ABOUTBOX };
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()

// CMfcGtkDlg dialog


CMfcGtkDlg::CMfcGtkDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMfcGtkDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMfcGtkDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CMfcGtkDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
    ON_BN_CLICKED(IDOK, &CMfcGtkDlg::OnBnClickedOk)
    ON_BN_CLICKED(IDC_BUTTON1, &CMfcGtkDlg::OnBnClickedButton1)
END_MESSAGE_MAP()

// CMfcGtkDlg message handlers
BOOL CMfcGtkDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	// Add "About..." menu item to system menu.
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	// TODO: Add extra initialization here
	return TRUE;  // return TRUE  unless you set the focus to a control
}
void CMfcGtkDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}
// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.
void CMfcGtkDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}
// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMfcGtkDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CMfcGtkDlg::OnBnClickedOk()
{
    // TODO: Add your control notification handler code here
    OnOK();
}
void CMfcGtkDlg::OnBnClickedButton1()
{
    HWND hWnd = (HWND)FindWindow(NULL, _T("Hello World!")); 
    if (hWnd != NULL){    
        //GetWindowThreadProcessId(hWnd, &dwProcessId); 
        //if (dwProcessId == proc_info.dwProcessId) { 
        printf("STOP Message\n"); 
        ::PostMessage (hWnd, WM_COMMAND,0, WM_MINE); 
        //} 
    } 
    else{ 
        // printf("ERROR\n"); 
    } 
    // TODO: Add your control notification handler code here
}


GTK应用程序


GTK Application

#include <gtk/gtk.h>
#include <windows.h>

#define WM_MINE (WM_USER+1)
static void destroy( GtkWidget* widget, gpointer gp)
{
	printf("dastroy called !");
        gtk_main_quit();
}
static gboolean delete_event( GtkWidget* widget, GdkEvent* event , gpointer gp)
{
	printf("delete event called !");
        return FALSE;
}

static gboolean check_msg(gpointer data)
{
	printf("Inside check_msg\n");
    int bRet;
    MSG msg;
    HWND hWnd;
    hWnd = FindWindow(NULL, "MfcGtk");
   if((NULL != hWnd)  && (-1 != (int)hWnd))
    	printf("MFC Window found\n");
    /*if(GetMessage( &msg, hWnd, 0, 0 ) != 0)
    {
		printf("Inside Message\n");
		switch(msg.message)
		{
			case WM_MINE :
				printf("START\n");
			default:
			    break;
		}
	}*/
    while( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0)
    {
        if (bRet == -1)
        {
            printf("ERROR\n");
            // handle the error and possibly exit
        }
        else
        {
            switch(msg.message)
			{
			case WM_MINE :
				printf("START\n");
			default:
			    break;
			}
        }
    }
    return( TRUE );
}

int main(int argc, char *argv[])
{

	MSG msg;
	int bRet = 0;
	GtkWidget *window, *lavel;
    gtk_init(&argc, &argv);
    window = gtk_window_new( GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Hello World!");
    gtk_container_set_border_width( GTK_CONTAINER (window), 10);
    gtk_widget_set_size_request( window, 200, 100);
    g_signal_connect( G_OBJECT (window), "destroy", G_CALLBACK(destroy), NULL );
    g_signal_connect( G_OBJECT(window), "delete_event", G_CALLBACK(delete_event), NULL);
    ///<Add selectable level

    lavel = gtk_label_new("Hello World!");

    gtk_label_set_selectable(GTK_LABEL(lavel), TRUE);

//    gtk_label_set_markup(GTK_LABEL(lavel), "<span style='color:italic'></span>");
    ///<Add lavel as child widget of the window

    gtk_container_add( GTK_CONTAINER(window), lavel);

	gtk_widget_show_all(window);

	g_timeout_add( 200, check_msg, NULL );

	gtk_main();

	return 0;

}

看看 ACE [
如果采用这种方法的好处是,虽然实施起来相对简单,但您的MFC和GTK应用程序不必驻留在同一台机器或OS上-当您要在GTK和MFC之间进行通信时,通常会遇到这种情况基于应用程序.

最好的问候
Espen Harlinn
Take a look at ACE[^]
There is an elegant solution available under "ACE_wrappers\examples\C++NPv1" - adapt it to suit your needs.

The beauty if this approach is that while relatively simple to implment, your MFC and GTK applications doesn''t have to reside on the same machine, or OS - something that''s often the case when you want to communicate between GTK and MFC based apps.

Best Regards
Espen Harlinn


看我的文章 http://www.codeproject.com/KB/cross-platform/WinMsg.aspx?display=打印>将Windows消息传递转换为GTK [ ]

它向您展示了如何在线程和GTK应用程序之间发送消息.

要使用两个应用程序,需要在两个应用程序中创建一个套接字,然后才能在两个应用程序之间发送数据.您需要在两个应用程序中都指定端口号.
Look at my article http://www.codeproject.com/KB/cross-platform/WinMsg.aspx?display=Print">Translating Windows Messaging to GTK[]

It show you how to send messages between a thread and a GTK appliction.

For your use, using two applications, you need to create a socket in both applications, then you can send data between the apps. You will need to specify the port number in both apps.