C#无边框窗体在任务栏右键菜单

本文介绍了如何在C#创建无边框窗体时,解决任务栏右键菜单无法显示的问题。通过添加特定代码,可以重新启用系统右键菜单,但需要注意的是,最大化窗口时可能会导致窗体覆盖任务栏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在建立C#的无边框窗体的时候,在任务栏上点击窗体的时候,无法显示系统的右键菜单。由于是C#隐藏掉了系统的右键菜单,使用以下代码显示出来就可以了。不过存在一个问题是,最大化的时候会把任务栏也覆盖掉,界面占满整个屏幕

1、在Class里面加入

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    
class Class1
    
{
        System.Windows.Forms.Form FormA;

        
public void Skin(System.Windows.Forms.Form frm)
        
{
            FormA 
= frm;

            frm.FormBorderStyle 
= System.Windows.Forms.FormBorderStyle.None;
            frm.BackColor 
= System.Drawing.SystemColors.Desktop;
            frm.TransparencyKey 
= System.Drawing.SystemColors.Desktop;

            NoneBorder();
        }


        
// http://blog.youkuaiyun.com/hbxtlhx/archive/2007/08/01/1721061.aspx
        [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);
        
        
void NoneBorder()
        
{
            
int WS_SYSMENU = 0x00080000// 系统菜单
            int WS_MINIMIZEBOX = 0x20000// 最大最小化按钮

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

    }

}

 

2、在Form中调用

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    
public partial class Form1 : Form
    
{
        Class1 SkinClass 
= new Class1();

        
public Form1()
        
{
            InitializeComponent();

            SkinClass.Skin(
this);
        }

    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值