Page 1 of 1

get the pressed dialog id

Posted: Fri Nov 14, 2025 12:00 pm
by damianodec

Hi,
in this dialog from resource

Code: Select all | Expand


DEFINE DIALOG cMyDialog RESOURCE "NESCNC" PIXEL FONT oFont TRUEPIXEL

REDEFINE GET oget1 VAR mget1 ID 101 OF cMyDialog COLOR CLR_BLACK,CLR_GET ;
    PICTURE "@ !!!!!!!!!!!!!!!" VALID (gohere())
    
REDEFINE GET oget2 VAR mget2 ID 102 OF cMyDialog COLOR CLR_BLACK,CLR_GET ; PICTURE "@ !!!!!!!!!!!!!!!" VALID (gothis()) REDEFINE BTNBMP BtnEsc ID 103 OF cMyDialog RESOURCE "chiudi" 2007 ; ACTION (goaway(), cMyDialog :End())

How do I know which ID was pressed?


Re: get the pressed dialog id

Posted: Fri Nov 14, 2025 1:55 pm
by Jack

Hi,
You have :
oget1:bGetFocus(.....)
oget2:bGetFocus(.....)


Re: get the pressed dialog id

Posted: Fri Nov 14, 2025 3:34 pm
by damianodec

hi jack, Maybe my question was unclear.
When I open the dialog, the focus is on ID 101 (mget1).
If I press the BTNBMP BtnEsc ID 103 button in the dialog, the VALID clause of ID 101 is activated and the gohere() function is started.
In the gohere function, I want to know if the VALID clause was activated because the user exited (with the tab key) from the mget1 field or if they pressed the BTNBMP button of ID 103.


Re: get the pressed dialog id

Posted: Fri Nov 14, 2025 5:34 pm
by karinha

Good afternoon, do you have a small example for testing and better understanding?

Buenas tardes, ¿tienen algún ejemplo pequeño para probar y comprender mejor?

Algo asi? something like that?

https://fivetechsoft.com/forums/viewtop ... f=3&t=5115

https://www.fivetechsoft.com/forums/vie ... php?t=7051

Code: Select all | Expand

   if GetFocus() == ::hWnd
      ::lCaptured = .T.
      ::lPressed  = .T.
      ::Capture()
      ::Refresh() // .F.
   endif

Gracias, tks.

Regards, saludos.


Re: get the pressed dialog id

Posted: Fri Nov 14, 2025 6:20 pm
by Antonio Linares

El problema es que llamar a GetDlgCtrlID() sobre un HWND inválido provoca errores cuando el foco está fuera del diálogo; por eso conviene comprobar primero el resultado de GetFocus() antes de usarlo.

Code: Select all | Expand

LOCAL hFocus := GetFocus()
LOCAL nId := 0

IF hFocus != 0
    nId := GetDlgCtrlID( hFocus )
ENDIF

// nId contiene el ID del control que tiene el foco (0 si no hay foco en este hilo)
? "Control con foco ID:", nId

Si necesitas detectar si el foco pertenece específicamente a tu diálogo, compara hFocus con el HWND del diálogo o con los HWND de tus controles; otra opción es guardar el último HWND válido y usarlo cuando el diálogo reciba el foco de nuevo.

--- Explicación técnica

GetFocus() devuelve el handle de la ventana que tiene el foco en la cola de mensajes del hilo actual y puede devolver NULL si el foco está en otra aplicación o en otro hilo, por lo que hay que comprobar que no sea 0 antes de usarlo.

Si llamas a GetDlgCtrlID() con un HWND inválido la aplicación puede fallar; esto es un problema conocido cuando el usuario hace clic fuera de la aplicación y el foco pasa a otra ventana, y por eso muchos desarrolladores añaden la comprobación previa para evitar crashes.

En el ecosistema Harbour / FiveWin esta práctica es habitual: usar GetFocus() para saber qué control tiene el foco y luego actuar solo si el HWND es válido, o bien comparar con el HWND del diálogo para asegurarse de que el foco pertenece a la ventana esperada.

--- Puntos clave
  • Comprobar GetFocus() != 0 antes de usar su valor.
  • No llamar a GetDlgCtrlID() con un HWND inválido; GetDlgCtrlID() devuelve 0 si falla.
  • Comparar hFocus con el HWND del diálogo si necesitas limitar la detección al diálogo concreto.
  • Guardar el último HWND válido si quieres restaurar o recordar el control activo cuando el diálogo reciba el foco.

Re: get the pressed dialog id

Posted: Tue Nov 18, 2025 10:53 pm
by jmsilva

Hago así:

Code: Select all | Expand

oGet := GetObjectForId(cMyDialog,101)
....

Function GetObjectForId(oObj,nId)
   Local oCtrl
   For Each oCtrl in oObj:aControls
      If oCtrl:nId == nId
         Return(oCtrl)
      Endif
  Next
Return Nil