您的位置:首页 > 其它

如何隐藏重绘窗体右上角按钮

2011-09-29 15:51 211 查看
最近项目中的一个功能让我纠结了很久,写出来总结一下。下面描述下我的需求。

项目中有开启第三方软件(飞信),需要控制它的大小,位置固定,不能最小化、关闭,这些操作对于没有重绘窗体使用API很容易实现,可这些对重绘窗体却不太好使。最先开始想到的是不让鼠标移上去,这样自然不能点击操作了,可客户那边反馈过来的是触摸屏操作,这种实现方式行不通了,只能另寻它法了。

这问题困扰了我两周了。好不夸张的说,走在路上都在想解决办法。就在昨天差点被车撞了,我很庆幸驾驶员没有喝醉酒之类的情况。

进入正题说说我的解决办法吧,不过觉得有些投机取巧,在此与大家分享交流,也希望能找到更好的解决方法。上文中我所说的重绘窗体什么样呢?最直观的感受就是漂亮。下面以计算机器和飞信为例,看看操作前后的效果。

View Code

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Fetion
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

[DllImport("user32.dll", EntryPoint = "FindWindow")]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle rect);

[DllImport("user32.dll")]
private static extern bool GetClientRect(IntPtr hWnd, ref Rect lpRect);

[DllImport("user32.dll")]
static extern IntPtr GetSystemMenu(IntPtr hwnd, bool bRevert);

[DllImport("user32.dll")]
static extern bool DeleteMenu(IntPtr hMenu, uint uPosition, uint uFlags);

[DllImport("user32.dll")]
static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);

[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
public static extern int GetWindowLong(IntPtr hwnd, int nIndex);

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

[DllImport("user32.dll")]
public static extern int SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw);

[DllImport("gdi32.dll")]
public static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);

[StructLayout(LayoutKind.Sequential)]
struct Rect
{
public int left, top, right, bottom;

}

const uint SC_MOVE = 0xF010; //移动
const uint SC_SIZE = 0xF000; //大小
const uint SC_CLOSE = 0xF060;//关闭
const uint MF_BYCOMMAND = 0x00; //按命令方式
const uint MF_DISABLED = 0x02;  //不可用

int GWL_STYLE = -16;
int WS_MAXIMIZEBOX = 0x10000;
int WS_MINIMIZEBOX = 0x20000;

private void Form2_Load(object sender, EventArgs e)
{

IntPtr ParenthWnd = FindWindow("FxWnd", "飞信2011");

/*重绘窗体不可行*/
int nStyle = GetWindowLong(ParenthWnd, GWL_STYLE);
nStyle &= ~(WS_MAXIMIZEBOX);
SetWindowLong(ParenthWnd, GWL_STYLE, nStyle);//废掉最大化按钮
nStyle &= ~(WS_MINIMIZEBOX);
SetWindowLong(ParenthWnd, GWL_STYLE, nStyle);//废掉最小化按钮

IntPtr hMenu = GetSystemMenu(ParenthWnd, false);
DeleteMenu(hMenu, SC_MOVE, MF_BYCOMMAND);//不让移动
DeleteMenu(hMenu, SC_SIZE, MF_BYCOMMAND);//不让改大小
EnableMenuItem(hMenu, SC_CLOSE, MF_DISABLED);//禁用关闭按钮(重绘窗体不可行)

Rect crect = new Rect(); ;
GetClientRect(ParenthWnd, ref crect);
IntPtr OutRgn = CreateRoundRectRgn(0, 20, crect.right, crect.bottom + 5, 5, 5);
SetWindowRgn(ParenthWnd, OutRgn, true);

}

}
}



最后提前祝各位园友节日快乐,准备好去哪旅行了没?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: