xsharp.eu • GetFont() function
Page 1 of 1

GetFont() function

Posted: Sat May 14, 2022 4:15 pm
by Karl-Heinz
Hi Devteam,

is it allowed ;-) to use reflection to implement a x# runtime function ? I ask because i currently try to implement the VFP GetFont() function. Unfortunately, the Setoption() method of the .net FontDialog class is an internal method, but fortunately this method is accessible via reflection. I´m using this method to disable the script combobox and to enable the font description.

Here´s a snippet how my code currently looks like:

Code: Select all

oDlg := FontDialog{} 
	
	oMi := TypeOf(FontDialog):GetMethod("SetOption", BindingFlags.NonPublic | BindingFlags.Instance)  

	IF oMi != NULL 
		   
		// this enables the font description that is always shown in the left/bottom area of the VFP GetFont() dialog
		oMI:Invoke(oDlg, { CF_PRINTERFONTS, TRUE }) 
		
		// btw. Its possible to include fonts like Courier, FixedSys, Terminal
		// But since the .net FontDialog supports true type fonts only, trying to select e.g. FixedSys
		// would trigger the .net runtime error "only true type fonts are supported"!		       
		//
		// oMI:Invoke(oDlg, { 0x00040000L , FALSE  }) // CF_TTONLY 
		
    ENDIF 

 	oDlg:ShowEffects := FALSE 
	oDlg:ShowHelp := TRUE
	oDlg:MinSize := 4
	oDlg:MaxSize := 127

	
	lReturnCharSet := IIF ( IsNil ( nFontCharSet ) , FALSE , TRUE ) 
	
	IF oMi != NULL .and. ! lReturnCharSet 		
		// this disables the script combobox 
		oMI:Invoke(oDlg, { CF_NOSCRIPTSEL, TRUE })		
	ENDIF  
What do you think ?

regards
Karl-Heinz

GetFont() function

Posted: Sat May 14, 2022 5:44 pm
by robert
Karl Heinz,

Using reflection is perfectly fine, as long as you add a check to make sure that the method indeed exists (like you are doing in your code above)
In this case (of the FontDialog) it is very unfortunate that they have made the method internal. I see no reason to do that at all.
In theory you could subclass FontDialog and try to do it from there, but the options field is private (not protected) and the SetOptions is internal.

Robert




Robert

GetFont() function

Posted: Sat May 14, 2022 8:42 pm
by Chris
Guys,

Problem is though that AFAIK, admins can configure security settings in such way that such reflection "cheats" will cause runtime exceptions.
So if possible I think it's better to avoid this. But if there's absolutely no other way...

GetFont() function

Posted: Sun May 15, 2022 7:35 pm
by Karl-Heinz
Guys,

I think for now it would be useful if the Foxpro guys take a look at the current GetFont() implementation (see the attached viaef) and report if GetFont() works as expected.

regards
Karl-Heinz

GetFont() function

Posted: Wed May 18, 2022 3:15 pm
by Karl-Heinz
Hi Robert and Chris,

i´ve noticed that i posted the "GetFont() function" topic to the private Fox forum. Is it possible to move the thread to the public Fox forum ?

regards
Karl-Heinz

GetFont() function

Posted: Wed May 18, 2022 3:42 pm
by robert
Karl-Heinz,
Yes, I'll do that.
Btw: I did not include the function in the beta of 2.12.
Will do that for the final release of 2.12.

Robert