SetWindowPos在屏幕外面

时间:2013-02-21 22:46:38

标签: c#

我只对c#有点了解,而且我的大多数代码都是关于google的。我正在尝试的是移动一个窗口,我使用SetWindowPos来完成这项工作。但是,我希望窗口部分在屏幕外,但Microsoft Windows不允许它。下面的代码是我到目前为止所得到的。当然我在google上搜索了答案,并找到了答案,我唯一不知道的是如何把它放到我的程序中。

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

Google回答: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);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

  1. 将常量移动到类范围,并为它们添加public修饰符。

  2. Main()

  3. 中删除Logic
  4. 将其保存在.cs文件

  5. 将文件添加到项目中。

  6. 假设您的主要表单类为Form1.cs,请在顶部添加一行

    using WindowMover;

  7. 双击表单设计器中的Form1,它会为您添加Load事件处理程序并进入代码编辑模式

  8. 使用SetWindowPos致电this.Handle,后面的参数取决于您的要求

相关问题