how to catch mouse event in a grid ?

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

User avatar
mol
Posts: 3774
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: how to catch mouse event in a grid ?

Post by mol »

Hi!
I want to refresh this topic.
Maybe someone realized it..
I want to detect if left button of mouse is pressed for long time to open new menu (as on tablet/phone).
Any ideas? (Maybe I missed something...)
edk
Posts: 998
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: how to catch mouse event in a grid ?

Post by edk »

mol wrote: Thu Nov 03, 2022 1:10 pm Hi!
I want to refresh this topic.
Maybe someone realized it..
I want to detect if left button of mouse is pressed for long time to open new menu (as on tablet/phone).
Any ideas? (Maybe I missed something...)
Two solutions:
1. Requires releasing the left button or moving the mouse pointer 0.5 seconds after pressing the left mouse button.

Code: Select all

#include "hmg.ch"

Function Main
Local aRows [20] [3]

	DEFINE WINDOW Form_1 ;
		AT 0,0 ;
		WIDTH 800 ;
		HEIGHT 290 ;
		TITLE 'Hello World!' ;
		MAIN 

		aRows [1]	:= {'Simpson','Homer','555-5555'}
		aRows [2]	:= {'Mulder','Fox','324-6432'} 
		aRows [3]	:= {'Smart','Max','432-5892'} 
		aRows [4]	:= {'Grillo','Pepe','894-2332'} 
		aRows [5]	:= {'Kirk','James','346-9873'} 
		aRows [6]	:= {'Barriga','Carlos','394-9654'} 
		aRows [7]	:= {'Flanders','Ned','435-3211'} 
		aRows [8]	:= {'Smith','John','123-1234'} 
		aRows [9]	:= {'Pedemonti','Flavio','000-0000'} 
		aRows [10]	:= {'Gomez','Juan','583-4832'} 
		aRows [11]	:= {'Fernandez','Raul','321-4332'} 
		aRows [12]	:= {'Borges','Javier','326-9430'} 
		aRows [13]	:= {'Alvarez','Alberto','543-7898'} 
		aRows [14]	:= {'Gonzalez','Ambo','437-8473'} 
		aRows [15]	:= {'Batistuta','Gol','485-2843'} 
		aRows [16]	:= {'Vinazzi','Amigo','394-5983'} 
		aRows [17]	:= {'Pedemonti','Flavio','534-7984'} 
		aRows [18]	:= {'Samarbide','Armando','854-7873'} 
		aRows [19]	:= {'Pradon','Alejandra','???-????'} 
		aRows [20]	:= {'Reyes','Monica','432-5836'} 

		@ 10,10 GRID Grid_1 ;
			WIDTH 760 ;
			HEIGHT 240 ;
			HEADERS {'Last Name','First Name','Phone'} ;
			WIDTHS {140,140,140};
			ITEMS aRows ;
			VALUE {1,1} ;
			TOOLTIP 'Editable Grid Control' ;
			EDIT ;
	                JUSTIFY { GRID_JTFY_CENTER,GRID_JTFY_RIGHT, GRID_JTFY_RIGHT } ;
			CELLNAVIGATION 

	END WINDOW

	EventProcessMouseMessage( EventCreate( { || IF ( EventLongPressLButton(), MsgInfo ( "Call LongPress Procedure ..." ) , Nil ) }, Form_1.Grid_1.Handle ), .T.)

	CENTER WINDOW Form_1

	ACTIVATE WINDOW Form_1

Return

FUNCTION EventLongPressLButton()
	LOCAL  nHWnd          := EventHWND()
	LOCAL  nMsg           := EventMSG()
	LOCAL  nWParam        := EventWPARAM()
	LOCAL  nLParam        := EventLPARAM()
	LOCAL  nPressMillisec := 500

	STATIC xStartLButtonPressed := NIL

	DO CASE
		CASE nMsg == WM_LBUTTONDOWN
			IF Empty ( xStartLButtonPressed )
				xStartLButtonPressed := hb_MilliSeconds()
			ENDIF
			
		CASE nMsg == WM_LBUTTONUP .OR. nMsg == WM_MOUSEMOVE
			IF .NOT.Empty ( xStartLButtonPressed ) .AND. hb_MilliSeconds() - xStartLButtonPressed >= nPressMillisec
				xStartLButtonPressed := NIL
				Return .T.
			ENDIF
			IF nMsg == WM_LBUTTONUP
				xStartLButtonPressed := NIL
			ENDIF
	END CASE

