zoukankan      html  css  js  c++  java
  • C# winform 启动外部程序的三种方式

    启用外部程序有很多方法,我自己要启动的程序因为内部调用了第三方的驱动,通过其它的调用方法均无法完美打开,始终有功能缺陷

    下面介绍几种可打开的方式:

    通过内置 Process 方式打开程序

    Process m_Process = null;
    m_Process = new Process();
    m_Process.StartInfo.FileName = @"C:	est.exe";
    m_Process.Start();

    通过win32 ,设置桌面鼠标位置,通过方法模拟鼠标双击事件

    //在class下面放入以下方法
    //绑定事件
    [System.Runtime.InteropServices.DllImport("user32")]
    private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
    
    //定位
    [DllImport("User32")]
    public extern static void SetCursorPos(int x, int y);  
     
    
    //代码放在需要执行的地方
    const int MOUSEEVENTF_LEFTDOWN = 0x0002;
    const int MOUSEEVENTF_LEFTUP = 0x0004;
    SetCursorPos(33, 28);
    
    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

    shell32.dll 方法

    //class里面放入这段代码
    [DllImport("shell32.dll")]
    public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
    
    //需要打开的地方插入此段代码
    ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder("test.exe"), new StringBuilder(""), new StringBuilder(@"C:文件夹名"), 1);

    以上三种方法,都能实现从winform打开外部程序

    第一种最方便,但是调用的外部程序中如果有使用第三方或系统插件时是无法调用起来的,今天我这里就不行

    第二种是通过设置鼠标在桌面中的位置,模拟双击事件,这种方式虽然可行,但是当服务器锁屏或者需要双击的位置被其它文件覆盖时则无法达到预期效果

    第三种是都可以的,缺陷是无法关闭它,需要自己再另外处理。

  • 相关阅读:
    NYOJ 10 skiing DFS+DP
    51nod 1270 数组的最大代价
    HDU 4635 Strongly connected
    HDU 4612 Warm up
    POJ 3177 Redundant Paths
    HDU 1629 迷宫城堡
    uva 796
    uva 315
    POJ 3180 The Cow Prom
    POJ 1236 Network of Schools
  • 原文地址:https://www.cnblogs.com/chonglu/p/12972429.html
Copyright © 2011-2022 走看看