程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> C#啟動記事本,並傳遞數據

C#啟動記事本,並傳遞數據

編輯:.NET實例教程

        #region [ API: 記事本 ]

        /// <summary>
        /// 傳遞消息給記事本
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="Msg"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        [DllImport("User32.DLL")]
        public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, string lParam);

        /// <summary>
        /// 查找句柄
        /// </summary>
        /// <param name="hwndParent"></param>
        /// <param name="hwndChildAfter"></param>
        /// <param name="lpszClass"></param>
        /// <param name="lpszWindow"></param>
        /// <returns></returns>
        [DllImport("User32.DLL")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        /// <summary>
        /// 記事本需要的常量
        /// </summary>
        public const uint WM_SETTEXT = 0x000C;

        #endregion

$False$

        private void button1_Click(object sender, EventArgs e)
        {
            #region [ 啟動記事本 ]

            System.Diagnostics.Process Proc;

            try
            {
                // 啟動記事本
                Proc = new System.Diagnostics.Process();
                Proc.StartInfo.FileName = "notepad.exe";
                Proc.StartInfo.UseShellExecute = false;
                Proc.StartInfo.RedirectStandardInput = true;
                Proc.StartInfo.RedirectStandardOutput = true;

                Proc.Start();
            }
            catch
            {
                Proc = null;
            }

            #endregion

            #region [ 傳遞數據給記事本 ]

            if (Proc != null)
            {
                // 調用 API, 傳遞數據
                while (Proc.MainWindowHandle == IntPtr.Zero)
                {
                    Proc.Refresh();
                }

                IntPtr vHandle = FindWindowEx(Proc.MainWindowHandle, IntPtr.Zero, "Edit", null);

                // 傳遞數據給記事本
                SendMessage(vHandle, WM_SETTEXT, 0, "Message");
            }  #endregion

        } 



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved