user32 MoveWindow在C#、windows 7、控制台应用程序中不起作用
我根本不是windows开发人员(我做的是3类东西),但我用visual C#2010编写了这个C#控制台应用程序来测试一些东西。 应用程序应该打开一个窗口,调整大小并重新定位 我打开了一个空白的Chrome窗口(标题为“Untitled”),但控制窗口的函数不起作用(即使调试器停止在它们上面——这意味着应用程序确实找到了正确的窗口) 你知道为什么吗user32 MoveWindow在C#、windows 7、控制台应用程序中不起作用,c#,window,console-application,user32,C#,Window,Console Application,User32,我根本不是windows开发人员(我做的是3类东西),但我用visual C#2010编写了这个C#控制台应用程序来测试一些东西。 应用程序应该打开一个窗口,调整大小并重新定位 我打开了一个空白的Chrome窗口(标题为“Untitled”),但控制窗口的函数不起作用(即使调试器停止在它们上面——这意味着应用程序确实找到了正确的窗口) 你知道为什么吗 using System; using System.Diagnostics; using System.Runtime.InteropServi
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hwnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
static void Main(string[] args)
{
Process[] processlist = Process.GetProcesses();
foreach (Process proc in processlist)
{
if (!String.IsNullOrEmpty(proc.MainWindowTitle) && proc.MainWindowTitle == "Untitled")
{
ShowWindow(proc.Handle, 3);
MoveWindow(proc.Handle, 0, 0, 100, 100, true);
}
}
}
}
}
进程句柄不是你想象的那样。您感兴趣的是进程句柄,而不是process.MainWindowHandle
你问这个问题是因为你没有检查错误。所以你不知道为什么它不起作用。这些函数返回bool,false表示您搞错了。引发Win32Exception,因此这不是完全不可诊断的:
if (!MoveWindow(proc.MainWindowHandle, ...)) {
throw new Win32Exception();
}
if (!MoveWindow(proc.MainWindowHandle, ...)) {
throw new Win32Exception();
}