从命令提示符执行按钮

时间:2011-03-02 21:55:00

标签: vb.net command-line

我正在开发一个vb.net项目,我在FormPost.exe上有一个“开始”和“暂停”按钮

我正在尝试安排批处理过程每天凌晨4:00运行。

如何运行命令提示符来执行FormPost.exe,然后单击“开始”按钮,全部通过命令提示符?

请告诉我。感谢

2 个答案:

答案 0 :(得分:1)

你可以做的是覆盖OnControlCreateMethod(),如下所示:

Public Class Form1
   Public Sub New()
     ' This call is required by the designer.
     InitializeComponent()

     ' Add any initialization after the InitializeComponent() call.

   End Sub

   Protected Overrides Sub OnCreateControl()
      MyBase.OnCreateControl()
      If Environment.CommandLine.Contains("/clickme") Then
         ClickMeButton.PerformClick()
         // ... need to wait here until click event handler has finished, e.g.
         // using synchronization objects
         Close()
      End If
   End Sub
End Class

如果在命令行上传递“/ clickme”,它将执行click事件,然后关闭表单。如果表单是应用程序中唯一的表单,它将终止。

但请注意,您需要添加一些等待click事件处理程序完成的逻辑。尽量避免使用轮询或睡眠。而是尝试使用同步对象。

答案 1 :(得分:0)

如果这是您的申请;您可以修改代码,以便它检查您是否从命令行/使用适当的参数运行它并触发按钮click()本身。这是最简单的方法(我认为John的答案显示了这一点)

如果不是你的申请;你仍然可以做同样的事情,但它不是那么漂亮。您可以编写将执行winForm然后激活它的代码(以确保它具有焦点)

Public Shared Sub ActivateWoW()
   Dim myApp As Process = Process.GetProcessesByName("MyApp").First
   AppActivate(myApp.Id)
End Sub

然后,您可以使用SendKeys()来模拟与表单的交互。假设开始按钮需要选择两个“标签”键...

SendKeys.Send("{TAB}{TAB}")

然后快速停顿......

Thread.Sleep(25)

然后点击回车键(选择按钮时几乎总是和鼠标一样好)

SendKeys.Send("{ENTER}")

如果你想要更多地参与其中;您需要开始使用WIN32 API调用。以下是鼠标单击的一些示例代码...

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

Private Const WM_LBUTTONUP As Long = &H202
Private Const WM_RBUTTONUP As Long = &H205

Private Const WM_LBUTTONDOWN As Long = &H201
Private Const WM_RBUTTONDOWN As Long = &H204

Private Shared Function MakeDWord(ByVal LoWord As Integer, ByVal HiWord As Integer) As Long
    Return (HiWord * &H10000) Or (LoWord And &HFFFF&)
End Function

Public Shared Sub SendMouseClick()
    Dim Wow As Long = FindWindow("GxWindowClass", "MyWindow")
    Dim dWord As Long = MakeDWord(LastX - LastRectX, LastY - LastRectY)

    SendMessage(Wow, WM_RBUTTONDOWN, 1&, dWord)
    Threading.Thread.Sleep(100)
    SendMessage(Wow, WM_RBUTTONUP, 1&, dWord)

End Sub