Extended StringGadget Decimal-Value
Posted: Wed Nov 26, 2003 8:58 am
Hello there
For a few months i posted a request about an extended StringGadget :
;************
What's about adding some new features to the StringGadget ?
For example :
StringGadget(#Gadget, x, y, Width, Height, Content$ [, Length, Flags])
where Length = number of caracters allowed
and a new Flag : #PB_String_DecimalValue ("0123456789.," are accepted)
It would be really helpful not to have to control those things all the time."
;************
I since worked a while that way and i know it would be very difficult to implant such an extension. Thanks to Art Sentinel and his well-documented 'HookIncludeFile Tutorial' i wrote a small application that controls which characters to be send (or not) to a StringGadget.
In this example it accepts these characters : 0123456789 and , (feel free to change them) :
;============================
;StringGadget with KEY-FILTER
;Thanks to Art Sentinel for the Hook-Tutorial
;============================
Global hhook
Global CommaPoint.b : CommaPoint = 0
#StringID1 = 11
#StringID2 = 14
;************
;PROCEDURES
;************
Procedure KeyboardProc(nCode, wParam, lParam)
DefType.b Result
If wParam < 31
Result = 0
ElseIf wParam = 188 And CommaPoint = 0 ;","
Result = 0
ElseIf wParam = 48 ;0
Result = 0
ElseIf wParam = 49 ;1
Result = 0
ElseIf wParam = 50 ;2
Result = 0
ElseIf wParam = 51 ;3
Result = 0
ElseIf wParam = 52 ;4
Result = 0
ElseIf wParam = 53 ;5
Result = 0
ElseIf wParam = 54 ;6
Result = 0
ElseIf wParam = 55 ;7
result = 0
ElseIf wParam = 56 ;8
Result = 0
ElseIf wParam = 57 ;9
Result = 0
Else
Result = 1
EndIf
ProcedureReturn Result
EndProcedure
;***********
;***********
Procedure HookProc(Hooked)
Shared hhook
Shared CommaPoint
Select Hooked
Case 1
hInstance = GetModuleHandle_(0)
lpdwProcessId = GetWindowThreadProcessId_(WindowID, 0)
hhook = SetWindowsHookEx_(#WH_KEYBOARD, @KeyboardProc(), hInstance, lpdwProcessId)
Case 0
UnhookWindowsHookEx_(hhook)
EndSelect
EndProcedure
;************
;************
If OpenWindow(0, 150, 150, 300, 300, #PB_Window_SystemMenu, "Decimal-Filter (Thanks to Art Sentinel)")
;GadgetList
If CreateGadgetList(WindowID())
TextGadget(10, 50, 96, 180, 22, "No Filter :")
StringGadget(#StringID1, 50, 120, 180, 22, "") : SendMessage_(GadgetID(#StringID1), #EM_LIMITTEXT, 20, 0)
TextGadget(12, 50, 166, 180, 22, "Filter Decimal-Value :")
StringGadget(#StringID2, 50, 190, 180, 22, "") : SendMessage_(GadgetID(#StringID2), #EM_LIMITTEXT, 20, 0)
ActivateGadget(#StringID1)
;Main loop
Repeat
Select WaitWindowEvent()
Case #WM_KEYDOWN
If EventwParam() = 13
Select EventGadgetID()
Case #StringID1
text$ = GetGadgetText(#StringID2)
SendMessage_(GadgetID(#StringID2), #EM_SETSEL, 0, Len(text$))
ActivateGadget(#StringID2)
Case #StringID2
text$ = GetGadgetText(#StringID1)
SendMessage_(GadgetID(#StringID1), #EM_SETSEL, 0, Len(text$))
ActivateGadget(#StringID1)
EndSelect
EndIf
Case #PB_EventGadget
HookProc(0)
Select EventGadgetID()
Case #StringID2
Text$=GetGadgetText(#StringID2)
StLength = Len(Text$)
CommaPoint = 0
If StLength > 0
For counter = 1 To StLength
If Mid(Text$,counter,1) = ","
CommaPoint = CommaPoint + 1
EndIf
Next
EndIf
HookProc(1)
EndSelect
Case #WM_CLOSE
Quit = 1
EndSelect
Until Quit = 1
EndIf
EndIf
End
;============================
;May the force be with you !
For a few months i posted a request about an extended StringGadget :
;************
What's about adding some new features to the StringGadget ?
For example :
StringGadget(#Gadget, x, y, Width, Height, Content$ [, Length, Flags])
where Length = number of caracters allowed
and a new Flag : #PB_String_DecimalValue ("0123456789.," are accepted)
It would be really helpful not to have to control those things all the time."
;************
I since worked a while that way and i know it would be very difficult to implant such an extension. Thanks to Art Sentinel and his well-documented 'HookIncludeFile Tutorial' i wrote a small application that controls which characters to be send (or not) to a StringGadget.
In this example it accepts these characters : 0123456789 and , (feel free to change them) :
;============================
;StringGadget with KEY-FILTER
;Thanks to Art Sentinel for the Hook-Tutorial
;============================
Global hhook
Global CommaPoint.b : CommaPoint = 0
#StringID1 = 11
#StringID2 = 14
;************
;PROCEDURES
;************
Procedure KeyboardProc(nCode, wParam, lParam)
DefType.b Result
If wParam < 31
Result = 0
ElseIf wParam = 188 And CommaPoint = 0 ;","
Result = 0
ElseIf wParam = 48 ;0
Result = 0
ElseIf wParam = 49 ;1
Result = 0
ElseIf wParam = 50 ;2
Result = 0
ElseIf wParam = 51 ;3
Result = 0
ElseIf wParam = 52 ;4
Result = 0
ElseIf wParam = 53 ;5
Result = 0
ElseIf wParam = 54 ;6
Result = 0
ElseIf wParam = 55 ;7
result = 0
ElseIf wParam = 56 ;8
Result = 0
ElseIf wParam = 57 ;9
Result = 0
Else
Result = 1
EndIf
ProcedureReturn Result
EndProcedure
;***********
;***********
Procedure HookProc(Hooked)
Shared hhook
Shared CommaPoint
Select Hooked
Case 1
hInstance = GetModuleHandle_(0)
lpdwProcessId = GetWindowThreadProcessId_(WindowID, 0)
hhook = SetWindowsHookEx_(#WH_KEYBOARD, @KeyboardProc(), hInstance, lpdwProcessId)
Case 0
UnhookWindowsHookEx_(hhook)
EndSelect
EndProcedure
;************
;************
If OpenWindow(0, 150, 150, 300, 300, #PB_Window_SystemMenu, "Decimal-Filter (Thanks to Art Sentinel)")
;GadgetList
If CreateGadgetList(WindowID())
TextGadget(10, 50, 96, 180, 22, "No Filter :")
StringGadget(#StringID1, 50, 120, 180, 22, "") : SendMessage_(GadgetID(#StringID1), #EM_LIMITTEXT, 20, 0)
TextGadget(12, 50, 166, 180, 22, "Filter Decimal-Value :")
StringGadget(#StringID2, 50, 190, 180, 22, "") : SendMessage_(GadgetID(#StringID2), #EM_LIMITTEXT, 20, 0)
ActivateGadget(#StringID1)
;Main loop
Repeat
Select WaitWindowEvent()
Case #WM_KEYDOWN
If EventwParam() = 13
Select EventGadgetID()
Case #StringID1
text$ = GetGadgetText(#StringID2)
SendMessage_(GadgetID(#StringID2), #EM_SETSEL, 0, Len(text$))
ActivateGadget(#StringID2)
Case #StringID2
text$ = GetGadgetText(#StringID1)
SendMessage_(GadgetID(#StringID1), #EM_SETSEL, 0, Len(text$))
ActivateGadget(#StringID1)
EndSelect
EndIf
Case #PB_EventGadget
HookProc(0)
Select EventGadgetID()
Case #StringID2
Text$=GetGadgetText(#StringID2)
StLength = Len(Text$)
CommaPoint = 0
If StLength > 0
For counter = 1 To StLength
If Mid(Text$,counter,1) = ","
CommaPoint = CommaPoint + 1
EndIf
Next
EndIf
HookProc(1)
EndSelect
Case #WM_CLOSE
Quit = 1
EndSelect
Until Quit = 1
EndIf
EndIf
End
;============================
;May the force be with you !