无边窗体在任务栏上的系统菜单

  我们可能会用到一个没有边框的窗体,因为这样我们可以在整个窗体上自由的绘制。然后我们也会发现一个讨厌的问题,这个时候在任务栏上鼠标右键点不出那可爱的系统菜单了。这样会不好,会使得用户感觉到没有道理,为什么别的窗口都有系统菜单,而只有我们的窗体没有系统菜单呢?

  其实如果我们使用VC建立MFC程序的时候,我们可以把窗体的“Title Bar”设置为False,这样MFC的窗口就是无标题的,然而这个窗口在运行后的任务栏上是有右键的系统菜单的。

  为什么C#做不到而MFC可以做的到呢。。。

  其实C#也可以做的到,只不过当C#里把一个窗体设置为不显示标题的时候它会默认的把系统菜单给隐藏掉了,而MFC是没有隐藏的。我们只要在C#里把这个被隐藏掉的系统菜单给放开就可以了。有如下的代码参考: 

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  NoBorderForm
{
    
public partial class NoBorder : Form
    
{
        [DllImport(
"user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
        
public static extern int GetWindowLong(HandleRef hWnd, int nIndex);

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

        
public NoBorder()
        
{
            InitializeComponent();
        }


        
private void button1_Click(object sender, EventArgs e)
        
{
            
int WS_SYSMENU = 0x00080000;

            
this.FormBorderStyle = FormBorderStyle.None;

            
int windowLong = (GetWindowLong(new HandleRef(thisthis.Handle), -16));
            SetWindowLong(
new HandleRef(thisthis.Handle), -16, windowLong | WS_SYSMENU);
        }

    }

}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值