2013-04-06 68 views
1

我不知道OpenFiledialog是否是正確的控件,但是我想在我的主窗體控件中打開一個已經添加了書籤的文件路徑。我有一個程序,實現這個,但我不知道如何去做。我的窗體中的OpenFileDialog不是windows瀏覽器彈出窗體

當用戶點擊一個按鈕時,下圖中左邊的列將顯示上次打開文件路徑的文件夾。如何去創造這個。我已經有了我想要顯示的文件夾的路徑,我應該使用哪個控件,任何代碼都將被高度讚賞 sample

+0

我不明白的問題 – 2013-04-06 13:54:45

+0

大衛嗨,我已經編輯我的問題和形象。我希望我的表單顯示我訪問的最後一條路徑,如圖片左側所示。 – Basco 2013-04-06 14:05:01

回答

0

您可以使用SetParent函數執行此操作。下面的 僅用於教育目的!

using System; 
using System.Diagnostics; 
using System.Linq; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.Windows.Forms; 
using System.Drawing; 

namespace WindowsFormsApplication4 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      this.Size = new System.Drawing.Size(800, 600); 
      this.TopMost = true; 
      this.FormBorderStyle = FormBorderStyle.FixedToolWindow; 
      Func<bool> run =() => 
       Window.Find(hwnd => 
       { 
        var cn = Window.GetClassName(hwnd); 
        var res = (cn == "CabinetWClass"); 
        if (res) 
        { 
         this.Controls.Clear(); 
         Window.SetParent(hwnd, this.Handle); 
         Window.SetWindowPos(hwnd, new IntPtr(0), -8, -30, this.Width + 10, this.Height + 37, 0x0040); 
        } 
        return res; 
       }); 

      new Button { Parent = this, Text = "Start" } 
       .Click += (s, e) => 
        { 
         if (run() == false) 
          MessageBox.Show("Open Explorer"); 
        }; 
     } 
    } 

    public static class Window 
    { 
     public static bool Find(Func<IntPtr, bool> fn) 
     { 
      return EnumWindows((hwnd, lp) => !fn(hwnd), 0) == 0; 
     } 
     public static string GetClassName(IntPtr hwnd) 
     { 
      var sb = new StringBuilder(1024); 
      GetClassName(hwnd, sb, sb.Capacity); 
      return sb.ToString(); 
     } 
     public static uint GetProcessId(IntPtr hwnd)  // {0:X8} 
     { 
      uint pid; 
      GetWindowThreadProcessId(hwnd, out pid); 
      return pid; 
     } 
     public static string GetText(IntPtr hwnd) 
     { 
      var sb = new StringBuilder(1024); 
      GetWindowText(hwnd, sb, sb.Capacity); 
      return sb.ToString(); 
     } 

     delegate bool CallBackPtr(IntPtr hwnd, int lParam); 

     [DllImport("user32.dll")] 
     static extern int EnumWindows(CallBackPtr callPtr, int lPar); 

     [DllImport("user32.dll")] 
     static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); 

     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); 

     [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] 
     static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); 

     [DllImport("User32", CharSet = CharSet.Auto, ExactSpelling = true)] 
     public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndParent); 

     [DllImport("user32.dll", SetLastError = true)] 
     public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int W, int H, uint uFlags); 
    } 
} 
+0

這與這個問題有什麼關係? – 2013-04-06 23:09:14

0

如果我理解你想打開Windows資源管理器在某個路徑的問題。 其簡單的話,你使用沒有控制只是這樣做:

Process.Start("Your path here"); 
+0

我終於使用了龔解決方案,它的工作完美 http://gong-shell.sourceforge.net/tutorials/filedialog.php – Basco 2013-04-07 11:14:27