日期:2009-12-23  浏览次数:20523 次

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();
        }

        const int GW_HWNDFIRST = 0; //{同级别 Z 序最上}
        const int GW_HWNDLAST = 1; //{同级别 Z 序最下}
        const int GW_HWNDNEXT = 2; //{同级别 Z 序之下}
        const int GW_HWNDPREV = 3; //{同级别 Z 序之上}
        const int GW_OWNER = 4; //{属主窗口}
        const int GW_CHILD = 5; //{子窗口中的最上}

        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        public static extern int FindWindow(
            string lpClassName,
            string lpWindowName
        );

        [DllImport("user32.dll", EntryPoint = "GetWindow")]//获取窗体句柄,hwnd为源窗口句柄
        /*wCmd指定结果窗口与源窗口的关系,它们建立在下述常数基础上:
              GW_CHILD
              寻找源窗口的第一个子窗口
              GW_HWNDFIRST
              为一个源子窗口寻找第一个兄弟(同级)窗口,或寻找第一个顶级窗口
              GW_HWNDLAST
              为一个源子窗口寻找最后一个兄弟(同级)窗口,或寻找最后一个顶级窗口
              GW_HWNDNEXT
              为源窗口寻找下一个兄弟窗口
              GW_HWNDPREV
              为源窗口寻找前一个兄弟窗口
              GW_OWNER
              寻找窗口的所有者
         */
        public static extern int GetWindow(
            int hwnd,
            int wCmd
        );

        [DllImport("user32.dll", EntryPoint = "SetParent")]//设置父窗体
        public static extern int SetParent(
            int hWndChild,
            int hWndNewParent
        );