无边框窗体的移动和任务栏菜单的实现

简介:

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 NetWork
{
public partial class Form2 : Form
{
private const int WM_SETREDRAW = 0x000B;
private const int HTCAPTION = 0x0002;//表示鼠标在窗口标题栏时的系统信息
private const int WM_NCHITTEST = 0x84;//鼠标在窗体客户区(除了标题栏和边框以外的部分)时发送的消息
private const int HTCLIENT = 0x1;//表示鼠标在窗口客户区的系统消息

[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);

[DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
public static extern int GetWindowLong(HandleRef hWnd, int nIndex);

[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

private void MenuShow()
{
int WS_SYSMENU = 0x80000; // 系统菜单
int WS_MINIMIZEBOX = 0x20000; // 最大最小化按钮
int WS_MAXIMIZEBOX = 0x00010000;

int windowLong = (GetWindowLong(new HandleRef(this, this.Handle), -16));
SetWindowLong(new HandleRef(this, this.Handle), -16, (windowLong | WS_SYSMENU | WS_MINIMIZEBOX) & (~WS_MAXIMIZEBOX));
}
public Form2()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
this.UpdateStyles();
MenuShow();
SendMessage(this.Handle, WM_SETREDRAW, 0, 0);
}

protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCHITTEST: //如果鼠标移动或单击
base.WndProc(ref m);//调用基类的窗口过程——WndProc方法处理这个消息
if (m.Result == (IntPtr)HTCLIENT)//如果返回的是HTCLIENT
{
m.Result = (IntPtr)HTCAPTION;//把它改为HTCAPTION
return;//直接返回退出方法
}
break;
}
base.WndProc(ref m);//如果不是鼠标移动或单击消息就调用基类的窗口过程进行处理
}

private void Form2_Load(object sender, EventArgs e)
{
}
}
}

其他窗体直接继承此窗体就ok




本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2009/08/18/1549114.html,如需转载请自行联系原作者


相关文章
|
C#
WPF 左键单击弹出菜单 ContextMenu
原文:WPF 左键单击弹出菜单 ContextMenu WPF中的ContextMenu在XAML中可直接做出来,但是仅限于右键弹出菜单,如果需要添加左键弹出功能,只需要在事件中添加Click事件 XMAL代码如下 ...
2783 0
|
2月前
|
索引
MFC工具栏和状态栏
MFC工具栏和状态栏
18 1
|
3月前
MFC隐藏对话框边框和可拖动
MFC隐藏对话框边框和可拖动
|
4月前
[Qt5] 右键窗体弹出菜单,实现图像适应窗体大小
[Qt5] 右键窗体弹出菜单,实现图像适应窗体大小
45 0
C#创建无边框可拖动窗口
C#创建无边框可拖动窗口
171 0
CDialog中使用工具栏和状态栏
CDialog中使用工具栏和状态栏
86 0
|
C#
WPF 标题栏 右键窗口标题添加关于对话框
原文:WPF 标题栏 右键窗口标题添加关于对话框 /// /// wpf标题栏 右键菜单 中添加新项 /// public partial class MainWindow : Window { private const int W...
1190 0