需要通过vba代码编辑putty的标题栏

时间:2014-04-10 19:06:26

标签: excel vba excel-vba vb6

我使用下面的vba代码在点击按钮上打开一个putty屏幕。

TaskID = Shell("C:\putty.exe 173.194.127.210", vbMaximizedFocus)

当它打开一个新屏幕时,标题栏将包含字符串“173.194.127.210 - PUTTY”。 当上面的代码打开一个新屏幕时,我想通过vba代码将“173.194.127.210 - PUTTY”更改为“173.194.127.210 - HELLO”。任何人都可以分享这样做的代码吗? 请注意,我没有使用超级腻子。

我可以通过以下步骤手动执行相同操作:

  1. 右键点击putty屏幕的标题栏。
  2. 点击更改设置
  3. 单击选项窗口
  4. 下的行为
  5. 将窗口标题更改为173.194.127.210 - HELLO

2 个答案:

答案 0 :(得分:3)

不幸的是,无法通过命令行更改此值。可以设置此值的唯一位置是会话。查看PuTTY Configuration页面,然后单击Session分支以查看此信息。总会有一个名为"默认设置"无法删除,只显示应用程序的内部默认值。你无法改变这些。但是,您可以以编程方式创建新会话,将窗口标题保存在其中,然后使用" -load" PuTTY命令行的选项,用于在启动应用程序时加载该会话。

会话的此信息存储在每个用户的注册表中,位于HKEY_CURRENT_USER \ Software \ SimonTatham \ PuTTY \ Sessions键下。此处的每个密钥都成为具有密钥名称的会话。为了创建不太可能与用户会话冲突的会话名称,下面的代码使用名称,该名称是应用程序EXE名称,前缀为两个下划线。

您需要为窗口标题编写的注册表值为" WinTitle"。但是,您还必须提供" HostName"," Protocol"和#34; Port" PuTTY正确打开的值。除" Port"之外的所有值是字符串(REG_SZ),而"端口"是一个整数(REG_DWORD)。

Option Explicit

Private Const HKEY_CURRENT_USER As Long = &H80000001

Private Const ERROR_SUCCESS     As Long = 0&

Private Const REG_SZ            As Long = 1
Private Const REG_DWORD         As Long = 4

Private Enum REGSAM
    KEY_ALL_ACCESS = &HF003F
    KEY_CREATE_LINK = &H20
    KEY_CREATE_SUB_KEY = &H4
    KEY_ENUMERATE_SUB_KEYS = &H8
    KEY_EXECUTE = &H20019
    KEY_NOTIFY = &H10
    KEY_QUERY_VALUE = &H1
    KEY_READ = &H20019
    KEY_SET_VALUE = &H2
    KEY_WOW64_32KEY = &H200
    KEY_WOW64_64KEY = &H100
    KEY_WRITE = &H20006
End Enum

Private Declare Function RegCloseKey Lib "Advapi32.dll" ( _
    ByVal hKey As Long _
) As Long

Private Declare Function RegCreateKeyEx Lib "Advapi32.dll" Alias "RegCreateKeyExW" ( _
    ByVal hKey As Long, _
    ByVal lpSubKey As Long, _
    ByVal Reserved As Long, _
    ByVal lpClass As Long, _
    ByVal dwOptions As Long, _
    ByVal samDesired As REGSAM, _
    ByVal lpSecurityAttributes As Long, _
    ByRef phkResult As Long, _
    ByRef lpdwDisposition As Long _
) As Long

Private Declare Function RegSetValueEx Lib "Advapi32.dll" Alias "RegSetValueExW" ( _
    ByVal hKey As Long, _
    ByVal lpValueName As Long, _
    ByVal Reserved As Long, _
    ByVal dwType As Long, _
    ByVal lpData As Long, _
    ByVal cbData As Long _
) As Long

Private Enum ConnectionType
    Raw
    Telnet
    Rlogin
    SSH
End Enum

Private Function OpenPutty(ByRef the_sHost As String, ByRef the_sTitle As String, ByVal enmConnectionType As ConnectionType, Optional ByVal the_nPort = -1) As Long

    Dim sUniqueSession                  As String
    Dim sKeyUniqueSession               As String
    Dim sConnectionType                 As String
    Dim nPort                           As Long
    Dim hKeyUniqueSession               As Long

    sUniqueSession = "__" & App.EXEName
    sKeyUniqueSession = "Software\SimonTatham\PuTTY\Sessions\" & sUniqueSession

    ' Provide the connection type / protocol string, and a default port value.
    Select Case enmConnectionType
    Case Raw
        sConnectionType = "raw"
        nPort = -1
    Case Telnet
        sConnectionType = "telnet"
        nPort = 23
    Case Rlogin
        sConnectionType = "rlogin"
        nPort = 513
    Case SSH
        sConnectionType = "ssh"
        nPort = 22
    End Select

    ' -1 indicates use the default port value.
    If the_nPort <> -1 Then
        nPort = the_nPort
    End If

    If RegCreateKeyEx(HKEY_CURRENT_USER, StrPtr(sKeyUniqueSession), 0&, 0&, 0&, KEY_SET_VALUE, 0&, hKeyUniqueSession, 0&) = ERROR_SUCCESS Then
        RegSetValueEx hKeyUniqueSession, StrPtr("HostName"), 0&, REG_SZ, StrPtr(the_sHost), LenB(the_sHost)
        RegSetValueEx hKeyUniqueSession, StrPtr("WinTitle"), 0&, REG_SZ, StrPtr(the_sTitle), LenB(the_sTitle)
        RegSetValueEx hKeyUniqueSession, StrPtr("Protocol"), 0&, REG_SZ, StrPtr(sConnectionType), LenB(sConnectionType)
        RegSetValueEx hKeyUniqueSession, StrPtr("PortNumber"), 0&, REG_DWORD, VarPtr(nPort), LenB(nPort)
        RegCloseKey hKeyUniqueSession
    End If

    OpenPutty = Shell(App.Path & "\putty.exe -load """ & sUniqueSession & """", vbMaximizedFocus)

End Function

Private Sub Command1_Click()
    OpenPutty "192.168.1.5", "My custom title", Telnet
End Sub

答案 1 :(得分:0)

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long

Sub Main()
    On Error Resume Next
    hwindows = FindWindow(vbNullString, "Microsoft Works Calendar")
    Ret = SetWindowText(hwindows, "Calandar")
End Sub