• 控制其它程序


    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        class XMindUtil
        {
            private static IntPtr _hwndPhoto;
    
            [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
            private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
            [DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
            private static extern void SetForegroundWindow(IntPtr hwnd);
    
            [DllImport("user32.dll", EntryPoint = "SendMessage")]
            public static extern int SendMessage(IntPtr hwnd, int wMsg, uint wParam, uint lParam);
    
            public static Process MyProcess;
    
            #region 杀掉进程   
            public static void Kill()
            {
                try
                {
                    var processes = Process.GetProcessesByName("xmind");
                    foreach (var process in processes)
                    {
                        process.Kill();
                    }
    
                    processes = Process.GetProcessesByName("MindManager");
                    foreach (var process in processes)
                    {
                        process.Kill();
                    }
                }
                catch (Exception)
                {
                }
            }
            #endregion
    
            #region 导出MindManager格式文件
            /// <summary>
            /// 功能:导出mmap文件格式
            /// 作者:黄海
            /// 时间:
            /// </summary>
            /// <param name="source"></param>
            /// <param name="target"></param>
            /// <returns></returns>
            public static bool ExportMmap(string source, string target)
            {
                var sourceFile = new FileInfo(source);
                var appName = @"C:XMindXMind_WindowsXMind.exe";
                var psi = new ProcessStartInfo
                {
                    FileName = appName,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    WorkingDirectory = @"C:XMindXMind_Windows",
                    Arguments = source
                };
                MyProcess = Process.Start(psi);
                WaitXMindStart(sourceFile);
                _hwndPhoto = FindWindow(null, "XMind Pro - " + sourceFile.Name);
    
                if (_hwndPhoto != IntPtr.Zero)
                {
                    SetForegroundWindow(_hwndPhoto);
                    Thread.Sleep(500);
                    SendKeys.Send("%f");
                    Thread.Sleep(500);
                    SendKeys.Send("e");
                    Thread.Sleep(500);
                    SendKeys.Send("~");
                    Thread.Sleep(500);
                    SendKeys.Send("{TAB}");
                    SendKeys.Send("{TAB}");
                    SendKeys.Send("{TAB}");
                    SendKeys.Send("{TAB}");
                    Thread.Sleep(500);
                    var fi = new FileInfo(target);
                    if (fi.Exists)
                    {
                        fi.IsReadOnly = false;
                        fi.Delete();
                    }
                    SendKeys.Send("{DELETE}");
                    Thread.Sleep(1000);
                    Clipboard.SetDataObject(target);
                    SendKeys.Send("^v");
                    Thread.Sleep(500);
                    SendKeys.Send("~");
                    while (true)
                    {
                        var a = FindWindow(null, "成功导出");
                        if (a != IntPtr.Zero)
                        {
                            break;
                        }
                        else
                        {
                            Thread.Sleep(100);
                        }
                    }
                    SendKeys.Send("%{F4}");
                    Thread.Sleep(500);
                    SendKeys.Send("%{F4}");
                    Thread.Sleep(500);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            #endregion
            
            #region 导出SWF格式文件
            public static bool ExportSwf(string source, string target)
            {
                var fi = new FileInfo(target);
                if (fi.Exists)
                {
                    fi.IsReadOnly = false;
                    fi.Delete();
                }
    
                var sourceFile = new FileInfo(source);
                var appName = @"C:Program FilesMindjetMindManager 16MindManager.exe";
                var psi = new ProcessStartInfo
                {
                    FileName = appName,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    WorkingDirectory = @"C:Program FilesMindjetMindManager 16",
                    Arguments = source
                };
                MyProcess = Process.Start(psi);
                WaitmmapStart(sourceFile);
    
                var hwndMmap = FindWindow(null, "Mindjet MindManager - " + sourceFile.Name);
    
                if (hwndMmap != IntPtr.Zero)
                {
                    SetForegroundWindow(hwndMmap);
                    SendKeys.Send("%");
                    Thread.Sleep(500);
    
                    SendKeys.Send("f");
                    Thread.Sleep(500);
                    SendKeys.Send("a");
    
                    Thread.Sleep(500);
                    SendKeys.Send("{TAB}");
                    Thread.Sleep(500);
                    SendKeys.Send("~");
                    Thread.Sleep(500);
                    SendKeys.Send("{DELETE}");
                    Thread.Sleep(100);
    
                    Clipboard.SetDataObject(target);
                    SendKeys.Send("^v");
                    Thread.Sleep(500);
                    SendKeys.Send("{TAB}");
                    Thread.Sleep(500);
                    SendKeys.Send("{DOWN}");
                    SendKeys.Send("{DOWN}");
                    SendKeys.Send("{DOWN}");
                    SendKeys.Send("{DOWN}");
                    SendKeys.Send("{DOWN}");
                    SendKeys.Send("{DOWN}");
                    SendKeys.Send("{DOWN}");
                    SendKeys.Send("{DOWN}");
                    SendKeys.Send("{DOWN}");
                    SendKeys.Send("{DOWN}");
                    SendKeys.Send("{DOWN}");
                    SendKeys.Send("{DOWN}");
                    Thread.Sleep(500);
                    SendKeys.Send("~");
                    Thread.Sleep(500);
                    SendKeys.Send("~");
    
                    while (true)
                    {
                        var a = FindWindow(null, "Mindjet Viewer");
                        if (a != IntPtr.Zero)
                        {
                            break;
                        }
                        else
                        {
                            Application.DoEvents();
                        }
                    }
                    SendKeys.SendWait("%{F4}");
                    Thread.Sleep(500);
                    SendKeys.SendWait("%{F4}");
                    Thread.Sleep(500);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            #endregion
    
            #region 等待启动完成
            public static void WaitXMindStart(FileInfo sourceFile)
            {
                while (true)
                {
                    var hwndPhoto = FindWindow(null, "XMind Pro - " + sourceFile.Name);
    
                    if (hwndPhoto != IntPtr.Zero)
                    {
                        break;
                    }
                    Application.DoEvents();
                }
            }
            public static void WaitmmapStart(FileInfo sourceFile)
            {
                while (true)
                {
                    var hwndPhoto = FindWindow(null, "Mindjet MindManager - " + sourceFile.Name);
    
                    if (hwndPhoto != IntPtr.Zero)
                    {
                        break;
                    }
                    Application.DoEvents();
                }
            }
            #endregion
        }
    }

    用法:

     private void button3_Click(object sender, EventArgs e)
            {
                var source = @"c:abcdefg.xmind";
                var target = @"c:abcdefg.mmap";
    
                XMindUtil.ExportMmap(source, target);
            }
            
            private void button1_Click(object sender, EventArgs e)
            {
                var source = @"c:abcdefg.mmap";
                var target = @"c:abcdefg.swf";
    
                XMindUtil.ExportSwf(source, target);
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                XMindUtil.Kill();
            }
  • 相关阅读:
    Javascript学习笔记3 Javascript与BOM简介
    Javascript学习笔记2.3 Javascript与DOM实现动态表格效果
    Javascript学习笔记2.2 Javascript与DOM选项卡(滑动门)案例详解
    javascript是做什么的
    Javascript学习笔记2.1 Javascript与DOM简介
    Javascript学习笔记1 javascript的特点
    CSS3新增的选择器和属性
    DNSlog实现Mysql注入
    [转]Firefox+Burpsuite抓包配置(可抓取https)
    爬虫初窥day3:BeautifulSoup
  • 原文地址:https://www.cnblogs.com/littlehb/p/5937303.html
Copyright © 2020-2023  润新知