首先查看一下WindowsAPI給我們的解釋
函數功能:該函數改變指定窗口的位置和尺寸。對於頂層窗口,位置和尺寸是相對於屏幕的左上角的:對於子窗口,位置和尺寸是相對於父窗口客戶區的左上角坐標的。
函數原型:bool MoveWindow(HWND hWnd,int x,int y,int nWidth,int nHeight,bool BRePaint);
參數:
hWnd:窗口句柄。
x:指定窗口的新位置的左邊界。
Y:指定窗口的新位置的頂部邊界。
nWidth:指定窗口的新的寬度。
nHaight:指定窗口的新的高度。
所在位置:user32.dll
需要命名空間
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Runtime.InteropServices;
首先是獲取目標窗體的句柄(什么是句柄?我不知道,但是百度知道 ^_^)
///
<summary>
/// 獲取窗體句柄
/// </summary>
/// <param name="lpClassName"></param>
/// <param name="lpWindowName"></param>
/// <returns></returns>
[DllImport( " User32.dll ", EntryPoint = " FindWindow ")]
public extern static IntPtr FindWindow( string lpClassName, string lpWindowName);
/// 獲取窗體句柄
/// </summary>
/// <param name="lpClassName"></param>
/// <param name="lpWindowName"></param>
/// <returns></returns>
[DllImport( " User32.dll ", EntryPoint = " FindWindow ")]
public extern static IntPtr FindWindow( string lpClassName, string lpWindowName);
然后就是我們的主角登場啦(MoveWindow)
///
<summary>
/// 設置目標窗體大小,位置
/// </summary>
/// <param name="hWnd"> 目標句柄 </param>
/// <param name="x"> 目標窗體新位置X軸坐標 </param>
/// <param name="y"> 目標窗體新位置Y軸坐標 </param>
/// <param name="nWidth"> 目標窗體新寬度 </param>
/// <param name="nHeight"> 目標窗體新高度 </param>
/// <param name="BRePaint"> 是否刷新窗體 </param>
/// <returns></returns>
[DllImport( " user32.dll ", CharSet = CharSet.Auto)]
public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);
/// 設置目標窗體大小,位置
/// </summary>
/// <param name="hWnd"> 目標句柄 </param>
/// <param name="x"> 目標窗體新位置X軸坐標 </param>
/// <param name="y"> 目標窗體新位置Y軸坐標 </param>
/// <param name="nWidth"> 目標窗體新寬度 </param>
/// <param name="nHeight"> 目標窗體新高度 </param>
/// <param name="BRePaint"> 是否刷新窗體 </param>
/// <returns></returns>
[DllImport( " user32.dll ", CharSet = CharSet.Auto)]
public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);
這里說一下,API里面第一個參數寫的類型是HWND ,到C#里面就要用IntPtr 類表示,指針呢,據說是用委托實現(我也剛接觸,沒試過)
有了這兩段代碼,就已經完成一半了
設計窗體是這個樣子滴。。
在按鈕的Click事件里添加如下代碼
IntPtr intptr = FindWindow(
null,
"
新編WIN32API大全
");
int x = this.GetInt(txtP_x.Text.Trim(), 0);
int y = this.GetInt(txtP_y.Text.Trim(), 0);
int nWidth = this.GetInt(txtNWidth.Text.Trim(), 0);
int nHeight = this.GetInt(txtNHeight.Text.Trim(), 0);
// 調用API
MoveWindow(intptr, x, y, nWidth, nHeight, true);
int x = this.GetInt(txtP_x.Text.Trim(), 0);
int y = this.GetInt(txtP_y.Text.Trim(), 0);
int nWidth = this.GetInt(txtNWidth.Text.Trim(), 0);
int nHeight = this.GetInt(txtNHeight.Text.Trim(), 0);
// 調用API
MoveWindow(intptr, x, y, nWidth, nHeight, true);
GetInt方法是簡單處理一下非數字的字符串
/// <summary>
/// 將字string轉成int類型
/// </summary>
/// <param name="value"> 要轉換的字符串 </param>
/// <param name="_default"> 默認值 </param>
/// <returns></returns>
public int GetInt( string value, int _default)
{
if ( int.TryParse(value, out _default))
return Convert.ToInt32(value);
else
return _default;
}
大功告成!! 運行一下試試吧。哦對了,忘了說了,改一下句柄名稱再試哦~ 可以改成QQ2012之類的,我試過了,可以的。哈哈