2016-04-22 127 views
2

过了一段时间,因为我做任何程序如此生锈。我正在研究代码来最大化和最小化其他应用程序。所以我找到了一些基本的东西,这是我的东西,从原来的东西稍微修改一下。它希望我生成一些我所做的FindWindow方法。现在一切看起来不错,我试图运行它,得到一个消息。不知道该从哪里出发。我发现它的原始线程没有提到这一点。最大化/最小化其他应用程序

enter image description here

private const int SW_SHOWNORMAL = 1; 
private const int SW_SHOWMINIMIZED = 2; 
private const int SW_SHOWMAXIMIZED = 3; 

[DllImport("user32.dll")] 
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); 
static void Main(string[] args) 
{ 
    // retrieve Notepad main window handle 
    IntPtr hWnd = FindWindow("Notepad", "Untitled - Notepad"); 
    if (!hWnd.Equals(IntPtr.Zero)) 
    { 
     // SW_SHOWMAXIMIZED to maximize the window 
     // SW_SHOWMINIMIZED to minimize the window 
     // SW_SHOWNORMAL to make the window be normal size 
     ShowWindowAsync(hWnd, SW_SHOWMAXIMIZED); 
    } 
} 

private static IntPtr FindWindow(string p, string p_2) 
{ 
    throw new NotImplementedException(); 
} 

回答

1

首先,你的方法FindWindow(),当一个方法,你需要抓住它在它是在这种情况下,Main()调用的方法抛出。

现在NotImplementedExceptionis类,这里我发表你的继承层次

  1. System.Object的
  2. System.Exception的
  3. System.SystemException
  4. System.NotImplementedException

由于说错误,你只需要实现该方法并删除de行:`throw new NotImplemente dException();

最后我发布了一个实现选项,只需要窗口应用程序的标题即可。

public static IntPtr FindWindow(string titleName) 
    { 
     Process[] pros = Process.GetProcesses("."); 
     foreach (Process p in pros) 
      if (p.MainWindowTitle.ToUpper().Contains(titleName.ToUpper())) 
       return p.MainWindowHandle; 
     return new IntPtr(); 
    } 

顺便说一句,here约为最大化/最小化其他应用程序的另一个问题