using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
public delegate bool EnumWindowCallBack(IntPtr hwnd, IntPtr lParam);
namespace WindowsApplication14
{
/// <summary>
/// Form1 の概要の説明です。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.ColumnHeader columnHeader4;
private System.Windows.Forms.Button button6;
/// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
}
/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows フォーム デザイナで生成されたコード
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
this.button6 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(144, 23);
this.button1.TabIndex = 0;
this.button1.Text = "デスクトップウィンドウサイズ";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(16, 48);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(144, 23);
this.button2.TabIndex = 1;
this.button2.Text = "メモ帳を起動する";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(184, 48);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(144, 23);
this.button3.TabIndex = 2;
this.button3.Text = "メモ帳を移動する";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(16, 88);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(144, 23);
this.button4.TabIndex = 3;
this.button4.Text = "EnumWindows";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(184, 88);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(144, 23);
this.button5.TabIndex = 4;
this.button5.Text = "移動テスト";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(184, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(280, 19);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "";
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3,
this.columnHeader4});
this.listView1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.listView1.Location = new System.Drawing.Point(0, 158);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(512, 224);
this.listView1.TabIndex = 6;
this.listView1.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
this.columnHeader1.Text = "hWnd";
this.columnHeader1.Width = 71;
//
// columnHeader2
//
this.columnHeader2.Text = "Parent";
this.columnHeader2.Width = 76;
//
// columnHeader3
//
this.columnHeader3.Text = "ClassName";
this.columnHeader3.Width = 132;
//
// columnHeader4
//
this.columnHeader4.Text = "WindowName";
this.columnHeader4.Width = 223;
//
// button6
//
this.button6.Location = new System.Drawing.Point(16, 128);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(144, 23);
this.button6.TabIndex = 7;
this.button6.Text = "IEを整列";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(512, 382);
this.Controls.Add(this.button6);
this.Controls.Add(this.listView1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
// BOOL MoveWindow(
// HWND hWnd, // ウィンドウのハンドル
// int X, // 横方向の位置
// int Y, // 縦方向の位置
// int nWidth, // 幅
// int nHeight, // 高さ
// BOOL bRepaint // 再描画オプション
// );
[DllImport("User32.dll")]
static extern int MoveWindow(
IntPtr hWnd,
int x,
int y,
int nWidth,
int nHeight,
int bRepaint
);
// HWND FindWindow(
// LPCTSTR lpClassName, // クラス名
// LPCTSTR lpWindowName // ウィンドウ名
// );
// Unicode:Windows NT/2000 は Unicode 版と ANSI 版を実装
[DllImport("User32.dll", CharSet = CharSet.Unicode)]
static extern IntPtr FindWindow(
string lpszClass,
string lpszWindow
);
// BOOL SetForegroundWindow(
// HWND hWnd // ウィンドウのハンドル
// );
[DllImport("User32.dll")]
static extern int SetForegroundWindow(
IntPtr hWnd
);
// int GetClassName(
// HWND hWnd, // ウィンドウのハンドル
// LPTSTR lpClassName, // クラス名
// int nMaxCount // クラス名バッファのサイズ
// );
// Unicode:Windows NT/2000 は Unicode 版と ANSI 版を実装
[DllImport("User32.Dll", CharSet = CharSet.Unicode)]
public static extern int GetClassName(
IntPtr hWnd,
StringBuilder s,
int nMaxCount
);
// int GetWindowText(
// HWND hWnd, // ウィンドウまたはコントロールのハンドル
// LPTSTR lpString, // テキストバッファ
// int nMaxCount // コピーする最大文字数
// );
// Unicode:Windows NT/2000 は Unicode 版と ANSI 版を実装
[DllImport("User32.Dll", CharSet = CharSet.Unicode)]
static extern int GetWindowText(
IntPtr hWnd,
StringBuilder s,
int nMaxCount
);
// HWND GetParent(
// HWND hWnd // 子ウィンドウのハンドル
// );
[DllImport("User32.Dll")]
static extern int GetParent(
IntPtr hWnd
);
// BOOL IsWindowVisible(
// HWND hWnd // ウィンドウのハンドル
// );
[DllImport("User32.Dll")]
static extern int IsWindowVisible(
IntPtr hWnd
);
// BOOL IsChild(
// HWND hWndParent, // 親ウィンドウのハンドル
// HWND hWnd // 調査するウィンドウのハンドル
// );
[DllImport("User32.Dll")]
static extern int IsChild(
IntPtr hWnd
);
// BOOL ShowWindow(
// HWND hWnd, // ウィンドウのハンドル
// int nCmdShow // 表示状態
// );
const int SW_HIDE = 0;
const int SW_SHOWNORMAL = 1;
const int SW_NORMAL = 1;
const int SW_SHOWMINIMIZED = 2;
const int SW_SHOWMAXIMIZED = 3;
const int SW_MAXIMIZE = 3;
const int SW_SHOWNOACTIVATE = 4;
const int SW_SHOW = 5;
const int SW_MINIMIZE = 6;
const int SW_SHOWMINNOACTIVE = 7;
const int SW_SHOWNA = 8;
const int SW_RESTORE = 9;
const int SW_SHOWDEFAULT = 10;
const int SW_FORCEMINIMIZE = 11;
const int SW_MAX = 11;
[DllImport("User32.Dll")]
static extern int ShowWindow(
IntPtr hWnd,
int nCmdShow
);
// HWND GetDesktopWindow(VOID);
[DllImport("User32.Dll")]
static extern IntPtr GetDesktopWindow();
// typedef struct _RECT
// {
// LONG left;
// LONG top;
// LONG right;
// LONG bottom;
// } RECT, *PRECT;
[StructLayout(LayoutKind.Sequential, Pack = 4)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
// BOOL GetWindowRect(
// HWND hWnd, // ウィンドウのハンドル
// LPRECT lpRect // ウィンドウの座標値
// );
[DllImport("User32.Dll")]
static extern int GetWindowRect(
IntPtr hWnd, // ウィンドウのハンドル
out RECT rect // ウィンドウの座標値
);
[DllImport("user32.Dll")]
static extern int EnumWindows(EnumWindowCallBack x, IntPtr y);
public static bool ListViewCallBack(IntPtr hwnd, IntPtr lParam)
{
ListView lview = (ListView)FromHandle(lParam);
StringBuilder sbClassName = new StringBuilder(256);
StringBuilder sbWindowText = new StringBuilder(256);
string hasParent = "";
if (GetParent(hwnd) > 0)
hasParent = "Has Parent";
else
hasParent = "";
GetClassName(hwnd, sbClassName, sbClassName.Capacity);
GetWindowText(hwnd, sbWindowText, sbWindowText.Capacity);
if( sbClassName.Length > 0 && IsWindowVisible(hwnd) > 0)
{
ListViewItem item = lview.Items.Add(hwnd.ToString());
item.SubItems.Add(hasParent);
item.SubItems.Add(sbClassName.ToString());
item.SubItems.Add(sbWindowText.ToString());
}
return true;
}
private void button1_Click(object sender, System.EventArgs e)
{
RECT rect;
GetWindowRect(GetDesktopWindow(), out rect);
this.textBox1.Text =
rect.left.ToString() + ","
+ rect.top.ToString() + ","
+ rect.left.ToString() + ","
+ rect.bottom.ToString() ;
}
private void button2_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process.Start("Notepad.exe");
}
private void button3_Click(object sender, System.EventArgs e)
{
IntPtr h = FindWindow("Notepad", null);
MoveWindow(h, 10, 10, 300, 200, 1);
SetForegroundWindow(h);
}
private void button4_Click(object sender, System.EventArgs e)
{
EnumWindows(new EnumWindowCallBack(Form1.ListViewCallBack), this.listView1.Handle);
}
private void button5_Click(object sender, System.EventArgs e)
{
int index = this.listView1.SelectedIndices[0];
int h = Int32.Parse(this.listView1.Items[index].Text);
MoveWindow((IntPtr)h, 10, 10, 300, 200, 1);
ShowWindow((IntPtr)h, SW_RESTORE);
}
public static bool IECallBack(IntPtr hwnd, IntPtr lParam)
{
StringBuilder sbClassName = new StringBuilder(256);
GetClassName(hwnd, sbClassName, sbClassName.Capacity);
if( sbClassName.ToString().StartsWith("IEFrame"))
{
Form1.al.Add(hwnd);
}
return true;
}
static public ArrayList al = null;
private void button6_Click(object sender, System.EventArgs e)
{
al = new ArrayList();
// デスクトップウィンドウのサイズを求める。
RECT rect;
GetWindowRect(GetDesktopWindow(), out rect);
// EnumWindows でIECallBack へさせる。
EnumWindows(new EnumWindowCallBack(Form1.IECallBack), IntPtr.Zero);
// デスクトップウィンドウの縦サイズをIEの数で割る。
int hw = rect.bottom / al.Count;
// IE を整列させる。
for(int i = 0; i < al.Count; i++)
{
SetForegroundWindow((IntPtr)al[i]);
ShowWindow((IntPtr)al[i], SW_SHOWNORMAL);
MoveWindow((IntPtr)al[i], 0, i*hw, rect.right/3, hw, 1);
}
}
}
}
|