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
#include "stdafx.h"
#include "MfcGtk.h"
#include "MfcGtkDlg.h"
#define WM_MINE (WM_USER+1)
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); 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::CMfcGtkDlg(CWnd* pParent )
: 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()
ON_BN_CLICKED(IDOK, &CMfcGtkDlg::OnBnClickedOk)
ON_BN_CLICKED(IDC_BUTTON1, &CMfcGtkDlg::OnBnClickedButton1)
END_MESSAGE_MAP()
BOOL CMfcGtkDlg::OnInitDialog()
{
CDialog::OnInitDialog();
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);
}
}
SetIcon(m_hIcon, TRUE); SetIcon(m_hIcon, FALSE); return TRUE; }
void CMfcGtkDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CMfcGtkDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
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;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR CMfcGtkDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CMfcGtkDlg::OnBnClickedOk()
{
OnOK();
}
void CMfcGtkDlg::OnBnClickedButton1()
{
HWND hWnd = (HWND)FindWindow(NULL, _T("Hello World!"));
if (hWnd != NULL){
printf("STOP Message\n");
::PostMessage (hWnd, WM_COMMAND,0, WM_MINE);
}
else{
}
}
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");
while( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0)
{
if (bRet == -1)
{
printf("ERROR\n");
}
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);