RETURN .F.
2. It does not require releasing the button, but clicking on the grid can be troublesome, especially when you click on another column.

Code: Select all

#include "hmg.ch"

Function Main
Local aRows [20] [3]

	DEFINE WINDOW Form_1 ;
		AT 0,0 ;
		WIDTH 800 ;
		HEIGHT 290 ;
		TITLE 'Hello World!' ;
		MAIN 

		aRows [1]	:= {'Simpson','Homer','555-5555'}
		aRows [2]	:= {'Mulder','Fox','324-6432'} 
		aRows [3]	:= {'Smart','Max','432-5892'} 
		aRows [4]	:= {'Grillo','Pepe','894-2332'} 
		aRows [5]	:= {'Kirk','James','346-9873'} 
		aRows [6]	:= {'Barriga','Carlos','394-9654'} 
		aRows [7]	:= {'Flanders','Ned','435-3211'} 
		aRows [8]	:= {'Smith','John','123-1234'} 
		aRows [9]	:= {'Pedemonti','Flavio','000-0000'} 
		aRows [10]	:= {'Gomez','Juan','583-4832'} 
		aRows [11]	:= {'Fernandez','Raul','321-4332'} 
		aRows [12]	:= {'Borges','Javier','326-9430'} 
		aRows [13]	:= {'Alvarez','Alberto','543-7898'} 
		aRows [14]	:= {'Gonzalez','Ambo','437-8473'} 
		aRows [15]	:= {'Batistuta','Gol','485-2843'} 
		aRows [16]	:= {'Vinazzi','Amigo','394-5983'} 
		aRows [17]	:= {'Pedemonti','Flavio','534-7984'} 
		aRows [18]	:= {'Samarbide','Armando','854-7873'} 
		aRows [19]	:= {'Pradon','Alejandra','???-????'} 
		aRows [20]	:= {'Reyes','Monica','432-5836'} 

		@ 10,10 GRID Grid_1 ;
			WIDTH 760 ;
			HEIGHT 240 ;
			HEADERS {'Last Name','First Name','Phone'} ;
			WIDTHS {140,140,140};
			ITEMS aRows ;
			VALUE {1,1} ;
			TOOLTIP 'Editable Grid Control' ;
			EDIT ;
	                JUSTIFY { GRID_JTFY_CENTER,GRID_JTFY_RIGHT, GRID_JTFY_RIGHT } ;
			CELLNAVIGATION 

	END WINDOW

	EventProcessMouseMessage( EventCreate( { || IF ( EventLongPressLButton(), MsgInfo ( "Call LongPress Procedure ..." ) , Nil ) }, Form_1.Grid_1.Handle ), .T.)


	CENTER WINDOW Form_1

	ACTIVATE WINDOW Form_1

Return

FUNCTION EventLongPressLButton()
	LOCAL  nHWnd          := EventHWND()
	LOCAL  nMsg           := EventMSG()
	LOCAL  nWParam        := EventWPARAM()
	LOCAL  nLParam        := EventLPARAM()
	LOCAL  nPressMillisec := 500
	LOCAL  nStartLButtonPressed 
	
	IF nMsg == WM_LBUTTONDOWN
		nStartLButtonPressed := hb_MilliSeconds()
		DO WHILE EventMSG() == WM_LBUTTONDOWN .AND. hb_MilliSeconds() - nStartLButtonPressed < nPressMillisec
			DO EVENTS
		ENDDO
		IF hb_MilliSeconds() - nStartLButtonPressed >= nPressMillisec
			Return .T.
		ENDIF
	ENDIF
	
RETURN .F.
User avatar
mol
Posts: 3774
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: how to catch mouse event in a grid ?

Post by mol »

thank you very much, Edward!
I want to implement this in my small touch system without grid. It should be no problem
Post Reply