Hod*_*lom 1 c# forms resize splitcontainer winforms
我有一个winform窗口.当我改变屏幕大小时,屏幕会立即增加或减少.
我更喜欢窗口的Resize行为就像Split Container一样,只要我拖动鼠标我只看到标记窗口大小的行,并且只有在离开Resize操作时才会进行.
我看到几个例子表明,通过隐藏窗口的框架,然后通过点击窗口本身画框.
我希望通过单击窗口的框架(我不想隐藏框架)而不是在窗口上.
有没有办法做到这一点?(可以以任何方式覆盖Resize的行为).
我很确定你在互联网上找不到任何解决方案.不过我已经为此尝试了一个演示,它运行得很好.
在winforms许多其他UI技术中,您无法在窗口外部呈现某些内容.为了获得我们想要的效果,我们必须在窗口外部或内部渲染一些指示性边框,具体取决于用户调整其大小的方式.所以看起来我们被卡住了?
但是有一种技术可以做到这一点(我称之为层技术).我们需要一个透明的非聚焦图层来渲染指示性边框.该层将使其Size与主窗口Location的Size和Location(仅有一点偏移)同步.该图层也将invisible默认显示,仅在用户调整大小时显示,并在结束调整大小时隐藏.
用我提到的技术就可以了.但是,当用户调整窗口大小时,如何防止/放弃默认大小调整?幸运的是,Win32支持2条消息可以轻松完成:
LParam保持RECT调整在当前窗口的结构.我们阅读此信息以正确呈现指示性边框.然后我们需要将其修改为窗口RECT的当前Bounds值以丢弃默认的调整大小效果(大小和位置立即更改).Size和Location基于该窗口Size和Location透明层,当然还有隐藏图层即可.现在问题完全可以解决了.这是我制作的演示代码.请注意,这里有一个非常讨厌的无法解决和难以理解的错误,它发生在你调整Top-Left角落大小时,Size释放鼠标后正确更新但是Location设置了偏移量.我调试但没有运气.在某些时候,Top并没有明确的原因Left跳转到意外的值.但是,所有侧面(左侧,顶部,右侧,底部)和其他角落的调整大小都可以.事实上,用户很难完成调整大小,所以我认为这个解决方案是可以接受的.Top-Left corner
//Must add using System.Runtime.InteropServices;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Sizing border initialization
SizingBorderWidth = 3;
SizingBorderStyle = DashStyle.Custom;
SizingBorderColor = Color.Orange;
//layer initialization
layer.Owner = this;//especially this one.
layer.Width = Width + SizingBorderWidth * 2;
layer.Height = Height + SizingBorderWidth * 2;
//Paint the border when sizing
layer.Paint += (s, e) => {
using (Pen p = new Pen(SizingBorderColor) { Width = SizingBorderWidth }) {
if (Use3DSizingBorder) {
ControlPaint.DrawBorder3D(e.Graphics, sizingRect.Left, sizingRect.Top, sizingRect.Width, sizingRect.Height, Border3DStyle.Bump, Border3DSide.All);
}
else {
p.DashStyle = SizingBorderStyle;
p.LineJoin = LineJoin.Round;
if(p.DashStyle == DashStyle.Custom)
p.DashPattern = new float[] { 8f, 1f, 1f, 1f };//length of each dash from right to left
e.Graphics.DrawRectangle(p, sizingRect);
}
}
};
//Bind the Location of the main form and the layer form together
LocationChanged += (s, e) => {
Point p = Location;
p.Offset(-SizingBorderWidth, -SizingBorderWidth);
layer.Location = p;
};
//Set the intial Location of layer
Load += (s, e) =>{
Point p = Location;
p.Offset(-SizingBorderWidth, -SizingBorderWidth);
layer.Location = p;
};
}
//Set this to true to use 3D indicative/preview border
public bool Use3DSizingBorder { get; set; }
//Change the indicative/preview border thickness
public int SizingBorderWidth { get; set; }
//Change the indicative/preview border style
public DashStyle SizingBorderStyle { get; set; }
//Change the indicative/preview border color
public Color SizingBorderColor { get; set; }
//hold the current sizing Rectangle
Rectangle sizingRect;
bool startSizing;
bool suppressSizing;
//This is a Win32 RECT struct (don't use Rectangle)
public struct RECT
{
public int left, top, right, bottom;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x214&&!suppressSizing)//WM_SIZING = 0x214
{
RECT rect = (RECT) m.GetLParam(typeof(RECT));
int w = rect.right - rect.left;
int h = rect.bottom - rect.top;
sizingRect = new Rectangle() {X = SizingBorderWidth/2, Y = SizingBorderWidth/2,
Width = w, Height = h};
layer.Left = rect.left-SizingBorderWidth;
layer.Top = rect.top-SizingBorderWidth;
layer.Width = w+2*SizingBorderWidth;
layer.Height = h+2*SizingBorderWidth;
if (!startSizing)
{
layer.Show();
startSizing = true;
}
layer.Invalidate();
//Keep the current position and size fixed
rect.right = Right;
rect.bottom = Bottom;
rect.top = Top;
rect.left = Left;
//---------------------------
Marshal.StructureToPtr(rect, m.LParam, true);
}
if (m.Msg == 0x232)//WM_EXITSIZEMOVE = 0x232
{
layer.Visible = false;
BeginInvoke((Action)(() => {
suppressSizing = true;
Left = layer.Left + SizingBorderWidth;
Top = layer.Top + SizingBorderWidth;
Width = layer.Width - 2 * SizingBorderWidth;
Height = layer.Height - SizingBorderWidth * 2;
suppressSizing = false;
}));
startSizing = false;
}
base.WndProc(ref m);
}
//Here is the layer I mentioned before.
NoActivationForm layer = new NoActivationForm();
}
public class NoActivationForm : Form {
public NoActivationForm() {
//The following initialization is very important
TransparencyKey = BackColor;
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
StartPosition = FormStartPosition.Manual;
//----------------------------------------------
}
protected override bool ShowWithoutActivation {
get { return true; }
}
}
Run Code Online (Sandbox Code Playgroud)
一些屏幕截图:

编辑:(此编辑建议Hodaya Shalom,OP(怪异:)
我找到了左角问题的解决方案:
在BeginInvoke之前我保存变量并在调用中放入局部变量:
int _top = layer.Top + SizingBorderWidth;
int _left = layer.Left + SizingBorderWidth;
int _width = layer.Width - 2 * SizingBorderWidth;
int _height = layer.Height - SizingBorderWidth * 2;
BeginInvoke((Action)(() => {
suppressSizing = true;
Left = _left;
Top = _top;
Width =_width;
Height =_height;
suppressSizing = false;
}));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1561 次 |
| 最近记录: |