想把所有窗口移到左上角,循环用下面语句:long p = SetWindowPos(handle, 0, 0, 0,0,0, 0x0001);//把此窗口移到左上角(0,0)结果却全缩最小,后移到左上角,请问错在哪?

解决方案 »

  1.   

    为什么不用MoveWindow(0,0,200,200,true);
      

  2.   

    我是遍历所有窗口,得到其句柄,再不改变窗口大小移动。
    用MoveWindow(0,0,200,200,true);怎么知道是移动哪个窗口呢?它是否改变了窗口大小为200*200?
      

  3.   

    不好意思,刚才拷贝了一个MFC的函数给你,试试这个
    long p = SetWindowPos(handle, 0, 0, 0,0,0, SWP_NOZORDER | SWP_NOSIZE );
    #define SWP_NOZORDER        0x0004
    #define SWP_NOSIZE          0x0001
      

  4.   

    SetWindowPos(handle, 0, 0, 0,0,0, 0x0005);
      

  5.   

    用 SetWindowPos(handle, 0, 0, 0,0,0, 0x0005);仍在左上角缩成1小点。
    用 SetWindowPos(handle, 0, 0,rc.bottom - rc.top, 0, 0, 0x0005);在左上角,上下长正确,但左右宽仍缩成1小点。
      

  6.   

    怎样上传rar文件呀?我想发源文件请大家测试下。
      

  7.   

    rc是当前窗口长宽结构。
    怀疑SetWindowPos()的参数5,6,7没起作用。
      

  8.   

    我用VC++和C#测试,均正常,你检查一下你的代码吧???
    ::SetWindowPos(this->GetSafeHwnd(), 0, 0, 0,0,0, 0x0005 );
    SetWindowPos(var.Handle, 0, 0, 0, 0, 0, 0x0005);//我同时移动了三个Form也正常
      

  9.   

    我的代码using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;using System.Runtime.InteropServices;namespace showAllWin
    {
      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      }  [DllImport("user32.dll", CharSet = CharSet.Auto)]
      private static extern int GetWindowText(HandleRef hWnd, StringBuilder lpString, int nMaxCount);
      [DllImport("user32.dll", CharSet = CharSet.Auto)]
      private static extern int GetWindowTextLength(HandleRef hWnd);
      [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
      private static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
      [DllImport("user32.dll", ExactSpelling = true)]
      private static extern bool EnumChildWindows(HandleRef hwndParent, EnumChildrenCallback lpEnumFunc, HandleRef lParam);
      private delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);
      private delegate bool EnumChildrenCallback(IntPtr hwnd, IntPtr lParam);  [DllImport("user32.dll")] //EnumWindows函数,EnumWindowsProc 为处理函数
      private static extern int EnumWindows(EnumWindowsProc ewp, int lParam);     //其他常用函数格式如下:
      [DllImport("user32.dll")]
      private static extern int GetWindowText(IntPtr hWnd, StringBuilder title, int size);
      [DllImport("user32.dll")]
      private static extern bool IsWindowVisible(IntPtr hWnd);
      [DllImport("user32.dll")]
      private static extern int GetWindowTextLength(IntPtr hWnd);
      [DllImport("USER32.DLL")]
      private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
      [DllImport("USER32.DLL")]
      private static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
      public delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
      [StructLayout(LayoutKind.Sequential)]
      public struct RECT
      {
      public int left;
      public int top;
      public int right;
      public int bottom;
      }
      [DllImport("user32.dll")]
      private static extern IntPtr GetForegroundWindow();
      [DllImport("user32.dll")]
      public static extern int GetWindowRect(IntPtr hWnd, out RECT lpRect);
      [DllImport("user32.dll")]
      public static extern int GetClientRect(IntPtr hWnd, out RECT lpRect);
      [DllImport("user32")]
      public static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long uFlags);
      private void button1_Click(object sender, EventArgs e)
      {
      listBox1.Items.Clear();  EnumThreadWindowsCallback callback1 = new EnumThreadWindowsCallback(this.EnumWindowsCallback);
      EnumWindows(callback1, IntPtr.Zero);
      }
      private bool EnumWindowsCallback(IntPtr handle, IntPtr extraParameter)
      {
      if (!IsWindowVisible(handle))
      { return true; }
      int num1 = GetWindowTextLength(new HandleRef(this, handle)) * 2;
      StringBuilder builder1 = new StringBuilder(num1);
      GetWindowText(new HandleRef(this, handle), builder1, builder1.Capacity);
      System.Console.WriteLine(string.Format("Wnd:{0} Title: {1}", handle, builder1.ToString()));
      Application.DoEvents();
      listBox1.Items.Add(string.Format("Wnd:{0} Title: {1}", handle, builder1.ToString()));  RECT rc;
      int i=GetWindowRect(handle, out rc);  //long p = SetWindowPos(handle, 0, 0,rc.bottom - rc.top, 100, 200, 0);
      long p = SetWindowPos(handle, 0, 0, rc.bottom - rc.top, 100, 200, 0x0040);// rc.right - rc.left, 0, 0);  EnumChildWindows(new HandleRef(this, handle), new EnumChildrenCallback(EnumChildWindowsCallback), new HandleRef(null, IntPtr.Zero));
      return true;
      }
      private bool EnumChildWindowsCallback(IntPtr handle, IntPtr lparam)
      {
      int num1 = GetWindowTextLength(new HandleRef(this, handle)) * 2;
      StringBuilder builder1 = new StringBuilder(num1);
      GetWindowText(new HandleRef(this, handle), builder1, builder1.Capacity);
      listBox1.Items.Add(string.Format("\tSubWnd:{0} Title: {1}", handle, builder1.ToString()));
      return true;
      }  }
    }
      

  10.   

    在窗体的resize事件里再调用该函数
      

  11.   

    将定义替换如下:我已经测试成功,所有窗口移至左上角,大小没有改变
    问题出在最后一个参数long和uint不是一个类型,虽然位数一样[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
            public static extern bool SetWindowPos(
                IntPtr hWnd,               // window handle      
                int hWndInsertAfter,    // placement-order handle      
                int X,                  // horizontal position      
                int Y,                  // vertical position      
                int cx,                 // width      
                int cy,                 // height      
                uint uFlags);           // window positioning flags   以下是全部代码:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;using System.Runtime.InteropServices;namespace WindowsApplication2
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        [DllImport("user32.dll", CharSet = CharSet.Auto)]
            private static extern int GetWindowText(HandleRef hWnd, StringBuilder lpString, int nMaxCount);
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            private static extern int GetWindowTextLength(HandleRef hWnd);
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
            [DllImport("user32.dll", ExactSpelling = true)]
            private static extern bool EnumChildWindows(HandleRef hwndParent, EnumChildrenCallback lpEnumFunc, HandleRef lParam);
            private delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);
            private delegate bool EnumChildrenCallback(IntPtr hwnd, IntPtr lParam);        [DllImport("user32.dll")] //EnumWindows函数,EnumWindowsProc 为处理函数
            private static extern int EnumWindows(EnumWindowsProc ewp, int lParam);        //其他常用函数格式如下:
            [DllImport("user32.dll")]
            private static extern int GetWindowText(IntPtr hWnd, StringBuilder title, int size);
            [DllImport("user32.dll")]
            private static extern bool IsWindowVisible(IntPtr hWnd);
            [DllImport("user32.dll")]
            private static extern int GetWindowTextLength(IntPtr hWnd);
            [DllImport("USER32.DLL")]
            private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
            [DllImport("USER32.DLL")]
            private static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
            public delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int left;
                public int top;
                public int right;
                public int bottom;
            }        [DllImport("user32.dll")]
            private static extern IntPtr GetForegroundWindow();
            [DllImport("user32.dll")]
            public static extern int GetWindowRect(IntPtr hWnd, out RECT lpRect);
            [DllImport("user32.dll")]
            public static extern int GetClientRect(IntPtr hWnd, out RECT lpRect);
            //[DllImport("user32")]
            //public static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long uFlags);
            [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
            public static extern bool SetWindowPos(
                IntPtr hWnd,               // window handle      
                int hWndInsertAfter,    // placement-order handle      
                int X,                  // horizontal position      
                int Y,                  // vertical position      
                int cx,                 // width      
                int cy,                 // height      
                uint uFlags);           // window positioning flags           /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                //listBox1.Items.Clear();            EnumThreadWindowsCallback callback1 = new EnumThreadWindowsCallback(this.EnumWindowsCallback);
                EnumWindows(callback1, IntPtr.Zero);
            }        /// <summary>
            /// 
            /// </summary>
            /// <param name="handle"></param>
            /// <param name="extraParameter"></param>
            /// <returns></returns>
            private bool EnumWindowsCallback(IntPtr handle, IntPtr extraParameter)
            {
                if (IsWindowVisible(handle))
                {
                    int num1 = GetWindowTextLength(new HandleRef(this, handle)) * 2;
                    StringBuilder builder1 = new StringBuilder(num1);
                    GetWindowText(new HandleRef(this, handle), builder1, builder1.Capacity);
                    System.Console.WriteLine(string.Format("Wnd:{0} Title: {1}", handle, builder1.ToString()));
                    //Application.DoEvents();
                    //listBox1.Items.Add(string.Format("Wnd:{0} Title: {1}", handle, builder1.ToString()));                RECT rc;
                    int i = GetWindowRect(handle, out rc);                SetWindowPos(handle, 0, 0, 0, 0, 0, 0x0005);
                    //long p = SetWindowPos(handle, 0, 0, rc.bottom - rc.top, 100, 200, 0x0040);// rc.right - rc.left, 0, 0);                EnumChildWindows(new HandleRef(this, handle), new EnumChildrenCallback(EnumChildWindowsCallback), new HandleRef(null, IntPtr.Zero));
                }
                return true;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="handle"></param>
            /// <param name="lparam"></param>
            /// <returns></returns>        private bool EnumChildWindowsCallback(IntPtr handle, IntPtr lparam)
            {
                int num1 = GetWindowTextLength(new HandleRef(this, handle)) * 2;
                StringBuilder builder1 = new StringBuilder(num1);
                GetWindowText(new HandleRef(this, handle), builder1, builder1.Capacity);
                //listBox1.Items.Add(string.Format("\tSubWnd:{0} Title: {1}", handle, builder1.ToString()));
                return true;
            }        /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Form2_Load(object sender, EventArgs e)
            {
                Form1 ff = new Form1(); ff.Show();
            }    }
    }
      

  12.   

    将定义替换如下:我已经测试成功,所有窗口移至左上角,大小没有改变
    问题出在最后一个参数long和uint不是一个类型,虽然位数一样[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
            public static extern bool SetWindowPos(
                IntPtr hWnd,               // window handle      
                int hWndInsertAfter,    // placement-order handle      
                int X,                  // horizontal position      
                int Y,                  // vertical position      
                int cx,                 // width      
                int cy,                 // height      
                uint uFlags);           // window positioning flags   以下是全部代码:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;using System.Runtime.InteropServices;namespace WindowsApplication2
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        [DllImport("user32.dll", CharSet = CharSet.Auto)]
            private static extern int GetWindowText(HandleRef hWnd, StringBuilder lpString, int nMaxCount);
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            private static extern int GetWindowTextLength(HandleRef hWnd);
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
            [DllImport("user32.dll", ExactSpelling = true)]
            private static extern bool EnumChildWindows(HandleRef hwndParent, EnumChildrenCallback lpEnumFunc, HandleRef lParam);
            private delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);
            private delegate bool EnumChildrenCallback(IntPtr hwnd, IntPtr lParam);        [DllImport("user32.dll")] //EnumWindows函数,EnumWindowsProc 为处理函数
            private static extern int EnumWindows(EnumWindowsProc ewp, int lParam);        //其他常用函数格式如下:
            [DllImport("user32.dll")]
            private static extern int GetWindowText(IntPtr hWnd, StringBuilder title, int size);
            [DllImport("user32.dll")]
            private static extern bool IsWindowVisible(IntPtr hWnd);
            [DllImport("user32.dll")]
            private static extern int GetWindowTextLength(IntPtr hWnd);
            [DllImport("USER32.DLL")]
            private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
            [DllImport("USER32.DLL")]
            private static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
            public delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int left;
                public int top;
                public int right;
                public int bottom;
            }        [DllImport("user32.dll")]
            private static extern IntPtr GetForegroundWindow();
            [DllImport("user32.dll")]
            public static extern int GetWindowRect(IntPtr hWnd, out RECT lpRect);
            [DllImport("user32.dll")]
            public static extern int GetClientRect(IntPtr hWnd, out RECT lpRect);
            //[DllImport("user32")]
            //public static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long uFlags);
            [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
            public static extern bool SetWindowPos(
                IntPtr hWnd,               // window handle      
                int hWndInsertAfter,    // placement-order handle      
                int X,                  // horizontal position      
                int Y,                  // vertical position      
                int cx,                 // width      
                int cy,                 // height      
                uint uFlags);           // window positioning flags           /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                //listBox1.Items.Clear();            EnumThreadWindowsCallback callback1 = new EnumThreadWindowsCallback(this.EnumWindowsCallback);
                EnumWindows(callback1, IntPtr.Zero);
            }        /// <summary>
            /// 
            /// </summary>
            /// <param name="handle"></param>
            /// <param name="extraParameter"></param>
            /// <returns></returns>
            private bool EnumWindowsCallback(IntPtr handle, IntPtr extraParameter)
            {
                if (IsWindowVisible(handle))
                {
                    int num1 = GetWindowTextLength(new HandleRef(this, handle)) * 2;
                    StringBuilder builder1 = new StringBuilder(num1);
                    GetWindowText(new HandleRef(this, handle), builder1, builder1.Capacity);
                    System.Console.WriteLine(string.Format("Wnd:{0} Title: {1}", handle, builder1.ToString()));
                    //Application.DoEvents();
                    //listBox1.Items.Add(string.Format("Wnd:{0} Title: {1}", handle, builder1.ToString()));                RECT rc;
                    int i = GetWindowRect(handle, out rc);                SetWindowPos(handle, 0, 0, 0, 0, 0, 0x0005);
                    //long p = SetWindowPos(handle, 0, 0, rc.bottom - rc.top, 100, 200, 0x0040);// rc.right - rc.left, 0, 0);                EnumChildWindows(new HandleRef(this, handle), new EnumChildrenCallback(EnumChildWindowsCallback), new HandleRef(null, IntPtr.Zero));
                }
                return true;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="handle"></param>
            /// <param name="lparam"></param>
            /// <returns></returns>        private bool EnumChildWindowsCallback(IntPtr handle, IntPtr lparam)
            {
                int num1 = GetWindowTextLength(new HandleRef(this, handle)) * 2;
                StringBuilder builder1 = new StringBuilder(num1);
                GetWindowText(new HandleRef(this, handle), builder1, builder1.Capacity);
                //listBox1.Items.Add(string.Format("\tSubWnd:{0} Title: {1}", handle, builder1.ToString()));
                return true;
            }        /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Form2_Load(object sender, EventArgs e)
            {
                Form1 ff = new Form1(); ff.Show();
            }    }
    }