日期:2014-05-20 浏览次数:21470 次
From1.Location = new Point(x, y);
------解决方案--------------------
[DllImport("user32.dll",EntryPoint="MoveWindow")] public static extern int MoveWindow(int hwnd, int x, int y, int nWidth, int nHeight, int bRepaint)
------解决方案--------------------
using System; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; class Form1 : Form { Random rand = new Random(); protected override void OnPaint(PaintEventArgs e) { e.Graphics.DrawString ( "单击窗体客户区,\r\n则窗体会随机移动\r\n并改变大小,\r\n很有趣吧!", new Font("Arial", 16), new SolidBrush(Color.Blue), 0, 0 ); base.OnPaint(e); } [DllImport("user32.dll",EntryPoint="MoveWindow")] public static extern int MoveWindow(IntPtr hwnd, int x, int y, int nWidth, int nHeight, bool bRepaint); // 这是使用 API 的版本 protected override void OnClick(EventArgs e) { int x = rand.Next(800); int y = rand.Next(600); int nWidth = rand.Next(50, 500); int nHeight = rand.Next(50, 500); MoveWindow(Handle, x, y, nWidth, nHeight, true); base.OnClick(e); } /* // 这是不使用 API 的版本 protected override void OnClick(EventArgs e) { Left = rand.Next(800); Top = rand.Next(600); Width = rand.Next(50, 500); Height = rand.Next(50, 500); base.OnClick(e); } */ static void Main() { Application.Run(new Form1()); } }