Saturday, September 23, 2006

A more reliable timer (unstoppable)

VFP Timer is known not so reliable. Under certain conditions, the VFP timer event can be pending/stopped. In Win32API There is a more reliable timer, SetTimer() API. However, since the older VFP version does not support the callback, many VFP'er go to C/C++ for the solution.

In VFP9, there is a way to use the SetTimer() API. BindEvent() can make this possible. Since our apps is under VFP control, there might a bit delay before the timer is fired. Anyway, again, thanks to enhanced of BindEvent() command :)

Actually, I had shown how to use SetTimer() in my OwnerDrawn Menu class. But you might not noticed that or maybe you don't get the idea about how it works. Here is the explanation.

Let's see the SetTimer declaration first:
Declare Long SetTimer in User32 ;
Long nhWnd, Long nEventId, Long uElapse, Long lpTimerFunc

As you see, the last parameter of SetTimer is pointer to a function (callback procedure). So how can you use this function. Very simple, pass in NULL pointer. Once you pass a NULL pointer, the OS will post a WM_TIMER message instead of calling the procedure. Now, simply bind the WM_TIMER into your top-level form. That would be all. For more info on SetTimer, take a look at MSDN (as usual)

This example will show you one condition that will pending/stopped the VFP timer, while SetTimer() will continue running.
? 'Using VFP Timer. Do you see the wait window?'
? 'Press ESC key or click on cancel button when ready...'
oVFPTimer = CreateObject( 'VFP_Timer' )
oVFPTimer = Null

? 'Using SetTimer() API. Watch for the wait window counting'
oWMTimer = CreateObject( 'TimerProc', _VFP.hWnd )
oWMTimer = Null


Define Class VFP_Timer as Timer
Interval = 500
Enabled = .T.
nCounter = 0

Procedure Timer
*** You won't see the wait window
Wait 'Using VFP Timer: ' + transform( This.nCounter ) window nowait
This.nCounter = This.nCounter + 1

Define Class TimerProc as Custom
#Define WM_TIMER 0x0113
#Define IDT_TIMER 1

nCounter = 0
hWnd = 0

Procedure Init( th_Wnd )
Declare Long SetTimer in User32 ;
Long nhWnd, Long nEventId, Long uElapse, Long lpTimerFunc

Declare Long KillTimer in User32 Long nhWnd, Long nEventId

** Bind the event first before SetTimer
BindEvent( th_Wnd, WM_TIMER, This, 'TimerProc' )
SetTimer( th_Wnd, IDT_TIMER, 500, 0 )
This.hWnd = th_Wnd

Procedure TimerProc( th_Wnd, tn_Msg, t_wParam, t_lParam )
Wait 'Using BindEvent (WM_TIMER): ' + transform( This.nCounter ) window nowait
This.nCounter = This.nCounter + 1
Return 0

Procedure Destroy
KillTimer( This.hWnd, IDT_TIMER )
UnBindEvents( This.hWnd )

Happy coding :)

Sunday, September 17, 2006

Detecting WinXP Active Theme

Many recent VFP applications is designed to be look more consistent with WinXP. One of the important factor is to know what is the active theme that the user is using.

There are two API Theme functions that comes in handy for this purpose, IsThemeActive() and GetCurrentThemeName().

Here is how to use them:
#Define MAX_WCHAR 512

Declare Long IsThemeActive in uxTheme
Declare Long GetCurrentThemeName in uxTheme ;
String @ O_pwszThemeFileName, Integer nMaxNameChars, ;
String @ O_pwszColorBuff, Integer nMaxColorChars, ;
String @ O_pwszSizeBuff, Integer nMaxSizeChars

Local lw_ThemeFileName, lw_ColorBuff, lw_SizeBuff
Local lc_ColorBuff, lc_SizeBuff, ll_Themed

If (IsThemeActive() == 1)
lw_ThemeFileName = space( MAX_WCHAR )
lw_ColorBuff = space( MAX_WCHAR )
lw_SizeBuff = space( MAX_WCHAR )
ll_Themed = (GetCurrentThemeName( @lw_ThemeFileName, MAX_WCHAR, @lw_ColorBuff, MAX_WCHAR, ;
@lw_SizeBuff, MAX_WCHAR ) == 0)

? 'Theme filename : '
If ll_Themed
?? MakeANSI( lw_ThemeFileName )
lc_ColorBuff = MakeANSI( lw_ColorBuff )

? 'Color scheme : '
Do case
Case (lc_ColorBuff == 'NormalColor')
?? 'Default'
Case (lc_ColorBuff == 'Metallic')
?? 'Silver'
?? 'Olive Green'
?? ' ( ' + lc_ColorBuff + ' )'

? 'Font size : '
lc_SizeBuff = MakeANSI( lw_SizeBuff )
Do case
Case (lc_SizeBuff == 'NormalSize')
?? 'Normal'
Case (lc_SizeBuff == 'LargeFonts')
?? 'Large Fonts'
?? 'Extra Large Fonts'
?? ' ( ' + lc_SizeBuff + ' )'
?? 'NONE ( Windows Classic )'
? 'Theme is not active ( Windows Classic )'
Clear DLLs

Procedure MakeANSI( tw_String )
Local lc_String, ln_Pos

lc_String = strconv( strconv( tw_String, 6 ), 2 )
ln_Pos = at( chr(0), lc_String )
If (ln_Pos > 0)
lc_String = left( lc_String, ln_Pos - 1 )

Return lc_String

To make your apps sensing the theme changes, you can bind the apps top-level form to WM_THEMECHANGED. Then use the code above (in your procedure handler) to detect the new theme.

Happy coding!

Saturday, September 02, 2006

Updated OwnerDrawn menu class

Today, the OwnerDrawn menu class has been updated (the article was published on UT Magazine October 2005 edition). A few bug has been fixed. And few features has been added based on e-mail I received lately.

I hope you enjoy the new class,
happy coding!

OwnerDrawn Menus class & sample