tags:

views:

217

answers:

2

I'm writing some code that id like to be able to work with any window, such as a window created through the windows API, MFC, wxWidgets, etc.

The problem is that for some things I need to use the same thread that created the window, which in many cases is just sat in a message loop.

My first thought was to post a callback message to the window, which would then call a function in my code when it recieves the message using one of the params and a function pointer of some sorts. However there doesnt seem to be a standard windows message to do this, and I cant create my own message since I dont control the windows code, so cant add the needed code to the message handler to implement the callback...

Is there some other way to get the thread that created the window to enter my function?

EDIT: John Z sugessted that I hooked the windows messages. If I do that is there some way to get "ids" for custom messages without the risk of conflicting with any custom messages the window already has?

eg I might do

WM_CALLBACK = WM_APP+1

But if the window I'm hooking has already done something with WM_APP+1 I'm gonna run into problems.

EDIT2: just found RegisterWindowMessage :)

+2  A: 

If you are in the same process as the window you can hook its messages by subclassing it. Check out http://msdn.microsoft.com/en-us/library/ms633570(VS.85).aspx

The key API is SetWindowLong.

// Subclass the edit control. 
wpOrigEditProc = (WNDPROC) SetWindowLong(hwndEdit, GWL_WNDPROC, (LONG)EditSubclassProc); 

// Remove the subclass from the edit control. 
SetWindowLong(hwndEdit, GWL_WNDPROC, (LONG)wpOrigEditProc);
John Z
A: 

Alternatively to subclassing, you can use SetTimer to call a function in the window thread.

VOID CALLBACK Function(      
HWND hwnd,
UINT uMsg,
UINT_PTR idEvent,
DWORD dwTime
)
{
  // stuff
}

SetTimer(hWnd, event, 0, Function);
1800 INFORMATION