关于winform自定义控件问题 自定义一个控件,如何能让他成为一个类似pannel一样的容器,并且具有ToolStrip那种停靠在上方并且不会被其他容器挤压或者占用他已经占用的区域求大神 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 继承pannel类只解决了他是个容器,最难搞的是他如何能像类似于ToolStrip一样停靠在最上面 当我们拖一个pannel到设计器的时候,所有的控件会被Toolstrip往下挤25个像素,所以自定义个控件想完成这种功能,表示很无助啊,以前没做过CS的东西,不太会弄,搞半下午了没搞定 说错了拖一个pannel到设计器不会有像拖一个ToolStrip把所有元素往下挤25个像素这种效果,现在想自定义一个控件继承自pannel 并且有ToolStrip的这种功能 1.重写Panel控件 如楼主所说 解决容器问题2.理解“设计时(DesignTime)”和“运行时(RunTime)”的概念 在OnLoad方法中 处理停靠、改变其他控件的位置 protected override OnLoad(...) { if(DesignMode) //控件处于 “设计时” 如果是“运行时” 就不需要了 { //处理停靠、改变其他控件位置 } }运行时 设计时 参考http://www.cnblogs.com/xiaozhi_5638/archive/2013/06/10/3131019.html 楼主 不好意思 刚才试了一下 7#的解决方案 只是在设计器中有效果 运行后无效果 完整的解决方法如下:using System.ComponentModel.Design;using System.Linq;using System.Text;using System.Windows.Forms;using System.Windows.Forms.Design;namespace Gravity{ [Designer(typeof(MyPanelDesigner))] class MyPanel : Panel { public MyPanel() { Dock = DockStyle.Top; BackColor = System.Drawing.Color.Red; } } class MyPanelDesigner : ControlDesigner { public override void Initialize(IComponent component) { base.Initialize(component); IDesignerHost ids = GetService(typeof(IDesignerHost)) as IDesignerHost; Control c = this.Control; //当前被设计的组件 MyPanel Control parent = ids.RootComponent as Control; //父窗体 MemberDescriptor aMem_Controls = TypeDescriptor.GetProperties(parent)["Controls"]; RaiseComponentChanging(aMem_Controls); //通知设计器 parent中的子控件发生变化 parent.Size = new System.Drawing.Size(parent.Width, parent.Height + c.Height); //父控件(窗体)高度增加 foreach (Control cc in parent.Controls) //改变父控件中每一个子控件位置 { cc.Location = new System.Drawing.Point(cc.Location.X, cc.Location.Y + c.Height); } RaiseComponentChanged(aMem_Controls,null,null); //通知设计器 parent中子控件以及发生变化 设计器会自动生成代码 } }} 在winform中怎么弹出打印的窗体 C# 怎么在两台电脑之间通信? 命令行问题,再也经不起折腾了 怎么在richTextBox选中指定一行字符.... FTP登陆问题 我的泛型怎么泛不起来? 开发端和客户端分辨率不同的时候,开发程序的烦恼。 C#开发WINDOWS应用程序,怎么调用动态链接库DLL文件,请教教我,请把步骤写详细点,谢谢 用C#写网站还要懂什么语言呢?比如VBScript或JAVAScript? 在线等待,C#链接sqlserver问题! 有大神帮忙看看正则表达是的问题 ado.net往数据库里面写入超长字符串的问题
最难搞的是他如何能像类似于ToolStrip一样停靠在最上面
拖一个pannel到设计器不会有像拖一个ToolStrip把所有元素往下挤25个像素这种效果,现在想自定义一个控件继承自pannel 并且有ToolStrip的这种功能
2.理解“设计时(DesignTime)”和“运行时(RunTime)”的概念
在OnLoad方法中 处理停靠、改变其他控件的位置
protected override OnLoad(...)
{
if(DesignMode) //控件处于 “设计时” 如果是“运行时” 就不需要了
{
//处理停靠、改变其他控件位置
}
}
运行时 设计时 参考http://www.cnblogs.com/xiaozhi_5638/archive/2013/06/10/3131019.html
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace Gravity
{
[Designer(typeof(MyPanelDesigner))]
class MyPanel : Panel
{
public MyPanel()
{
Dock = DockStyle.Top;
BackColor = System.Drawing.Color.Red;
}
}
class MyPanelDesigner : ControlDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize(component);
IDesignerHost ids = GetService(typeof(IDesignerHost)) as IDesignerHost; Control c = this.Control; //当前被设计的组件 MyPanel
Control parent = ids.RootComponent as Control; //父窗体 MemberDescriptor aMem_Controls = TypeDescriptor.GetProperties(parent)["Controls"];
RaiseComponentChanging(aMem_Controls); //通知设计器 parent中的子控件发生变化
parent.Size = new System.Drawing.Size(parent.Width, parent.Height + c.Height); //父控件(窗体)高度增加
foreach (Control cc in parent.Controls) //改变父控件中每一个子控件位置
{
cc.Location = new System.Drawing.Point(cc.Location.X, cc.Location.Y + c.Height);
}
RaiseComponentChanged(aMem_Controls,null,null); //通知设计器 parent中子控件以及发生变化 设计器会自动生成代码
}
}
}