Pixels and Foxels

Foxel is a Visual FoxPro term that corresponds to the maximum height and average width of a character in the current font. The row height corresponds to the maximum height of a letter in the current font; the column width corresponds to the average width of a letter in the current font.

Whenever VFP command or function has row or/and column as a parameter, it is expressed in foxels. It can have decimal fractions for precise positioning.

The Fox2pix() and Pix2fox() functions below provide conversion between pixels and foxels.

$SAMPLECODE$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

* Convert foxels to pixels
FUNCTION Fox2pix  
LPARAMETER tnFoxels, tlVertical, tcFontName, tnFontSize
* tnFoxels - foxels to convert
* tlVertical - .F./.T. convert horizontal/vertical coordinates
* tcFontName, tnFontSize - use specified font/size 
*       or current form (active output window) font/size, if not specified 

LOCAL lnPixels

IF PCOUNT() > 2
	lnPixels = tnFoxels * FONTMETRIC(IIF(tlVertical, 1, 6), tcFontName, tnFontSize)
ELSE
	lnpixels = tnFoxels * FONTMETRIC(IIF(tlVertical, 1, 6))
ENDIF	

RETURN lnPixels 
*------------------------------------

* Convert pixels to foxels 
FUNCTION Pix2fox
LPARAMETER tnPixels, tlVertical, tcFontName, tnFontSize
* tnPixels - pixels to convert
* tlVertical - .F./.T. convert horizontal/vertical coordinates
* tcFontName, tnFontSize - use specified font/size 
*         or current form (active output window) font/size, if not specified 
LOCAL lnFoxels

IF PCOUNT() > 2
	lnFoxels = tnPixels/FONTMETRIC(IIF(tlVertical, 1, 6), tcFontName, tnFontSize)
ELSE
	lnFoxels = tnPixels/FONTMETRIC(IIF(tlVertical, 1, 6))
ENDIF	

RETURN lnFoxels

Example 1 of using above functions
1
2
3
4
5
6

* Convert Windows Desktop size to foxels and back
? SYSMETRIC(1), Pix2fox(SYSMETRIC(1)), Fox2pix(Pix2fox(SYSMETRIC(1)))
? SYSMETRIC(2), Pix2fox(SYSMETRIC(2), .T.), Fox2pix(Pix2fox(SYSMETRIC(2), .T.), .T.)

Example 2 shows how to use Pix2fox() to correctly position a shortcut menu and WAIT window.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
PUBLIC oform1

oform1=NEWOBJECT("form1")
oform1.Show
RETURN

DEFINE CLASS form1 AS form

	Top = 0
	Left = 0
	Caption = "Example of using Pix2fox() UDF"
	Name = "Form1"

	ADD OBJECT command1 AS commandbutton WITH ;
		Top = 140, ;
		Left = 145, ;
		Height = 27, ;
		Width = 84, ;
		Caption = "Menu", ;
		Name = "Command1"

	ADD OBJECT command2 AS commandbutton WITH ;
		Top = 40, ;
		Left = 145, ;
		Height = 27, ;
		Width = 84, ;
		Caption = "Wait", ;
		Name = "Command2"

	PROCEDURE command1.Click
		LOCAL lnPosX, lnPosY

		* Position a shortcut menu at the bottom-right corner of the button
		lnPosX = Pix2fox(This.Left + This.Width, .F.)
		lnPosY = Pix2fox(This.Top + This.Height, .T.)

		DEFINE POPUP test SHORTCUT RELATIVE FROM (lnPosY), (lnPosX)
		DEFINE BAR 1 OF test PROMPT "Menu 1" 
		DEFINE BAR 2 OF test PROMPT "Menu 2" 
		DEFINE BAR 3 OF test PROMPT "Menu 3" 
		ACTIVATE POPUP test
	ENDPROC

	PROCEDURE command2.Click
		LOCAL lnPosX, lnPosY, lnRow, lnCol

		* Position a WAIT WINDOW menu at the bottom-right corner of the button
		lnPosY = _screen.Top + Thisform.Top + SYSMETRIC(4) + SYSMETRIC(9) + This.Top + This.Height
		lnPosX = Thisform.Left + SYSMETRIC(3) + SYSMETRIC(12) + This.Left + This.Width

		lnRow = pix2fox(lnPosY, .T.,_screen.Fontname ,_screen.FontSize)
		lnCol = pix2fox(lnPosX, .F.,_screen.Fontname ,_screen.FontSize)

		WAIT WINDOW "TEST" AT (lnRow), (lnCol)
	ENDPROC

ENDDEFINE



Comments

Sergey,

I have used your code for positioning a right-click menu on a toolbar, I believe. Now I search for it and can't seem to find it. I have the functions, but just not an example of how to use them on a toolbar. This would be a good addition to your sample code.