SetWindowPos在屏幕外

本文关键字:屏幕 SetWindowPos | 更新日期: 2023-09-27 18:00:41

我对c#只了解一点点,我的大部分代码都不在谷歌上。我正在尝试移动一个窗口,我通过使用SetWindowPos实现了这一点。然而,我希望窗口特别在屏幕外,但Microsoft Windows不允许。下面的代码是我目前为止得到的。当然,我在谷歌上搜索了答案,找到了答案,唯一我不知道的是如何将其放入我的程序中。

有人能解释一下我该如何把它放进我的申请表中吗?

谷歌回答:将一个窗口从屏幕顶部部分移开

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);
                }
            }
        }
    }
}

SetWindowPos在屏幕外

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

  2. Logic类中删除Main()

  3. 将其保存在.cs文件中

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

  5. 假设您的主表单类是Form1.cs,在顶部添加一行

    using WindowMover;

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

  7. this.Handle调用SetWindowPos,后一个参数取决于您的要求