最新国产好看的视频,伊人天堂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;
            }


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

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

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

相關文章

最新評論

安仁县| 神木县| 兴城市| 临高县| 旬邑县| 顺义区| 洱源县| 乐陵市| 闽清县| 建水县| 泰兴市| 绥阳县| 岳西县| 仪陇县| 南通市| 鲁山县| 麟游县| 霍城县| 密山市| 红安县| 金华市| 栾川县| 银川市| 榆中县| 灵璧县| 湘潭市| 耿马| 永年县| 博客| 甘肃省| 旬阳县| 武宣县| 开阳县| 新郑市| 开平市| 深圳市| 子长县| 桃江县| 长宁区| 改则县| 北宁市|