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;
using Microsoft.Win32;namespace 活动窗体震动
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        internal struct RECT  //Win32汇编数据结构不用自己定义
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }        [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
        internal static extern IntPtr GetForegroundWindow();        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
        internal static extern RECT GetWindowRect(IntPtr hwnd);        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
        internal static extern void MoveWindow(IntPtr hwnd,int X,int Y,int nWidth,int nHeight,bool bRepaint);
        IntPtr id;
        private void timer1_Tick(object sender, EventArgs e)
        {
            id = GetForegroundWindow();
            Random myRandom = new Random(); 
            RECT Rect = GetWindowRect(id);
            MoveWindow(id, myRandom.Next(1024), myRandom.Next(768), Rect.right-Rect.left, Rect.bottom-Rect.top, true);
        }
        
    }
}跟踪发现Rect.right-Rect.left,Rect.bottom-Rect.top算出来居然是4,4我的顶层窗体少说也有(100,60)大小了.我原来用Win32汇编成功过,不过RECT这些数据结构不用自己定义.

解决方案 »

  1.   

    1:你使用的GetWindowRect方法说实话没有见过,我觉得应该使用如下的方法:
    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    internal static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);2:你使用GetForegroundWindow获取不一定是你设置的窗口,而是指最上一次用户正使用的窗口。觉得你应该使用如下的属性:
        Form.Handle改了下你的代码并运行通过:
    internal struct RECT  //Win32汇编数据结构不用自己定义
    {
    public int left;
    public int top;
    public int right;
    public int bottom;
    }[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
    internal static extern IntPtr GetForegroundWindow();[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    internal static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
    internal static extern void MoveWindow(IntPtr hwnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
    IntPtr id;
    private void timer1_Tick(object sender, EventArgs e)
    {
    //id = GetForegroundWindow();
    id = this.Handle; RECT Rect=new RECT();
    Random myRandom = new Random();
    GetWindowRect(id, ref Rect);
    MoveWindow(id, myRandom.Next(1024), myRandom.Next(768), Rect.right - Rect.left, Rect.bottom - Rect.top, true);
    }
      

  2.   

    是我太粗心了,自以为然GetWindowRect返回的是RECT类型,居然身边就有手册还不会去查看他是布尔型.GetForegroundWindow是获得当前被激活也就是桌面最上层的窗口.如果把本程序的自己的窗口隐藏,就是个恶作剧了.