GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Nov 01, 2014 1:53 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: handling win32 window messages
PostPosted: Thu May 31, 2007 5:49 am 
Offline
Familiar Face

Joined: Sun Nov 05, 2006 8:56 am
Posts: 19
http://www.codeproject.com/system/HwDetect.asp

Is this possible with gtk? I've searched off and on for a few weeks now and haven't come up with anything.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 01, 2007 11:55 am 
Offline
Familiar Face

Joined: Sun Nov 05, 2006 8:56 am
Posts: 19
Well since nobody seems to know how to do this, when I get the free time i'm going to attempt to try RegisterDeviceNotification() API. The only other thing I can think of is trying to register a gtkwindow as a windows toplevel which its currently not doing (bug?).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 01, 2007 11:57 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
Most of the people here use Linux I believe, but someone should know the answer to your question. You might just have to wait longer than one day ...

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 02, 2007 1:42 am 
Offline
Familiar Face

Joined: Sun Nov 05, 2006 8:56 am
Posts: 19
openldev wrote:
Most of the people here use Linux I believe, but someone should know the answer to your question. You might just have to wait longer than one day ...

Yes I know... I could just use c# or something and use mono later if I really need to port it, but I like the idea of having my application cross-platform compatible. Plus just learning gtk takes the hassle out of learning windows crap, well until now...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 07, 2007 11:56 am 
Offline
Familiar Face

Joined: Sun Nov 05, 2006 8:56 am
Posts: 19
Incase anybody is interested in this code here it is:

Code:
// Takes Gtk window handle and overrides WindowProc to listen on Hardware change messages
// Currently works on MSVC++ 2005 with Gtkmm and Gtk+
#include <gtk/gtk.h>
#include <gtkmm/main.h>
#include <gtkmm/label.h>
#include <gtkmm/window.h>
#include <gdk/gdkwin32.h>
#include <windows.h>
#include <dbt.h>

#include <iostream>

// Global -- Original Window Procedure
WNDPROC OldWinProc = NULL;

int DeviceArrival = 0;
int DeviceRemoval = 0;


// =================================================================================
// PROTOTYPE FUNCTIONS =============================================================
// =================================================================================

// Returns a Win32 HWND from the Gtk::Window
// requires <gtk/gtk.h>, <gtkmm/window.h>, <windows.h>, and <gdk/gdkwin32.h> headers
HWND gtkmm_win32_get_handle(Gtk::Window *Window);

int override_window_procedure(HWND & hWnd);

// New Window Procedure
LRESULT CALLBACK NewWndProc(HWND hwnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam
);

// =================================================================================
// END PROTOTYPE FUNCTIONS =========================================================
// =================================================================================


int main(int argc, char *argv[])
{
   /*****************************************************************
   GtkWidget *window;
   gtk_init(&argc, &argv);

   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_window_set_title(GTK_WINDOW(window), "HWND TEST");
   gtk_widget_show_all(window);

   HWND hWnd = (HWND) gdk_win32_drawable_get_handle(window->window);
   if(!override_window_procedure(hWnd) )
   {
      std::cout << "override failed\n";
      std::cout << "GetLastError(): " << GetLastError() << "\n";
   }

   gtk_main();
   ******************************************************************/


   Gtk::Main kit(argc, argv);

   Gtk::Window *window = new Gtk::Window();

   window->set_title("Hello World");
   window->show_all();

   HWND hWnd = gtkmm_win32_get_handle(window);
   if(!override_window_procedure(hWnd) )
   {
      std::cout << "override failed\n";
      std::cout << "GetLastError(): " << GetLastError() << "\n";
      exit(-1);
   }

   Gtk::Main::run(*window);

   return 0;
}


// =================================================================================


LRESULT CALLBACK NewWndProc(HWND hwnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam
)
{
   if (uMsg == WM_DEVICECHANGE)
   {

      switch (wParam)
      {
      case DBT_DEVICEARRIVAL: std::cout << "Device New: " << ++DeviceArrival << "\n";
         break;
      case DBT_DEVICEREMOVECOMPLETE: std::cout << "Device Gone: " << ++DeviceRemoval << "\n";
         break;
      };

   }
   return CallWindowProc(OldWinProc,hwnd,uMsg,wParam,lParam);
}


inline HWND gtkmm_win32_get_handle(Gtk::Window * Window)
{
   // window must be visible to grab gobj()
   if (!Window->is_visible())
      Window->show_all();

   return static_cast<HWND>(gdk_win32_drawable_get_handle(Window->get_window()->gobj()) );
}


int override_window_procedure(HWND & hWnd)
{
    OldWinProc = (WNDPROC)GetWindowLong(hWnd,GWL_WNDPROC);
   if(!OldWinProc) return (int)OldWinProc;
    return SetWindowLong(hWnd,GWL_WNDPROC,(LONG)(WNDPROC)NewWndProc);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
   return main(__argc, __argv);
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group