Winfrom控件使用

Posted 欢迎莅临 SUN WU GANG 的园子!!!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Winfrom控件使用相关的知识,希望对你有一定的参考价值。

1.Lablelable添加图片,解决图片和字体重叠?

Text属性添加足够空格即可,显示效果如下所示:

 

2.根据窗体名称获取窗体并显示到指定panel?

Label item = sender as Label;

if (item == null) return;

Assembly assembly = Assembly.GetExecutingAssembly();
var path = "Namespace." + item.Name;
Form form = assembly.CreateInstance(path) as Form;
if (form == null) return;

this.panelContent.Controls.Clear();
form.TopLevel = false;
form.FormBorderStyle = FormBorderStyle.None;
form.Dock = DockStyle.Fill;
form.Parent = this.panelContent;
this.panelContent.Controls.Add(form);
form.Show();

注意:item.Name为获取到的窗体名称,如:LoginForm.

 

3.panel添加控件并为控件添加事件?

public class MenuItemNodes
    {
        public string Value { get; set; }
        public string Name { get; set; }
    }
  
private void InitNavigation(List<MenuItemNodes> items)
        {
            if (items == null) return;

            this.panleNavigation.Controls.Clear();
            foreach (MenuItemNodes item in items)
            {
                Add(new Label(), item, this.panleNavigation);
            }
        }

        private void Add(Label item, MenuItemNodes node, Panel panel)
        {
            item.Name = node.Name;
            item.Text = node.Value;
            item.Size = new Size(1500, 35);
            item.TextAlign = ContentAlignment.MiddleLeft;
            item.ForeColor = Color.White;
            item.Font = new Font("微软雅黑", 12f, FontStyle.Bold);
            //34, 95, 129
            item.BackColor = System.Drawing.Color.FromArgb(55, 139, 175);
            item.BorderStyle = BorderStyle.FixedSingle;
            
            if (panel.Controls.Count == 0) item.Location = new Point();
            else
            {
                int y = 0;
                int x = 0;
                if (panel.Controls.Count % 1 > 0)
                {
                    y = panel.Controls[panel.Controls.Count - 1].Location.Y;
                    x = panel.Controls[panel.Controls.Count - 1].Location.X + item.Width;
                }
                else
                {
                    y = panel.Controls[panel.Controls.Count - 1].Location.Y + item.Height;
                    x = panel.Controls[panel.Controls.Count - 1].Location.X;
                }

                item.Location = new Point(x, y);
            }
            item.MouseClick -= item_MouseClick;
            item.MouseClick += new MouseEventHandler(item_MouseClick);  

            panel.Controls.Add(item);
        }

 void item_MouseClick(object sender, MouseEventArgs e)
{
}

 

以上是关于Winfrom控件使用的主要内容,如果未能解决你的问题,请参考以下文章

.NET(winfrom)中button控件怎么设置成透明?

winfrom 窗体控件实现二级联动

Winfrom控件使用

WPF 精修篇 WPF嵌入Winfrom控件

分享一个 C# Winfrom 下的 OutlookBar 控件的使用

winfrom 控件的显示隐藏方法