digitalmars.D.learn - Using delegate for WindowProc - possible ?
- Tal (14/14) Dec 28 2011 Can I do something like this :
- Juan Campanas (3/19) Dec 28 2011 Nope. You are trying to assign the address of a function to a
- bls (18/32) Dec 29 2011 ok next try, this works for me.
- Juan Campanas (5/21) Dec 29 2011 Wow! This is actually pretty cool! I needed something similar,
- bls (30/44) Dec 29 2011 Hi Tal.
Can I do something like this : ______________________________________________________________________ extern (Windows) LRESULT delegate (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) MyWinProcDelegate; this() { MyWinProcDelegate = &Events; } extern (Windows) LRESULT Events (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { MessageBoxA(null , "Success!!!" , null ,0); return DefWindowProcA(hWnd, message, wParam, lParam); } ______________________________________________________________________ The Events() doesn't seem to fire... am I missing something ?
Dec 28 2011
On Wednesday, 28 December 2011 at 23:45:05 UTC, Tal wrote:Can I do something like this : ______________________________________________________________________ extern (Windows) LRESULT delegate (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) MyWinProcDelegate; this() { MyWinProcDelegate = &Events; } extern (Windows) LRESULT Events (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { MessageBoxA(null , "Success!!!" , null ,0); return DefWindowProcA(hWnd, message, wParam, lParam); } ______________________________________________________________________ The Events() doesn't seem to fire... am I missing something ?Nope. You are trying to assign the address of a function to a delegate, both are differents.
Dec 28 2011
On 12/28/2011 03:45 PM, Tal wrote:Can I do something like this : ______________________________________________________________________ extern (Windows) LRESULT delegate (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) MyWinProcDelegate; this() { MyWinProcDelegate =&Events; } extern (Windows) LRESULT Events (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { MessageBoxA(null , "Success!!!" , null ,0); return DefWindowProcA(hWnd, message, wParam, lParam); } ______________________________________________________________________ The Events() doesn't seem to fire... am I missing something ?ok next try, this works for me. import std.stdio; import std.functional; int main(string[] argv) { extern(Windows) int delegate( int i) dg; alias dg callback; callback = toDelegate(&test); writeln( callback( 1 ) ); readln(); return 0; } extern(Windows) int test(int i) { return 41 +i;} hth, bjoern
Dec 29 2011
On Thursday, 29 December 2011 at 20:08:45 UTC, bls wrote:import std.stdio; import std.functional; int main(string[] argv) { extern(Windows) int delegate( int i) dg; alias dg callback; callback = toDelegate(&test); writeln( callback( 1 ) ); readln(); return 0; } extern(Windows) int test(int i) { return 41 +i;} hth, bjoernWow! This is actually pretty cool! I needed something similar, but it was to call a member function from the Windows procedure, and I had to use some thunking though assembly. Maybe I can adapt my code to use a delegate like this instead.
Dec 29 2011
On 12/28/2011 03:45 PM, Tal wrote:Can I do something like this : ______________________________________________________________________ extern (Windows) LRESULT delegate (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) MyWinProcDelegate; this() { MyWinProcDelegate =&Events; } extern (Windows) LRESULT Events (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { MessageBoxA(null , "Success!!!" , null ,0); return DefWindowProcA(hWnd, message, wParam, lParam); } ______________________________________________________________________ The Events() doesn't seem to fire... am I missing something ?Hi Tal. I have added an other example using array of delegates. I guess I know what you want to do... so do not miss the end of the document. Enjoy. import std.stdio; import std.functional; int main(string[] argv) { // array of delegates extern(Windows) int delegate( int i)[] dg; alias dg callback; // function to delegate callback ~= toDelegate( &test ); // std.functional writeln( callback[0]( 1 ) ); callback ~= toDelegate( &inc ); writeln( callback[1] (0) ); writeln (callback[0]( 1 ) ); readln(); return 0; } extern(Windows) int test(int i) { return 41 +i;} extern(Windows) int inc(int i) { return ++i;} ------------------------------------------------------------------------------------ // Now an associative array of delegates indexed by (of course) HWND ;) extern (Windows) LRESULT delegate (HWND hWnd, UINT message, WPARAM wParam,LPARAM lParam)[HWND] dg; alias dg MyWinProcDelegate; bjoern
Dec 29 2011