Extended StringGadget Decimal-Value
Extended StringGadget Decimal-Value
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 !
Re: Extended StringGadget Decimal-Value
> In this example it accepts these characters : 0123456789 and ,
This example isn't foolproof -- you can still right-click the StringGadget
and paste non-digits into it. The best approach is to check when its
contents have been changed, then strip out any non-digits and store
the updated "fixed" value back into it.
			
			
									
									
						This example isn't foolproof -- you can still right-click the StringGadget
and paste non-digits into it. The best approach is to check when its
contents have been changed, then strip out any non-digits and store
the updated "fixed" value back into it.
What's about this : 
(no more Copy-Paste allowed inside the StringGadget)
;============================
;StringGadget with KEY-FILTER
;Thanks to Art Sentinel for the Hook-Tutorial
;============================
Global hhook
Global hookValue.b : hookValue = 0
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 = 8 ;Del
Result = 0
ElseIf wParam = 37 ;ArrowLeft
Result = 0
ElseIf wParam = 38 ;ArrowUp
Result = 0
ElseIf wParam = 39 ;ArrowRight
Result = 0
ElseIf wParam = 40 ;ArrowDown
Result = 0
ElseIf wParam = 46 ;Entf
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
;************
;************
WindowID = OpenWindow(0, 150, 150, 300, 300, #PB_Window_SystemMenu, "Decimal-Filter (Thanks to Art Sentinel)")
If WindowID <> 0
;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
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
If hookValue = 0 : hookValue = 1 : HookProc(1) : EndIf
Default
If hookValue = 1 : hookValue = 0 : HookProc(0) : EndIf
EndSelect
Case #WM_CLOSE
Quit = 1
EndSelect
Until Quit = 1
EndIf
EndIf
End
;============================
			
			
									
									
						(no more Copy-Paste allowed inside the StringGadget)
;============================
;StringGadget with KEY-FILTER
;Thanks to Art Sentinel for the Hook-Tutorial
;============================
Global hhook
Global hookValue.b : hookValue = 0
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 = 8 ;Del
Result = 0
ElseIf wParam = 37 ;ArrowLeft
Result = 0
ElseIf wParam = 38 ;ArrowUp
Result = 0
ElseIf wParam = 39 ;ArrowRight
Result = 0
ElseIf wParam = 40 ;ArrowDown
Result = 0
ElseIf wParam = 46 ;Entf
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
;************
;************
WindowID = OpenWindow(0, 150, 150, 300, 300, #PB_Window_SystemMenu, "Decimal-Filter (Thanks to Art Sentinel)")
If WindowID <> 0
;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
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
If hookValue = 0 : hookValue = 1 : HookProc(1) : EndIf
Default
If hookValue = 1 : hookValue = 0 : HookProc(0) : EndIf
EndSelect
Case #WM_CLOSE
Quit = 1
EndSelect
Until Quit = 1
EndIf
EndIf
End
;============================
Hi,
Its works under WIN XP
No Works under WIN VISTA
Who can Help ?
Donald
..
			
			
									
									Its works under WIN XP
No Works under WIN VISTA
Who can Help ?
Donald
..
DONALD 
  http://www.PureBasic-Donald.de
PureBasic - jaPBe - PureFORM - PureVisonXP - TailBite
GFABasic 16Bit 4.38 - GB32 2.30 B1160 - GFABasic DOS 4.51
						PureBasic - jaPBe - PureFORM - PureVisonXP - TailBite
GFABasic 16Bit 4.38 - GB32 2.30 B1160 - GFABasic DOS 4.51
- netmaestro
 - PureBasic Bullfrog

 - Posts: 8452
 - Joined: Wed Jul 06, 2005 5:42 am
 - Location: Fort Nelson, BC, Canada
 
You can try this, I just modded it quickly so no guarantees:
			
			
									
									Code: Select all
;============================ 
;StringGadget with KEY-FILTER 
;Thanks to Art Sentinel for the Hook-Tutorial 
;============================ 
Global hhook 
Global hookValue.b : hookValue = 0 
Global CommaPoint.b : CommaPoint = 0 
#StringID1 = 11 
#StringID2 = 14 
;************ 
;PROCEDURES 
;************ 
Procedure KeyboardProc(nCode, wParam, lParam) 
  result.b = 1
  Select wParam
    Case 0 To 30, 37 To 40, 46, 48 To 57 : result=0
    Case 188 : If CommaPoint = 0: result = 0 : EndIf
  EndSelect
  
  ProcedureReturn Result 
EndProcedure 
;*********** 
;*********** 
Procedure HookProc(Hooked) 
  Shared hhook 
  Shared CommaPoint 
  Select Hooked 
    Case 1 
      hhook = SetWindowsHookEx_(#WH_KEYBOARD, @KeyboardProc(), 0, GetCurrentThreadId_()) 
    Case 0 
      UnhookWindowsHookEx_(hhook) 
  EndSelect 
EndProcedure 
;************ 
;************ 
WindowID = OpenWindow(0, 150, 150, 300, 300, "Decimal-Filter (Thanks to Art Sentinel)",#PB_Window_SystemMenu) 
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) 
setActiveGadget(#StringID1) 
;Main loop 
Repeat 
  Select WaitWindowEvent() 
    Case #WM_KEYDOWN 
      If EventwParam() = 13 
        Select EventGadget() 
          Case #StringID1 
            text$ = GetGadgetText(#StringID2) 
            SendMessage_(GadgetID(#StringID2), #EM_SETSEL, 0, Len(text$)) 
            setactiveGadget(#StringID2) 
          Case #StringID2 
            text$ = GetGadgetText(#StringID1) 
            SendMessage_(GadgetID(#StringID1), #EM_SETSEL, 0, Len(text$)) 
            setactiveGadget(#StringID1) 
        EndSelect 
      EndIf 
    Case #PB_Event_Gadget 
      Select EventGadget() 
        Case #StringID2 
          Select EventType()
            Case #PB_EventType_Focus 
              HookProc(1) 
            Case #PB_EventType_LostFocus 
              HookProc(0)
            Default
              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 
        EndSelect
      EndSelect 
    Case #PB_Event_CloseWindow
      Quit = 1 
  EndSelect 
Until Quit = 1 
End 
;============================BERESHEIT
						Now we have this in purebasic:
So no need for using this old code...  
			
			
									
									
						Code: Select all
If OpenWindow(0, 0, 0, 322, 105, "StringGadget Flags", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(0, 8,  10, 306, 20, "Normal StringGadget...")
  StringGadget(1, 8,  35, 306, 20, "1234567", #PB_String_Numeric)
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf- netmaestro
 - PureBasic Bullfrog

 - Posts: 8452
 - Joined: Wed Jul 06, 2005 5:42 am
 - Location: Fort Nelson, BC, Canada
 
Hello,
thanks, its works.
Donald
..
old
new
			
			
									
									thanks, its works.
Donald
..
old
Code: Select all
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
EndProcedureCode: Select all
Procedure HookProc(Hooked) 
  Shared hhook 
  Shared CommaPoint 
  Select Hooked 
    Case 1 
      hhook = SetWindowsHookEx_(#WH_KEYBOARD, @KeyboardProc(), 0, GetCurrentThreadId_()) 
    Case 0 
      UnhookWindowsHookEx_(hhook) 
  EndSelect 
EndProcedure 
DONALD 
  http://www.PureBasic-Donald.de
PureBasic - jaPBe - PureFORM - PureVisonXP - TailBite
GFABasic 16Bit 4.38 - GB32 2.30 B1160 - GFABasic DOS 4.51
						PureBasic - jaPBe - PureFORM - PureVisonXP - TailBite
GFABasic 16Bit 4.38 - GB32 2.30 B1160 - GFABasic DOS 4.51
- netmaestro
 - PureBasic Bullfrog

 - Posts: 8452
 - Joined: Wed Jul 06, 2005 5:42 am
 - Location: Fort Nelson, BC, Canada
 
No not picky really - I just didn't really understand why the decimal point was being ignored and why the virtual key code generated is one of the OEM specific ones?  I see it now that I take a closer look at the code!  
 
Guess I am wondering why a hook is being used rather than simply subclassing the edit control? Oh well!
			
			
									
									Guess I am wondering why a hook is being used rather than simply subclassing the edit control? Oh well!
I may look like a mule, but I'm not a complete ass.
						

