日期:2009-12-24  浏览次数:20495 次

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;

namespace 获得当前活动窗口坐标
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //桌面分辨率
            int SH = Screen.PrimaryScreen.Bounds.Height;   //高度
            int SW = Screen.PrimaryScreen.Bounds.Width;   //宽度

            zhu1();
        }
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;   //最左坐标
            public int Top;     //最上坐标          
            public int Right;      //最右坐标     
            public int Bottom;     //最下坐标     
        }
        int X = 150;

        void zhu1()
        {
            IntPtr awin = GetForegroundWindow();
            RECT rc = new RECT();
            GetWindowRect(awin, ref rc);
            int width = rc.Right - rc.Left;   //取得当前窗口的宽度
            int height = rc.Bottom - rc.Top;//取得当前窗口的高度
            int x = rc.Left;   //取得当前窗口的与左边相距多少个相素   X轴
            int y = rc.Top;    //取得当前窗口与上面差多少个相素   Y轴
        }
    }
}