0

我对 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);
                }
            }
        }
    }
}
4

1 回答 1

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

  2. Main()Logic班级中删除

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

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

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

    using WindowMover;

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

  7. SetWindowPos用调用this.Handle,后面的参数取决于你的要求

于 2013-02-21T23:18:46.063 回答