日期:2014-05-18  浏览次数:20777 次

关于SetWindowPos函数的使用问题
我现在希望调用系统的鼠标属性窗口,让这个窗口每次显示的位置为我设置的位置,我用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函数没有用。所以请高手指点我应该怎样做才能设置窗口弹出的位置呢?

------解决方案--------------------
C# code
        [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);

        [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);             
        }