程序代码:/*
* Copyright (c) bccn.net ZhouFeng
*/
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace MoveWindow
{
public partial class FrmMoveWnd : Form
{
//
// Windows API: MoveWindow
//
// hwnd: Handle to the window.
// x: Specifies the new position of the left side of the window.
// y: Specifies the new position of the top of the window.
// width: Specifies the new width of the window.
// height: Specifies the new height of the window
// repaint:Specifies whether the window is to be repainted
[DllImport("User32.dll")]
private extern static int MoveWindow(int hwnd, int x, int y, int width, int height, int repaint);
private bool bIsMoving;
private Point mouseDownPoint;
private int xOffset, yOffset;
public FrmMoveWnd()
{
InitializeComponent();
this.bIsMoving = false;
this.mouseDownPoint = Point.Empty;
this.Paint += new PaintEventHandler(FrmMoveWnd_Paint);
// Mouse hanlding
this.MouseDown += new MouseEventHandler(FrmMoveWnd_MouseDown);
this.MouseUp += new MouseEventHandler(FrmMoveWnd_MouseUp);
this.MouseMove += new MouseEventHandler(FrmMoveWnd_MouseMove);
}
private void FrmMoveWnd_MouseMove(object sender, MouseEventArgs e)
{
if (this.bIsMoving)
{
xOffset = e.X - this.mouseDownPoint.X;
yOffset = e.Y - this.mouseDownPoint.Y;
MoveWindow((int)this.Handle, this.Left + xOffset, this.Top + yOffset,
this.Width, this.Height, 1);
}
}
private void FrmMoveWnd_MouseUp(object sender, MouseEventArgs e)
{
this.bIsMoving = false;
}
private void FrmMoveWnd_MouseDown(object sender, MouseEventArgs e)
{
this.bIsMoving = true;
this.mouseDownPoint.X = e.X;
this.mouseDownPoint.Y = e.Y;
}
private void FrmMoveWnd_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Font bigFont = new Font(this.Font.FontFamily, this.Font.Size * 2);
g.DrawString("Drag window to position.", bigFont, Brushes.DarkBlue, this.ClientRectangle, sf);
}
}
}你好,你看看上面写的是不是你所要的功能。
其实一个API就搞定了。








