110,533
社区成员
发帖
与我相关
我的任务
分享
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;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Drawing.Drawing2D;
namespace AutoApp
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
int formwidth;
int formheight;
public Form1()
{
InitializeComponent();
}
private void form_toolStyle_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
private void form_toolStyle_MouseLeave(object sender, EventArgs e)
{
int nowx = Cursor.Position.X;
int nowy = Cursor.Position.Y;
if (nowx >= (this.Location.X + this.Width) | nowx <= this.Location.X | nowy <= this.Location.Y | nowy >= (this.Location.Y + this.Height))
{
if (this.Location.X == 0)
{
this.Width = 0;
}
if (this.Location.Y == 0)
{
this.Height = 0;
}
}
}
private void form_toolStyle_Move(object sender, EventArgs e)
{
formwidth = this.Width;
formheight = this.Height;
int x = this.Location.X;
int y = this.Location.Y;
if (x <= 0)
{
this.Location = new System.Drawing.Point(0, y);
this.Width = 0;
}
if (y <= 0)
{
this.Location = new System.Drawing.Point(x, 0);
this.Height = 0;
}
}
private void form_toolStyle_MouseEnter(object sender, EventArgs e)
{
this.Width = formwidth;
this.Height = formheight;
}
//public void SetWindowRegion()
//{
//GraphicsPath FormPath;
//FormPath = new System.Drawing.Drawing2D.GraphicsPath();
//Rectangle rect = new Rectangle(-1, -1, this.Width + 1, this.Height);
//FormPath = GetRoundedRectPath(rect, 25);
//this.Region = new Region(FormPath);
//}
//private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
//{
//int diameter = radius;
//Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
//GraphicsPath path = new GraphicsPath();
//path.AddArc(arcRect, 185, 90);
//arcRect.X = rect.Right - diameter;
//path.AddArc(arcRect, 275, 90);
//arcRect.Y = rect.Bottom - diameter;
//path.AddArc(arcRect, 356, 90);
//arcRect.X = rect.Left;
//path.AddArc(arcRect, 90, 90);
//path.CloseFigure();
//return path;
//}
//protected override void OnPaint(PaintEventArgs e)
//{
//base.OnPaint(e);
//SetWindowRegion();
//}
private void Form1_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
}
}
}
而且你真的把高度设置成0,如何判断鼠标已经回到你的窗体上,从而应该显示出来了? 不会还要鼠标钩子去取坐标吧
[quote=引用 7 楼 porenasckx 的回复:] 这应该是个动画效果
这应该是个动画效果
同上,隐藏就好了啊 宽高变0,再圆角?不会出错?
变成宽高为零?为什么不隐藏呢?
在Form1初始化后,给formwidth和formheight赋初始值public Form1() { InitializeComponent(); formwidth = this.Width; formheight = this.Height; }
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace AutoApp
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
int formwidth;
int formheight;
public Form1()
{
InitializeComponent();
formwidth = this.Width;
formheight = this.Height;
}
private void form_toolStyle_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
private void form_toolStyle_MouseLeave(object sender, EventArgs e)
{
int nowx = Cursor.Position.X;
int nowy = Cursor.Position.Y;
if (nowx >= (this.Location.X + this.Width)
|| nowx <= this.Location.X
|| nowy <= this.Location.Y
|| nowy >= (this.Location.Y + this.Height)
)
{
SetDimension();
}
}
private void form_toolStyle_Move(object sender, EventArgs e)
{
SetDimension();
}
private void form_toolStyle_MouseEnter(object sender, EventArgs e)
{
this.Width = formwidth;
this.Height = formheight;
System.Diagnostics.Debug.Print(this.Location.X.ToString() + ":" + this.Location.Y.ToString());
}
private void SetDimension()
{
int x = this.Location.X;
int y = this.Location.Y;
if (x <= 0)
{
this.Location = new System.Drawing.Point(0, y);
this.Width = 0;
}
if (y <= 0)
{
this.Location = new System.Drawing.Point(x, 0);
this.Height = 0;
}
}
public void SetWindowRegion()
{
GraphicsPath FormPath;
FormPath = new System.Drawing.Drawing2D.GraphicsPath();
Rectangle rect = new Rectangle(-1, -1, this.Width + 1, this.Height);
FormPath = GetRoundedRectPath(rect, 25);
this.Region = new Region(FormPath);
}
private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
{
int diameter = radius;
Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
GraphicsPath path = new GraphicsPath();
path.AddArc(arcRect, 185, 90);
arcRect.X = rect.Right - diameter;
path.AddArc(arcRect, 275, 90);
arcRect.Y = rect.Bottom - diameter;
path.AddArc(arcRect, 356, 90);
arcRect.X = rect.Left;
path.AddArc(arcRect, 90, 90);
path.CloseFigure();
return path;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
SetWindowRegion();
}
private void Form1_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
}
}
}
按我开始上传的代码,不加注释的部分是可以实现自动缩出缩回屏幕边缘的(屏幕左、上边可以)。问题在于加了注释部分后,也就是窗体圆角之后,只能从屏幕右边缩回不能从上边缩回了。
用你的代码,在我本机上加上我说明的两句后测试,窗口上边缘到达屏幕顶部,会自动向上收缩隐藏的,像QQ窗口一样。和你预期的效果一样。 你的代码55,56行可以去掉,因为已经移到窗口初始化那里了。 你确定一下你的窗体的MouseDown,MouseEnter,MouseLeave,Move这四个事件都绑定了代码里面的相应函数了。你在试一下,如果不行,我可以把我的项目上传。 [quote=引用 4 楼 h2041075 的回复:] [quote=引用 1 楼 save4me 的回复:] 在Form1初始化后,给formwidth和formheight赋初始值public Form1() { InitializeComponent(); formwidth = this.Width; formheight = this.Height; }
[quote=引用 1 楼 save4me 的回复:] 在Form1初始化后,给formwidth和formheight赋初始值public Form1() { InitializeComponent(); formwidth = this.Width; formheight = this.Height; }
你不会在sizechanged事件里执行的重绘吧
public Form1()
{
InitializeComponent();
formwidth = this.Width;
formheight = this.Height;
}