Jump to content

Select Registry Key dialog


Mat
 Share

Recommended Posts

I've been doing some work on embedding explorer windows + controls into GUI's and letting them do the hardwork, and decided to see if it could be applied to other windows. The result was so nice I decided i'd share. I've always wanted one of these anyway! Don't worry, i'm still working on embedded explorer panes, and I have made some progress there as well. Thanks to WBD for the idea of navigating to the key using Send ("left/right") etc.

; #FUNCTION# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
; Name...........: _RegSelectKey
; Description ...: Initiates a dialog for the user to select a registry key.
; Syntax.........: _RegSelectKey ( [$sText [, $sDefault [, $hWnd]]] )
; Parameters ....: $sText       - The dialog text
;                  $sDefault    - Path to the initial registry key. If "" (default) is last used.
;                  $hWnd        - handle to a parent window.
; Return values .: Success      - The path to the selected key.
;                  Failure      - 0
; Author ........: Mat Diesel (Mat)
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: $tmp = _RegSelectKey ("test", "HKCR\AutoitScript")
;                  MsgBox (0, "Result", $tmp)
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
_RegSelectKey ("test", "HKCR\Autoit3Script")


Func _RegSelectKey ($sText = "", $sDefault = "", $hWnd = 0)
   Local $hGUI, $hTree, $hBtn_Ok, $hBtn_Cancel, $hLbl_Txt, $ret = 0

   If $sDefault <> "" Then
      If StringRight ($sDefault, 1) <> "\" Then $sDefault &= "\"
      RegRead ($sDefault, "")
      If @Error Then $sDefault = "NULL"
      Switch StringLeft ($sDefault, 4)
         Case "HKLM"
            $sDefault = "HKEY_LOCAL_MACHINE"  & StringTrimLeft ($sDefault, 4)
         Case "HKCR"
            $sDefault = "HKEY_CLASSES_ROOT"   & StringTrimLeft ($sDefault, 4)
         Case "HKCU"
            $sDefault = "HKEY_CURRENT_USER"   & StringTrimLeft ($sDefault, 4)
         Case "HKU\"
            $sDefault = "HKEY_USERS"          & StringTrimLeft ($sDefault, 3)
         Case "HKCC"
            $sDefault = "HKEY_CURRENT_CONFIG" & StringTrimLeft ($sDefault, 4)
         Case "HKEY"
         Case Else
            Return SetError (2, 0, 0)
      EndSwitch
   EndIf

   Local $iOpt = Opt ("GUIOnEventMode", 0)
   $hGUI = GUICreate ("Select Registry Key", 318, 310, 69, 34, 46669824, 1024, $hWnd)

   $hLbl_Txt = GUICtrlCreateLabel ($sText, 11, 11, 296, 33, 0x50020000, 0x00000004)

   $hBtn_Ok = GUICtrlCreateButton ("Ok", 150, 270, 75, 23)
   $hBtn_Cancel = GUICtrlCreateButton ("Cancel", 231, 270, 75, 23)

   GUISetState ()

   Run ("REGEDIT", @ScriptDir, @SW_HIDE)
   WinWait ("[CLASS:RegEdit_RegEdit]")
   $hRegEdit = WinGetHandle ("[CLASS:RegEdit_RegEdit]")

   $hTree = ControlGetHandle ($hRegedit, "", "[CLASS:SysTreeView32; INSTANCE:1]")
   WinMove ($hTree, "", 11, 44, 296, 206)
   DllCall("user32.dll", "hwnd", "SetParent", "hwnd", $hTree, "hwnd", $hGUI)
   WinSetState ($hTree, "", @SW_SHOW)

   ControlFocus ($hGUI, "", $hTree)

   If $sDefault <> "" Then
      Send ("{LEFT 30}")
      Send ("{RIGHT}")
      For $i = 1 To StringLen ($sDefault)
         If StringMid ($sDefault, $i, 1) = "\" Then
            Send ("{RIGHT}")
         Else
            Send (StringMid ($sDefault, $i, 1))
         EndIf
      Next
   EndIf

   Local $nMsg, $nCur
   While 1
      $nMsg = GUIGetMsg ()
      Switch $nMsg
         Case -3, $hBtn_Cancel
            $ret = 0
            ExitLoop
         Case $hBtn_Ok
            Local $sSel = ControlTreeView ($hGUI, "", $hTree, "GetSelected", 1)
            If String ($sSel) = "0" Then ExitLoop
            Local $aParts = StringSplit ($sSel, "|"), $sString = $aParts[1]
            $ret = ""
            For $i = 2 to $aParts[0]
               $sString &= "|" & $aParts[$i]
               $ret &= "\" & ControlTreeView ($hGUI, "", $hTree, "GetText", $sString, 0)
            Next
            $ret = StringTrimLeft ($ret, 1)
            ExitLoop
      EndSwitch
   WEnd

   GUIDelete ($hGUI)
   WinClose ($hRegedit)
   Opt ("GUIOnEventMode", $iOpt)
   If String ($ret) = "0" Then Return SetError (1, 0, 0)
   Return $ret
EndFunc ; ==> _RegSelectKey

Mat

Edited by Mat
Link to comment
Share on other sites

Nice!

But here is few things that can be done differently:

Instead of loop with send left key use just «Send("{LEFT 30}")».

Instead of these conditions «If StringLeft ($sDefault, 4) = "HKLM" Then» you could use array and loop:

$aHK_Short_Names = StringSplit("LM|CR|CU|U|CC", "|")
    $aHK_Long_Names = StringSplit("LOCAL_MACHINE|CLASSES_ROOT|CURRENT_USER|USERS|CURRENT_CONFIG", "|")
   
    For $i = 1 To $aHK_Short_Names[0]
        $sDefault = StringRegExpReplace($sDefault, "(?im)^HK" & $aHK_Short_Names[$i], "HKEY_" & $aHK_Long_Names[$i])
    Next

And also after ControlFocus($hGUI, "", $hTree) there is need to add WinActivate($hGUI), otherwise the window does not shown.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Instead of loop with send left key use just «Send("{LEFT 30}")».

:) I did mention it was WBD's code didn't I? ;)

Instead of these conditions «If StringLeft ($sDefault, 4) = "HKLM" Then» you could use array and loop:

<code>

hmm... It would look a bit prettier but... no difference in lines, only yours is slower and uses extra variables. However, I would consider a slightly more pleasant method. See first post for that update.

And also after ControlFocus($hGUI, "", $hTree) there is need to add WinActivate($hGUI), otherwise the window does not shown.

No problem here... I can't seem to reproduce it either.

Thanks for the tips, I'll update first post with my results.

Mat

Link to comment
Share on other sites

  • 3 years later...

I realize this post is very old, but I needed this functionality, stumbled upon this code, and this is perfect for my needs.

Just one thing - for some reason, when the script exits, the keyboard is in a state as if {!LSHIFT} (Alt-Shift) key sequence is being perpetually held. I have to press the Shift key, manually, to make it stop. I'm mucking with the code right now, trying to fix it, going so far as to try and send a {LSHIFT} right before the script exits. So far my efforts have been in vain. Can anyone see what the problem is and help to quickly resolve it?

Thanks in advance.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...