Best way to show a picture in an X# VOForm

This forum is meant for questions and discussions about the X# language and tools
User avatar
Kees Bouw
Posts: 119
Joined: Wed Nov 06, 2019 11:35 am
Location: Netherlands

Best way to show a picture in an X# VOForm

Post by Kees Bouw »

Hi Chris,

Thank you for taking the time to look at my issue! I have just sent you the solution.

Kees.
User avatar
Kees Bouw
Posts: 119
Joined: Wed Nov 06, 2019 11:35 am
Location: Netherlands

Best way to show a picture in an X# VOForm

Post by Kees Bouw »

Hi,

Chris Pyrgas helped me a lot to get a control, intended for Winforms or WPF to work on a VO form. The problem was that the control did not respond to some keys or key combinations. With his permission I would like to share the solution which worked very well. I added two methods to the class that inherits from the App class.

Code: Select all

METHOD BeforeDispatch(hWnd,uMsg,wParam,lParam)

IF uMsg == WM_KEYDOWN

    IF IsExplorerServer(hWnd)
        LOCAL netMsg AS System.Windows.Forms.Message
        netMsg := System.Windows.Forms.Message{}
        netMsg:HWnd := hWnd
        netMsg:Msg := uMsg
        netMsg:WParam := (IntPtr)((Int32)wParam)
        netMsg:LParam := (IntPtr)((Int32)lParam)
				
        LOCAL control AS System.Windows.Forms.Control
        // Find the .NET object corresponding to the handle
        // Could be a control, or could be the owning window
        // If there isn't one look up the ownership chain, 
        // will have to eventually hit an owning control or failing that, the owning window
        DO WHILE control == NULL
            control := System.Windows.Forms.Control.FromHandle(hWnd)
            hWnd := GetParent(hWnd)
        ENDDO
        IF control != NULL
            IF control:PreProcessMessage(REF netMsg)
                RETURN FALSE
            ENDIF
        ENDIF
    ENDIF

ENDIF

RETURN TRUE
You could also check for certain keys by using for example

IF uMsg == WM_KEYDOWN .AND. (wParam == VK_RETURN)

instead of checking just for WM_KEYDOWN.

The method IsExplorerServer checks for the correct control, in my case the class name was "Internet Explorer_Server" but this should be changed according to the control used.

Code: Select all

STATIC METHOD IsExplorerServer(hWnd AS PTR) AS LOGIC

STATIC LOCAL buffer := MemAlloc(30) AS PTR

IF GetClassName(hWnd, buffer, 30) == 24 // length of class name
    IF Psz2String(buffer) == "Internet Explorer_Server"
        RETURN TRUE
    ENDIF
ENDIF

RETURN FALSE
Thank you very much Chris! Maybe someone else will find it useful also.

Kees.
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

Best way to show a picture in an X# VOForm

Post by Karl-Heinz »

Hi Keese,

Are you aware of that the allocated "buffer" memory is never released ?

btw: why do you declare the var "buffer" as a STATIC LOCAL ?

Try this code with "lHandleStaticMemoryCorrectly" set to true/false and you see the difference.

Code: Select all

FUNCTION Start( ) AS VOID 

// -----------
MemTrace(TRUE)
// -----------  
		
MemTest()

RETURN

FUNCTION MemTest() AS VOID 
LOCAL lHandleStaticMemoryCorrectly AS LOGIC
LOCAL i AS DWORD 

 	lHandleStaticMemoryCorrectly := FALSE // TRUE 
	
	FOR i := 1 TO 4

		IF lHandleStaticMemoryCorrectly
			IsExplorerServer_New(NULL_PTR ) 
		ELSE 	
			IsExplorerServer(NULL_PTR ) 
		ENDIF 
		
		ListAllStaticMem()			

	NEXT
	?
      
	
FUNCTION IsExplorerServer(hWnd AS PTR ) AS LOGIC
STATIC LOCAL buffer := MemAlloc(30) AS PTR

IF GetClassName(hWnd, buffer, 30) == 24 // length of class name
    IF Psz2String(buffer) == "Internet Explorer_Server"
        RETURN TRUE
    ENDIF
ENDIF  

RETURN FALSE

FUNCTION IsExplorerServer_New(hWnd AS PTR ) AS LOGIC
LOCAL lOk AS LOGIC 
LOCAL buffer := MemAlloc(30) AS PTR

IF GetClassName(hWnd, buffer, 30) == 24 // length of class name
    IF Psz2String(buffer) == "Internet Explorer_Server"
        lOk := TRUE
    ENDIF
ENDIF

MemFree ( buffer )  // <----------- 

RETURN lOk 
           

FUNCTION ListAllStaticMem() AS VOID
    
	?
	? "Currently allocated static memory:"
	? "----------------------------------"
	
	MemWalk(StaticMemChecker) 

	RETURN 
	
FUNCTION StaticMemChecker( pMem AS IntPtr, nSize AS DWORD) AS LOGIC // callback 
	
	? pMem , nSize  
	
	RETURN TRUE		 

regards
Karl-Heinz
User avatar
Chris
Posts: 4845
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Best way to show a picture in an X# VOForm

Post by Chris »

Hi Karl-Heinz,

This was my code that I had sent to Kees, and did it this way just to keep it simple. I thought that not releasing 30 bytes for the duration of the app is not a big deal ;)

.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

Best way to show a picture in an X# VOForm

Post by Karl-Heinz »

Hi Chris,

ok, because you´re using at least a STATIC LOCAL instead of a LOCAL the memory leak is acceptable ;-)

regards
Karl-Heinz
Post Reply