如何在C#中实现窗口的自动隐藏

popomark18 2007-12-05 08:32:08
我想用C#编写一个类似于Visual Studio中的解决方案资源管理器那样的窗口,可以实现停靠、自动隐藏等功能,不知道如何实现,望大家多帮忙。
...全文
425 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dmhaifeng 2007-12-06
  • 打赏
  • 举报
回复
授人予鱼不如授人予渔
brookmill 2007-12-06
  • 打赏
  • 举报
回复
mark
王集鹄 2007-12-05
  • 打赏
  • 举报
回复
参考如下代码:
using System.Runtime.InteropServices;

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();

protected override CreateParams CreateParams
{
get
{
const int WS_EX_TOPMOST = 8;
base.CreateParams.Parent = GetDesktopWindow();
base.CreateParams.ExStyle |= WS_EX_TOPMOST;
return base.CreateParams;
}
}
AnchorStyles anchors;
const int OFFSET = 2;
protected override void WndProc(ref Message m)
{
const int WM_MOVING = 534;
switch (m.Msg)
{
case WM_MOVING: // 窗体移动的消息,控制窗体不会移出屏幕外
int left = Marshal.ReadInt32(m.LParam, 0);
int top = Marshal.ReadInt32(m.LParam, 4);
int right =Marshal.ReadInt32(m.LParam, 8);
int bottom =Marshal.ReadInt32(m.LParam, 12);
left = Math.Min(Math.Max(0, left),
Screen.PrimaryScreen.Bounds.Width - Width);
top = Math.Min(Math.Max(0, top),
Screen.PrimaryScreen.Bounds.Height - Height);
right = Math.Min(Math.Max(Width, right),
Screen.PrimaryScreen.Bounds.Width);
bottom = Math.Min(Math.Max(Height, bottom),
Screen.PrimaryScreen.Bounds.Height);
Marshal.WriteInt32(m.LParam, 0, left);
Marshal.WriteInt32(m.LParam, 4, top);
Marshal.WriteInt32(m.LParam, 8, right);
Marshal.WriteInt32(m.LParam, 12, bottom);
anchors = AnchorStyles.None;
if (left <= OFFSET) anchors |= AnchorStyles.Left;
if (top <= OFFSET) anchors |= AnchorStyles.Top;
if (bottom >= Screen.PrimaryScreen.Bounds.Height - OFFSET)
anchors |= AnchorStyles.Bottom;
if (right >= Screen.PrimaryScreen.Bounds.Width - OFFSET)
anchors |= AnchorStyles.Right;
timer1.Enabled = anchors != AnchorStyles.None;
break;
}
base.WndProc(ref m);
}

private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = false;
timer1.Interval = 200;
TopMost = true;
}

[DllImport("user32.dll")]
public static extern IntPtr WindowFromPoint(Point Point);
[DllImport("user32.dll")]
public static extern IntPtr GetParent(IntPtr hWnd);
private void timer1_Tick(object sender, EventArgs e)
{

IntPtr vHandle = WindowFromPoint(Control.MousePosition);
while (vHandle != IntPtr.Zero && vHandle != Handle)
vHandle = GetParent(vHandle);
if (vHandle == Handle) // 如果鼠标停留的窗体是本窗体,还原位置
{
if ((anchors & AnchorStyles.Left) == AnchorStyles.Left) Left = 0;
if ((anchors & AnchorStyles.Top) == AnchorStyles.Top) Top = 0;
if ((anchors & AnchorStyles.Right) == AnchorStyles.Right)
Left = Screen.PrimaryScreen.Bounds.Width - Width;
if ((anchors & AnchorStyles.Bottom) == AnchorStyles.Bottom)
Top = Screen.PrimaryScreen.Bounds.Height - Height;
}
else // 隐藏起来
{
if ((anchors & AnchorStyles.Left) == AnchorStyles.Left)
Left = -Width + OFFSET;
if ((anchors & AnchorStyles.Top) == AnchorStyles.Top)
Top = -Height + OFFSET;
if ((anchors & AnchorStyles.Right) == AnchorStyles.Right)
Left = Screen.PrimaryScreen.Bounds.Width - OFFSET;
if ((anchors & AnchorStyles.Bottom) == AnchorStyles.Bottom)
Top = Screen.PrimaryScreen.Bounds.Height - OFFSET;
}
}
}
stan0714 2007-12-05
  • 打赏
  • 举报
回复
有个目前已经编写好了商业软件控件,DotNetBar
你可以去搜索一些,有破解版本。
swort_177 2007-12-05
  • 打赏
  • 举报
回复
关注
SilverNet 2007-12-05
  • 打赏
  • 举报
回复
up
CNLAN 2007-12-05
  • 打赏
  • 举报
回复
黑,虽然没想过,不过先mark一下。
weisheng_lu 2007-12-05
  • 打赏
  • 举报
回复
看看........

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