gpt4 book ai didi

c# - 如何将我的表单粘贴到第三方应用程序的窗口?

转载 作者:行者123 更新时间:2023-11-30 21:55:54 25 4
gpt4 key购买 nike

我正在尝试将我的表单粘贴到另一个应用程序(比方说 Microsoft Outlook)的窗口中。当我移动 Outlook 窗口时,我的表单应该仍然停留在它的右侧。

目前,我正在监视 Outlook 在 while(true) 循环(使用 sleep())中的位置,并根据它调整我的表单位置。

这里有两个问题:

  • 如果 sleep() 持续时间太短,则检查 Outlook 的位置和经常调整我的表格需要很多性能。
  • 如果 sleep() 持续时间太长,我的表单适应 Outlook 的速度太慢(它滞后)。

没有针对此的 native 解决方案吗?

最佳答案

你必须了解进程并监听事件

这应该给你一个很好的起点

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private const uint WINEVENT_OUTOFCONTEXT = 0x0000;
private const uint EVENT_OBJECT_LOCATIONCHANGE = 0x800B;

private const uint EVENT_SYSTEM_MOVESIZESTART = 0x000A;
private const uint EVENT_SYSTEM_MOVESIZEEND = 0x000B;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.Width = 100;
this.Height = 100;
this.TopMost = true;
int processId = Process.GetProcessesByName("OUTLOOK")[0].Id;

//this will also be triggered by mouse moving over the process windows
//NativeMethods.SetWinEventHook(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE, IntPtr.Zero, WinEventProc, (uint)processId, (uint)0, WINEVENT_OUTOFCONTEXT);

NativeMethods.SetWinEventHook(EVENT_SYSTEM_MOVESIZESTART, EVENT_SYSTEM_MOVESIZEEND, IntPtr.Zero, WinEventProc, (uint)processId, (uint)0, WINEVENT_OUTOFCONTEXT);
}

private void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
Rect move = new Rect();

if (eventType == EVENT_SYSTEM_MOVESIZESTART)
{
NativeMethods.GetWindowRect(hwnd, ref move);

Debug.WriteLine("EVENT_SYSTEM_MOVESIZESTART");
}
else if (eventType == EVENT_SYSTEM_MOVESIZEEND)
{
NativeMethods.GetWindowRect(hwnd, ref move);

Debug.WriteLine("EVENT_SYSTEM_MOVESIZEEND");
}

this.Left = move.Left;
this.Top = move.Top;
}
}

public struct Rect
{
public int Left { get; set; }
public int Top { get; set; }
public int Right { get; set; }
public int Bottom { get; set; }
}

static class NativeMethods
{
[DllImport("user32.dll")]
public static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);

public delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);

[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
}
}

关于c# - 如何将我的表单粘贴到第三方应用程序的窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31832433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com