c# 中如何根据进程名获得指定的窗体在屏幕中Location
发布网友
发布时间:2024-09-30 02:58
我来回答
共1个回答
热心网友
时间:2024-10-12 08:04
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using WINDOWINFO = WindowsFormsApplication1.tagWINDOWINFO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);
private void button1_Click(object sender, EventArgs e)
{
Process [] ps = Process.GetProcessesByName("Config");
foreach (var p in ps)
{
IntPtr handle = p.MainWindowHandle;
WINDOWINFO info = new WINDOWINFO();
info.cbSize = (uint)Marshal.SizeOf(info);
GetWindowInfo(handle, ref info);
MessageBox.Show(info.rcWindow.top.ToString() + ":" +
info.rcWindow.left.ToString());
}
}
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct tagWINDOWINFO
{
/// DWORD->unsigned int
public uint cbSize;
/// RECT->tagRECT
public tagRECT rcWindow;
/// RECT->tagRECT
public tagRECT rcClient;
/// DWORD->unsigned int
public uint dwStyle;
/// DWORD->unsigned int
public uint dwExStyle;
/// DWORD->unsigned int
public uint dwWindowStatus;
/// UINT->unsigned int
public uint cxWindowBorders;
/// UINT->unsigned int
public uint cyWindowBorders;
/// ATOM->WORD->unsigned short
public ushort atomWindowType;
/// WORD->unsigned short
public ushort wCreatorVersion;
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct tagRECT
{
/// LONG->int
public int left;
/// LONG->int
public int top;
/// LONG->int
public int right;
/// LONG->int
public int bottom;
}
}
MessageBox显示的就是location ,我输出的可能X,Y反了,但都一样