C#句柄使用

原文: C#句柄使用

调用 API 函数 SendMessage 发送 WM_CLOSE 消息。

DllImport("User32.dll",EntryPoint="SendMessage")] 

private static extern int SendMessage( int hWnd,int Msg,int wParam,int lParam);

const int WM_CLOSE = 0x10;
SendMessage(那个程序的窗口句柄, WM_CLOSE, 0, 0);

可以用 IntPtr 代替 int 数据类型

 

 

在Visual C# 2005中如何实现窗口任意处移动窗口

首先,要用到一个WimdowsAPI函数,因此必须引入
using System.Runtime.InteropServices;
命名空间;

然后,这里有两种方法,一种使用API, 一种不用,重写WndProc窗口过程的方式不需要API函数。另一个方法需要两个:
SendMessage 像指定窗口过程发送消息
ReleaseCapture 释放鼠标捕获

最后是一些必要的常数声明,这些声明可以在MSDN或者CPP头文件中找到:
private const int WM_SYSCOMMAND = 0x0112;//点击窗口左上角那个图标时的系统信息
private const int SC_MOVE = 0xF010;//移动信息
private const int HTCAPTION = 0x0002;//表示鼠标在窗口标题栏时的系统信息
private const int WM_NCHITTEST = 0x84;//鼠标在窗体客户区(除了标题栏和边框以外的部分)时发送的消息
private const int HTCLIENT = 0x1;//表示鼠标在窗口客户区的系统消息
private const int SC_MAXIMIZE = 0xF030;//最大化信息
private const int SC_MINIMIZE = 0xF020;//最小化信息

Here we go。

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 WindowsApplicationTestNoBoarderAndMove
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();

        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        private const int WM_SYSCOMMAND = 0x0112;//点击窗口左上角那个图标时的系统信息
        private const int SC_MOVE = 0xF010;//移动信息
        private const int HTCAPTION = 0x0002;//表示鼠标在窗口标题栏时的系统信息
        private const int WM_NCHITTEST = 0x84;//鼠标在窗体客户区(除了标题栏和边框以外的部分)时发送的消息
        private const int HTCLIENT = 0x1;//表示鼠标在窗口客户区的系统消息
        private const int SC_MAXIMIZE = 0xF030;//最大化信息
        private const int SC_MINIMIZE = 0xF020;//最小化信息

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        //下面的代码我用//注释了一下,这是两种实现方法

        //private void Form1_MouseDown(object sender, MouseEventArgs e)
        //{
        //    ReleaseCapture();//首先释放鼠标焦点捕获,这样就不会再发出WM_NCHITTEST消息
        //    SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);//然后向当前窗体发送消息,消息是移动+表示鼠标在标题栏上
        //}


        //如果用这种重写的方法,就把上面的部分注释掉……

       
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
           

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值