最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#定位txt指定行的方法小例子

 更新時間:2013年04月24日 15:43:59   投稿:shangke  
近日,在開發(fā)CAD插件時需要定位TXT文件指定行并將其選中,在網(wǎng)絡找了一下沒有找到現(xiàn)成的,自己編了一個定位程序,實現(xiàn)了定位功能..與大家分享

復制代碼 代碼如下:

            [DllImport("User32.dll", EntryPoint = "FindWindow")]
            private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
            [DllImport("user32.dll")]
            static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
            [DllImport("user32.dll")]
            static extern bool SetForegroundWindow(IntPtr hWnd);
            ///<summary>
            /// 定位到txt文件指定行
            ///</summary>
            ///<param name="strFullName">文件路徑</param>
            ///<param name="strRow">指定行</param>
            ///<returns>定位是否成功</returns>
            private bool LocateNotePad(string strFullName, string strRow)
            {
                int iRow;
                int.TryParse(strRow, out iRow);
                if (iRow <= 0)
                {
                    return false;
                }
                IntPtr hwnd = FindWindow("Notepad", string.Format("{0} - 記事本", Path.GetFileName(strFullName)));//查看當前文件是否已打開
                if (hwnd.ToInt32() == 0)
                {
                    Process p = Process.Start(@"notepad.exe",strFullName);
                    p.WaitForInputIdle(1000);  //等一秒,等文本打開,焦點去到notepad
                    System.Windows.Forms.SendKeys.SendWait("{DOWN " + (iRow - 1) + "}");
                    System.Windows.Forms.SendKeys.SendWait("{HOME}"); //行首
                    System.Windows.Forms.SendKeys.SendWait("+{END}"); //選中當前行
                    return true;
                }
                else
                {
                    hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Edit", string.Empty);
                    if (hwnd.ToInt32() == 0) return false;
                    else
                    {
                        SetForegroundWindow(hwnd);
                        System.Windows.Forms.SendKeys.SendWait("^{HOME}");//將光標定位到首行
                        System.Windows.Forms.SendKeys.SendWait("{DOWN " + (iRow - 1) + "}"); //
                        System.Windows.Forms.SendKeys.SendWait("{HOME}"); //行首
                        System.Windows.Forms.SendKeys.SendWait("+{END}"); //選中當前行
                    }
                }
                return true;
            }


調用代碼 LocateNotePad("D:\\test.txt","3");

代碼很簡單,通過FindWindow,FindWindowEx,SetForegroundWindow三個API進行獲取句柄并設置進程當前以及發(fā)送系統(tǒng)命令操作,利用winform中的SendKeys發(fā)送鍵盤命令達到定位的目的.

PS:此命令需要增加 System.Windows.Forms,在引用處添加..希望對各位有幫助,也希望能得到各位朋友的指點改進,謝謝

相關文章

最新評論

莫力| 甘肃省| 庆安县| 读书| 揭东县| 平阳县| 定兴县| 时尚| 寻甸| 太仆寺旗| 大关县| 潞西市| 民丰县| 博野县| 姚安县| 岐山县| 无锡市| 绥芬河市| 青川县| 延寿县| 万载县| 时尚| 方山县| 砀山县| 饶河县| 平昌县| 彩票| 营口市| 龙井市| 措美县| 南宫市| 娄烦县| 武川县| 洮南市| 青河县| 伊宁县| 阿克陶县| 咸丰县| 从江县| 南漳县| 湖口县|