2014-10-06 24 views
1

我有一個小問題,並試圖解決它現在近6至8小時,但我沒有找到任何匹配的答案。我是一個WPF的新手,所以請給我指出我犯的任何錯誤。從WinForms轉換MultipleMonitor應用程序到WPF

起初我已經在我的App.xaml.cs如下:

namespace WpfVideowand 
{ 
    public partial class App : Application 
    { 
    ... 
    private void Application_Startup(object sender, StartupEventArgs e) 
    { 
     foreach (System.Windows.Forms.Screen MyScreen in System.Windows.Forms.Screen.AllScreens) 
     { 
     List<string> MyStrings = Xml.GetScreens(i); 
     if (MyStrings[1] == "true") 
     { 
      OpenWindow(MyScreen, MyStrings[0], i); 
     } 
     i++; 
     Shelf MyShelf = new Shelf(MyScreen, i, MyStrings[0]); 
     MyShelf.Show(); 
     } 
    } 
    private void OpenWindow(System.Windows.Forms.Screen myScreen, string configName, int screenNumber) 
    { 
     Shelf NewShelf = new Shelf(myScreen, screenNumber, configName); 
    } 
    } 
} 

而且Shelf.xaml.cs裏面,它看起來是這樣的:

namespace WpfVideowand 
{ 
    public partial class Shelf : Window 
    { 
    [DllImport("user32.dll")] 
    static extern IntPtr GetActiveWindow(); 
    System.Windows.Forms.Screen _Screen { get; set; } 
    ... 
    public Shelf(System.Windows.Forms.Screen myScreen, int screenNumber, string configName) 
    { 
     InitializeComponent(); 
     _Screen = myScreen; 
     ShowOnMonitor(screenNumber); 
     ... 
    } 
    private void ShowOnMonitor(int screenNumber) 
    { 
     System.Windows.Forms.Screen[] ScreenArray; 
     ScreenArray = System.Windows.Forms.Screen.AllScreens; 
     int XCoord = Convert.ToInt32(ScreenArray[screenNumber].Bounds.Left); 
     this.Left = XCoord; 
     int YCoord = Convert.ToInt32(ScreenArray[screenNumber].Bounds.Top); 
     this.Top = XCoord; 
     IntPtr active = GetActiveWindow(); 
     System.Windows.Application.Current.Windows.OfType<Window>().SingleOrDefault(window => new WindowInteropHelper(window).Handle == active).Name = "Monitor" + screenNumber.ToString(); 
     System.Windows.Application.Current.Windows.OfType<Window>().SingleOrDefault(window => new WindowInteropHelper(window).Handle == active).WindowState = WindowState.Maximized; 
    } 
    ... 
    } 
} 

上面所描述的工作方式在Windows窗體應用程序罰款。在WPF中,我遇到了問題,我得到了錯誤消息,那個矩形(窗口)將沒有Top或Left屬性。

我甚至嘗試了一些其他的方式,比如用

System.Windows.Forms.Screen _screen = System.Windows.Forms.Screen.FromControl(this); 

創建一個對象,這將有.TOP和。左。但是我得到的消息是,我無法將Shelf對象轉換爲System.Windows.Forms.Control。 任何人的建議,我怎麼能讓我的屏幕出現在顯示器上它應該是什麼?

回答

0

好吧,我自己找到了...... 對於有興趣找到這個問題的一些答案的人,這裏是: 首先,看到你已經實現了正確的參考。爲此,您需要System.Drawing和System.Windows.Forms。 (之後,你必須聲明很多東西,比如System.Windows.Controls.Button,而不是按鈕等) 然後,看到你啓動的應用程序類似於Startup =「Application_Startup」,而不是uri,因爲你想開始多種形式,而不僅僅是一種。 之後絕對確保不要將Windowstyle設置爲在XAML中最大化(這耗費了我近4個小時的時間,而我之間長了56根灰色頭髮)。在代碼隱藏中使用它: System.Windows.Application.Current.Windows.OfType()。SingleOrDefault(window => new WindowInteropHelper(window).Handle == active).WindowState = WindowState.Maximized;

相關問題