'--- Manage Grid Editing ---
'--- by SorCipollo AKA Quameyd ---
'--- It is possible to get total control about Edit object into a grid object ---
'--- With SubClassing ---
'--- Start Code ---
$APPTYPE GUI
$TYPECHECK ON
$ESCAPECHARS ON
'--------------------------
$INCLUDE "RapidQ2.inc"
'-------------------------
CONST null = "\0"     'Define a NULL STRING
CONST rqInpGridEdit = "TImpGridInplaceEdit\0" 'Name of Edit Class into Grid Objects
'-------------------------
DefByte Flag
DefLng hWndGridEdit 'Store GridEdit Handle
DefLng OldWindowProc 'Store old window proc address
'------------------------
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
     			 (ByVal parentHwnd As LONG, _
	  			  ByVal childHwnd As LONG, _
	  			  ByVal lpsz1 As String, _
	  			  ByVal lpsz2 As String)_
				  As LONG
'---------- Subclassing MyGrid ---------
Function MyNewProcedure(hWnd AS INTEGER, uMsg AS DWORD, wParam AS LONG, lParam AS LONG) As Long
    Select Case uMsg
		Case WM_KEYDOWN AND wParam = VK_LEFT
			 KillMessage (hWnd, WM_CHAR) 'Maybe this is not needed
		Case WM_KEYDOWN AND wParam = VK_RIGHT
			 KillMessage (hWnd, WM_CHAR) 'Maybe this is not needed
		Case WM_KEYDOWN AND wParam = VK_HOME
			 KillMessage (hWnd, WM_CHAR) 'Maybe this is not needed	 
		Case WM_KEYDOWN AND wParam = VK_BACK
			 KillMessage (hWnd, WM_CHAR) 'Maybe this is not needed
  	    Case Else
          ' Call Default WinProc
          MyNewProcedure = CallWindowProc(OldWindowProc, hWnd, uMsg, wParam, lParam)
    End Select
End Function
'--------------------------
DEFLNG AddressOfMyFunction = CodePtr(MyNewProcedure)
'--------------------------
DECLARE SUB CheckIfEditObjecIsCreated(Key AS BYTE, Sender AS QStringGrid)
'--------------------------		
CREATE MainForm AS QFormEx
		DelBorderIcons (biMaximize)
		BorderStyle = bsSingle	
		Height = 300
		Width = 430
		Caption = "Manage Grid Editing"
		Center
END CREATE

CREATE MainPanel AS QPanel
	   Parent = MainForm
	   Top = 20
	   Left = 10
	   Width = 400
	   Height = 200
	   BevelInner = bvRaised
	   BevelOuter = bvLowered
END CREATE

CREATE Label_1 AS QLabel
	   Parent = MainPanel
	   Top = 10
	   Left = 50
	   Caption = "When you are in editing mode into grid cells"
END CREATE

CREATE Label_2 AS QLabel
	   Parent = MainPanel
	   Top = 25
	   Left = 50
	   Caption = "you cannot use the left, right, home and BackSpace keys."
END CREATE	   

CREATE MyGrid AS QStringGrid
	   Parent = MainPanel
	   Top = 50
	   Left = 50
	   Height = 100
	   Width = 260
	   DefaultColWidth = 51
	   DefaultRowHeight = 19
	   GridLineWidth = 1
	   ColCount = 5
	   RowCount = 5
	   FixedCols = 0
	   FixedRows = 0
	   ScrollBars = ssNone
	   BorderStyle = bsNone
	   AddOptions (goEditing)
	   Color = &HFFFFFF
	   OnKeyPress = CheckIfEditObjecIsCreated
END CREATE
'-----------------------------------------------------					
MainForm.ShowModal
'------------------------------------------------------------------------------
SUB CheckIfEditObjecIsCreated(Key AS BYTE, Sender AS QStringGrid)	         	              
    IF Flag = 0 THEN 'IF flag = 1 we don't perform this action anymore
       DoEvents 'Allows system to create a EditObject into Grid
       hWndGridEdit = FindWindowEx(MyGrid.Handle, null, rqInpGridEdit, null)
       OldWindowProc = GetWindowLong(hWndGridEdit, GWL_WNDPROC) 
       SetWindowLong(hWndGridEdit, GWL_WNDPROC, AddressOfMyFunction)
       Flag = 1 
   END IF
END SUB
'------------------------------------------------------------------------------
'--- End Code ---
