2013-02-21 77 views
0

我只知道一些关于c#的知识,我的大部分代码都是关闭的。我正在尝试的是移动一个窗口,我通过使用SetWindowPos得到了这个工作。但是,我希望窗口局部在屏幕外,但Microsoft Windows不允许。下面的代码是我到目前为止。当然,我在谷歌搜索答案,并找到答案,我不知道唯一的是如何把这个到我的程序。SetWindowPos在屏幕外

有人可以解释我如何将它放入我的应用程序?

谷歌回答:Move a window partially off the top of the screen

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ComponentModel; 
using System.Data; 
using System.Runtime.InteropServices; 
using System.Diagnostics; 

namespace WindowMover 
{ 

    static class Logic 
    { 

     [DllImport("user32.dll", EntryPoint = "SetWindowPos")] 
     public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags); 

     public static void Main() 
     { 
      const short SWP_NOSIZE = 1; 
      const short SWP_NOZORDER = 0X4; 
      const int SWP_NOACTIVATE = 0x0010; 

      Process[] processes = Process.GetProcesses("DeviceEmulator"); 
      foreach (var process in processes) 
      { 
       if (process.ProcessName == "DeviceEmulator") 
       { 
        var handle = process.MainWindowHandle; 
        SetWindowPos(handle, 0, 0, 0, -100, -100, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE); 
       } 
      } 
     } 
    } 
} 
+0

你的代码没有意义,你试图给窗口一个负面的大小。这当然不可能。然后指定SWP_NOSIZE告诉SetWindowPos()它不应该改变大小。改变位置。不要通过(0,0),将它放在屏幕的左上角,按照定义,不要部分地离开屏幕。 – 2013-02-22 02:05:14

+0

谢谢,这至少解决了一个谜团! – user2097402 2013-02-22 10:19:24

回答

0
  1. 移动的常量类的范围内,并添加public修改它们。

  2. Logic

  3. 将它保存在一个文件.cs

  4. 删除Main()将文件添加到您的项目。

  5. 假设你的主窗体类是Form1.cs,在顶部

    using WindowMover;

  6. 双击Form1在窗体设计器中添加一条线,这将增加一个Load事件处理程序,为您和进入代码编辑模式

  7. 呼叫SetWindowPosthis.Handle,和后面的参数都取决于你的需求