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

解决方案 »

  1.   

      long p = SetWindowPos(handle, 0, 0, rc.bottom - rc.top, 100, 200, 0x0040);// rc.right - rc.left, 0, 0);
    你的100,200设置了
    cx:以像素指定窗口的新的宽度。
    cy:以像素指定窗口的新的高度。可以参考http://www.cnblogs.com/mugua/archive/2009/03/17/1414560.html
      

  2.   

    为什么要用Windows API呢? WinForm自己也可以实现这些啊。
      

  3.   


                this.Top = 0;
                this.Left = 0;
      

  4.   

       SWP_NOSIZE:维持当前尺寸(忽略cx和Cy参数)。
      

  5.   

    我是要枚举桌面上全部窗口(同时运行多个有窗口的程序),全部保持原大小,移到(0,0)。LCL_data:
    “你的100,200设置了”
    我无论怎么改100,200,如换做99,400,窗口移到左上角后宽度大小都是变窄。
    sunny906:
    每个窗口得到的是句柄,怎么用:
     this.Top = 0;
     this.Left = 0;
    呢?
      

  6.   

    我是要枚举桌面上全部窗口(同时运行多个有窗口的程序),全部保持原大小,移到(0,0)。LCL_data:
    “你的100,200设置了”
    我无论怎么改100,200,如换做99,400,窗口移到左上角后宽度大小都是变窄。
    sunny906:
    每个窗口得到的是句柄,怎么用:
     this.Top = 0;
     this.Left = 0;
    呢?