www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using delegate for WindowProc - possible ?

reply Tal <publictal gmail.com> writes:
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
next sibling parent "Juan Campanas" <johnny bells.com> writes:
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
prev sibling next sibling parent reply bls <bizprac orange.fr> writes:
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
parent "Juan Campanas" <johnny bells.com> writes:
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, bjoern

Wow! 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
prev sibling parent bls <bizprac orange.fr> writes:
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