FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour get the pressed dialog id
Posts: 434
Joined: Wed Jun 06, 2007 02:58 PM
get the pressed dialog id
Posted: Fri Nov 14, 2025 12:00 PM

Hi,
in this dialog from resource

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?

FiveWin for xHarbour 24.02 - Feb. 2024 - Embarcadero C++ 7.60 for Win32 Copyright (c) 1993-2023

FWH 64 for Harbour 19.06 (MSVC++) Jun. 2019 - Harbour 3.2.0dev (r1904111533)

Visual Studio 2019 - Pelles C V.8.00.60 (Win64)
Posts: 300
Joined: Wed Jul 11, 2007 11:06 AM
Re: get the pressed dialog id
Posted: Fri Nov 14, 2025 01:55 PM

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

Posts: 434
Joined: Wed Jun 06, 2007 02:58 PM
Re: get the pressed dialog id
Posted: Fri Nov 14, 2025 03:34 PM

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.

FiveWin for xHarbour 24.02 - Feb. 2024 - Embarcadero C++ 7.60 for Win32 Copyright (c) 1993-2023

FWH 64 for Harbour 19.06 (MSVC++) Jun. 2019 - Harbour 3.2.0dev (r1904111533)

Visual Studio 2019 - Pelles C V.8.00.60 (Win64)
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: get the pressed dialog id
Posted: Fri Nov 14, 2025 05:34 PM

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/viewtopic.php?f=3&t=5115

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

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

Gracias, tks.

Regards, saludos.

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: get the pressed dialog id
Posted: Fri Nov 14, 2025 06:20 PM

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 (harbour): Select all Collapse
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.
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 9
Joined: Sun Oct 02, 2011 08:41 PM
Re: get the pressed dialog id
Posted: Tue Nov 18, 2025 10:53 PM

Hago así:

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

Continue the discussion