我现在希望调用系统的鼠标属性窗口,让这个窗口每次显示的位置为我设置的位置,我用SetWindowPos函数设置它的显示位置,但是发现这个函数没有起到作用,代码如下:[DllImport("User32.dll", CharSet = CharSet.Auto)]
private static extern int SetWindowPos(IntPtr win_handle, IntPtr win_handle_insert_after, int x, int y, int width, int height, uint flags);ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "rundll32.exe";
psi.Arguments = "shell32.dll, Control_RunDLL main.cpl";
Process proc = Process.Start(psi);
SetWindowPos(proc.MainWindowHandle, new IntPtr(-1), 500, 500, 0, 0, 3);
proc.WaitForExit();代码执行过程中,我发现SetWindowPos函数根本没起到作用,弹出的窗口并不是出现在(500,500)的位置,后来debug发现,当Process.Start(psi); 语句执行过后,窗口就已经弹出来了,SetWindowPos函数没有用。所以请高手指点我应该怎样做才能设置窗口弹出的位置呢?
private static extern int SetWindowPos(IntPtr win_handle, IntPtr win_handle_insert_after, int x, int y, int width, int height, uint flags);ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "rundll32.exe";
psi.Arguments = "shell32.dll, Control_RunDLL main.cpl";
Process proc = Process.Start(psi);
SetWindowPos(proc.MainWindowHandle, new IntPtr(-1), 500, 500, 0, 0, 3);
proc.WaitForExit();代码执行过程中,我发现SetWindowPos函数根本没起到作用,弹出的窗口并不是出现在(500,500)的位置,后来debug发现,当Process.Start(psi); 语句执行过后,窗口就已经弹出来了,SetWindowPos函数没有用。所以请高手指点我应该怎样做才能设置窗口弹出的位置呢?
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "rundll32.exe";
psi.Arguments = "shell32.dll, Control_RunDLL main.cpl";
Process proc = Process.Start(psi);
MoveWindow(proc.MainWindowHandle, 500, 500, 0, 0, false);
proc.WaitForExit();
}
你说错反了,是因为Start后窗口没有马上出来
所以MainWindowHandle为0,所以MoveWindow当然不行了
你在Start后面加个Sleep(1000)
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "rundll32.exe";
psi.Arguments = "shell32.dll, Control_RunDLL main.cpl";
Process proc = Process.Start(psi);
Thread.Sleep(1000);
MoveWindow(proc.MainWindowHandle, 500, 500, 0, 0, false);
proc.WaitForExit();
}但是效果还是一样的,MoveWindow设置的位置没起作用,到底是哪里的原因呢?
private static extern int SetWindowPos(IntPtr win_handle, IntPtr win_handle_insert_after, int x, int y, int width, int height, uint flags); [DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName); private const int SWP_NOSIZE = 0x0001; private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "rundll32.exe";
psi.Arguments = "shell32.dll, Control_RunDLL main.cpl";
Process proc = Process.Start(psi);
Thread.Sleep(100); SetWindowPos(FindWindow("#32770", "鼠标 属性"), IntPtr.Zero, 0, 10, 0, 0, SWP_NOSIZE);
}试了一下
换回SetWindowPos吧
你原来的SetWindowPos有两个错误
1. proc.MainWindowHandle
获取关联进程主窗口的窗口句柄
主进程是rundll32,可能没有主窗体,所以就没有所谓的主窗口句柄2.SetWindowPos最后一个参数使用SWP_NOSIZE不改变大小