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

来源:互联网 发布:手机系统更新软件 编辑:程序博客网 时间:2024/05/16 12:27

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

  其实如果我们使用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);
        }

    }

}