2013-04-25 29 views
4

我需要我的應用程序來打開Excel 2010並將其放置在我的應用程序的特定區域中。 我還需要Excel是不可移動的,不可移動並消除所有菜單交互(關閉,最大化,最小化)如何防止在C#中移動另一個應用程序(MS Excel 2010)

我已經找到了幾乎所有的解決方案,使它不可移動。 我試圖使用SetWindowPos並設置SWP_NOMOVE標誌和一些其他標誌,但我沒有成功。

--Specifications:我使用WPF C#.NET 4.0和MS Excel 2010中

--Below是我特地與方法。一切工作從SetWindowPos除了預期(它不帶任何效果)

public static void StartExcel(string p_processArguments, int p_x, int p_y, int p_height, int p_width, 
            bool p_startMin = true, bool p_setForeground = true, bool p_useShell = true, 
            bool p_waitInput = true, bool p_setNewStyles = true, bool p_removeMenu = true) 
    { 
     //Make sure there is no excel opened (Kill them all - if any) 
     CloseAllExcelProcesses(true); 

     //Make the most basic validations and required info.   
     if (!ValidateProcessArgument(p_processArguments)) 
      throw new Exception("Process' argument is invalid or incorrectly setted. " + p_processArguments); 

     ProcessStartInfo psiApp = new ProcessStartInfo("excel", p_processArguments); 
     if (p_useShell) 
      psiApp.UseShellExecute = true; 
     else 
      psiApp.UseShellExecute = false; 

     if (p_startMin) 
      psiApp.WindowStyle = ProcessWindowStyle.Minimized; 

     Process pApp = Process.Start(psiApp); 
     if (p_waitInput) 
      pApp.WaitForInputIdle(); 

     //Wait for the app to receive the window handle(ID) max limit of 3sec. 
     for (int i = 0; i < 25; i++) 
     { 
      System.Threading.Thread.Sleep(100); 
     } 

     if (pApp.MainWindowHandle != (IntPtr)0) 
     { 
      if (p_startMin) //Now restore its state 
       Win32Import.ShowWindow(pApp.MainWindowHandle, WindowShowStyle.ShowNormal); 

      //Set Foreground 
      if (p_setForeground) 
       Win32Import.SetForegroundWindow(pApp.MainWindowHandle); 

      if (p_setNewStyles) 
      { 
       //Make it an Overlapped Window (Which has no size border, title bar and etc). 
       int style = Win32Import.GetWindowLong(pApp.MainWindowHandle, Win32Import.GWL_STYLE); 
       Win32Import.SetWindowLong(pApp.MainWindowHandle, Win32Import.GWL_STYLE, (uint)(style & ~Win32.WindowStyles.WS_OVERLAPPEDWINDOW)); 

       //NOT WORKING - Apply some flags, to make it unmovable. 
       Win32Import.SetWindowPos(pApp.MainWindowHandle, (IntPtr)0, 0, 0, 0, 0, Win32.SWP.NOMOVE); 
       Win32Import.UpdateWindow(pApp.MainWindowHandle); 
      } 

      if (p_removeMenu) 
      {      
       //Get the app original menu. 
       IntPtr hMenu = Win32Import.GetSystemMenu(pApp.MainWindowHandle, false); 
       //Get the amount of menu the app has. 
       int count = Win32Import.GetMenuItemCount(hMenu); 

       //Remove all existing main menus. 
       for (uint i = 0; i < count; i++) 
        Win32Import.RemoveMenu(hMenu, i, (Win32Import.MF_BYPOSITION | Win32Import.MF_REMOVE)); 

       //Force a redraw. 
       Win32Import.DrawMenuBar(pApp.MainWindowHandle); 
      } 

      //Move the window to the specified location and size (set new position).     
      Win32Import.MoveWindow(pApp.MainWindowHandle, p_x, p_y, p_width, p_height, true);     
     } 
     else 
     { 
      throw new Exception("StartEmbeddedApp - Couldn't get the embedded app handle."); 
     } 
    } 

我也拿出了幾個不同的想法,如interceping的Excel WM_MESSAGE(比如用HWNDSource)。任何想法不要太複雜,以實現這一點是值得歡迎的

在此先感謝, 路易斯。

+0

在一個不相關的注意,['IntPtr.Zero'(http://msdn.microsoft.com/en-us/library/system.intptr.zero.aspx) 。 – Romoku 2013-04-25 13:58:55

+0

Yuck。不會像[this](http://www.codeproject.com/Articles/15760/How-to-Integrate-Excel-in-a-Windows-Form-Applicati)更簡單(即使我懷疑還是比必要的更復雜)? – hvd 2013-04-25 14:16:31

+0

hvd,我看過你之前提到的那篇文章。但是,它不再工作。我剛剛下載並使用MS Excel 2010進行了試用。從未捕獲並放入Excel。它只是在箱子外正常打開。 – Luishg 2013-04-25 14:22:58

回答

0

我想你錯了關於SWP_NOMOVE參數。該參數並不意味着該窗口將不可移動,它只是意味着在對SetWindowPos方法的特定調用期間,x,y參數將被忽略。該標誌的目標是使用函數SetWindowPos來改變目標窗口的大小而不改變其位置。

From SetWindowPos in MSDN:

SWP_NOMOVE 0×0002 保留當前位置(忽略X和Y參數)。

的東西,你可以嘗試是創建處理有關大小和位置的所有消息,您的應用程序自定義窗口(例如:WM_SIZE,WM_SIZING,WM_WINDOWPOSCHANGED,WM_WINDOWPOSCHANGING等),以防止修改,然後設置您的Excel窗口您的自定義窗口的子窗口。

您也可以嘗試to just remove the title bar

+1

一個布魯斯部隊方法將持續進行輪詢,並強制用戶的應用程序只是有重點的窗口。 – 2013-04-25 14:51:17

+0

Ramhound,不幸的是,這不是一個選項。 Excel需要關注。用戶可以與其工作簿,工作表,保存等進行交互,但用戶無法移動它! – Luishg 2013-04-25 15:01:01

相關問題