Announcement

Collapse
No announcement yet.

Control get state

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Control get state

    Is it possible to check the state of a control (ie. whether CONTROL ENABLE or CONTROL DISABLE was last used) in a dialog?
    Bernard Ertl
    InterPlan Systems

  • #2
    You should be able to use GetWindowLong() to return the style, then check whether %WS_Disabled is in the style ... something like this ...
    Code:
    'Compilable Example:
    #Compile Exe
    #Include "Win32API.inc"
    
    %IDC_Button = 500
    Global hDlg As Dword
    Function PBMain() As Long
       Dialog New Pixels, 0, "PowerBASIC",300,300,200,200, %WS_OverlappedWindow To hDlg
       Control Add Button, hDlg, %IDC_Button,"Push", 50,10,100,20
       Dialog Show Modal hDlg Call DlgProc
    End Function
    
    CallBack Function DlgProc() As Long
       Select Case Cb.Msg
          Case %WM_InitDialog
             Control Disable hDlg, %IDC_Button
             If (GetWindowLong(GetDlgItem(hDlg,%IDC_Button),%GWL_Style) And %WS_Disabled) = %WS_Disabled Then ? "Disabled"
       End Select
    End Function
    Last edited by Gary Beene; 21 Sep 2015, 02:36 PM. Reason: code fix

    Comment


    • #3
      The IsWindowEnabled() Windows API function may be used to get the current enabled state of any window.

      MCM

      Comment


      • #4
        Bern.
        I often leave a window to return to it later and wish to return to the same control that had the focus when I left. If this is what you're trying to accomplish, here's what I use to do this in the callback routine:

        Code:
        STATIC hWndSaveFocus AS DWORD
        
          '-----------------------------------
          ' set or save control focus
          '-----------------------------------
            CASE %WM_NCACTIVATE
              IF ISFALSE CB.WPARAM THEN
                ' Save control focus
                hWndSaveFocus = GetFocus()
              ELSEIF hWndSaveFocus THEN
                ' Restore control focus
                DIALOG POST CB.HNDL, %WM_NEXTDLGCTL, hWndSaveFocus, %TRUE
                hWndSaveFocus = 0
              END IF
        It's impossible to make anything foolproof because fools are so ingenious.

        Comment


        • #5
          With MCM's suggestion ...
          Code:
          'Compilable Example:
          #Compile Exe
          #Include "Win32API.inc"
          
          %IDC_Button = 500
          Global hDlg As Dword
          Function PBMain() As Long
             Dialog New Pixels, 0, "PowerBASIC",300,300,200,200, %WS_OverlappedWindow To hDlg
             Control Add Button, hDlg, %IDC_Button,"Push", 50,10,100,20
             Dialog Show Modal hDlg Call DlgProc
          End Function
          
          CallBack Function DlgProc() As Long
             Select Case Cb.Msg
                Case %WM_InitDialog
                   Control Disable hDlg, %IDC_Button
                   If IsWindowEnabled(GetDlgItem(hDlg,%IDC_Button)) Then ? "Enabled"
             End Select
          End Function

          Comment


          • #6
            Code:
            #COMPILE EXE
            #DIM ALL
            #INCLUDE ONCE "WIN32API.INC"
            
            '----------------------------------------------------------------------------(')
            
            ENUM dlgMain
                btnEnabled = 1000
                btnDisabled
            END ENUM
            
            '----------------------------------------------------------------------------(')
            
            CALLBACK FUNCTION cb_dlgMain( )
                SELECT CASE AS LONG CB.MSG
                    CASE %WM_INITDIALOG
                        CONTROL DISABLE CB.HNDL, %dlgMain.btnDisabled
                        DIALOG POST CB.HNDL, %WM_USER + 1, 0, 0
                    CASE %WM_USER + 1
                        IF ISWINDOWENABLED( GETDLGITEM( CB.HNDL, %dlgMain.btnEnabled )) THEN
                            MSGBOX "Enabled Button is Enabled"
                        ELSE
                            MSGBOX "Enabled Button is Disabled"
                        END IF
                        IF ISWINDOWENABLED( GETDLGITEM( CB.HNDL, %dlgMain.btnDisabled )) THEN
                            MSGBOX "Disabled Button is Enabled"
                        ELSE
                            MSGBOX "Disabled Button is Disabled"
                        END IF
                END SELECT
            END FUNCTION
            
            
            '----------------------------------------------------------------------------(')
            
            FUNCTION PBMAIN( ) AS LONG
                LOCAL v_lngResult AS LONG
                LOCAL v_hDlg AS DWORD
                DIALOG NEW PIXELS, %HWND_DESKTOP, "Test", 564, 406, 204, 88, %WS_POPUP OR _
                            %WS_BORDER OR %WS_DLGFRAME OR %WS_CAPTION OR %WS_SYSMENU OR _
                            %WS_MINIMIZEBOX OR %WS_CLIPSIBLINGS OR %WS_VISIBLE OR %DS_MODALFRAME _
                            OR %DS_CENTER OR %DS_3DLOOK OR %DS_NOFAILCREATE OR %DS_SETFONT, _
                            %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR _
                            %WS_EX_RIGHTSCROLLBAR, TO v_hDlg
                CONTROL ADD BUTTON, v_hDlg, %dlgMain.btnEnabled, "&Enabled", 20, 15, 75, 55
                CONTROL ADD BUTTON, v_hDlg, %dlgMain.btnDisabled, "&Disabled", 110, 15, 75, 55
                DIALOG SHOW MODAL v_hDlg, CALL cb_dlgMain TO v_lngResult
                FUNCTION = v_lngResult
            END FUNCTION
            <b>George W. Bleck</b>
            <img src='http://www.blecktech.com/myemail.gif'>

            Comment


            • #7
              Thanks all.
              Bernard Ertl
              InterPlan Systems

              Comment


              • #8
                FWIW, the companion to IsWindowEnabled() is EnableWindow().

                I prefer EnableWindow() to the DDT syntax, since the same call may be used to either enable or disable a window, as in ..

                Code:
                  bEnable =  ShouldWeEnableACertainControl(params) 
                  EnableWindow GetDlgItem(hWnd, %ID), bEnable
                With DDT you'd have to
                Code:
                  bEnable =  ShouldWeEnableACertainControl(params) 
                  IF ISTRUE bEnable THEN 
                       CONTROL ENABLE hWnd, %ID 
                  ELSE
                       CONTROL DISABLE hWnd, %ID
                  END IF
                I always thought DDT should have a single statement accepting an "enable/disable" argument..
                Code:
                   CONTROL SET ENABLE, hDlg, %ID, bEnable
                ... but 'twas never thus.

                MCM

                Comment

                Working...
                X