C#获得windows任务栏窗口句柄及一些操作(放大、缩小、关闭、隐藏……)

需调用API函数

需在开头引入命名空间
using System.Runtime.InteropServices;

1、通过窗口名字查找

[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lp1, string lp2);

示例:

IntPtr hWnd = FindWindow(null, "abc");

2、对窗口进行在任务栏隐藏和打开操作

[DllImport("user32.dll", EntryPoint = "ShowWindow")]
public static extern IntPtr ShowWindow(IntPtr hWnd, int _value);

0    隐藏窗口到后台

1    正常大小显示窗口

3、获取当前焦点窗口的句柄

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();

使用方法 :   IntPtr myPtr=GetForegroundWindow();

4、获取到该窗口句柄后,可以对该窗口进行操作

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

使用实例:    ShowWindow(myPtr, 0);

0    关闭窗口

1    正常大小显示窗口

2    最小化窗口

3    最大化窗口

5、获取窗口大小,及屏幕坐标

[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;                        //最下坐标
        }

示例:                 

                   InPtr awin = GetForegroundWindow();    //获取当前窗口

                   RECT rect = new RECT();
                   GetWindowRect(awin, ref rect);
                   int width = rect.Right - rect.Left;                        //窗口的宽度
                   int height = rect.Bottom - rect.Top;                   //窗口的高度

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用C#中的`DragDrop`事件和Windows API中的`RegisterDragDrop`函数来实现窗口接收文件拖放功能。 以下是一个示例代码,可以让窗口接收文件拖放,并在控制台输出文件路径: ```csharp using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace WindowDragDrop { public partial class Form1 : Form { public Form1() { InitializeComponent(); RegisterDragDrop(this.Handle); } private void Form1_DragDrop(object sender, DragEventArgs e) { string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); foreach (string file in files) { Console.WriteLine(file); } } private void Form1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy; else e.Effect = DragDropEffects.None; } [DllImport("ole32.dll")] private static extern int RegisterDragDrop(IntPtr hwnd, IDropTarget target); private void RegisterDragDrop(IntPtr handle) { IDropTarget target = new FileDropTarget(this); RegisterDragDrop(handle, target); } private class FileDropTarget : IDropTarget { private readonly Form1 _form; public FileDropTarget(Form1 form) { _form = form; } public void OnDragEnter(DragEventArgs e) { _form.OnDragEnter(e); } public void OnDragOver(DragEventArgs e) { _form.OnDragOver(e); } public void OnDragLeave() { _form.OnDragLeave(); } public void OnDrop(DragEventArgs e) { _form.OnDrop(e); } } } [ComImport] [Guid("00000122-0000-0000-C000-000000000046")] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] internal interface IDropTarget { void OnDragEnter(DragEventArgs e); void OnDragOver(DragEventArgs e); void OnDragLeave(); void OnDrop(DragEventArgs e); } } ``` 在窗口的构造函数中,调用了`RegisterDragDrop`函数,将窗口句柄窗口的`IDropTarget`对象传递给了`RegisterDragDrop`函数,从而实现了窗口的文件拖放功能。同时,窗口中还需要实现`DragEnter`和`DragDrop`事件的处理函数,用来处理文件拖放的具体操作

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值