发布于2024-11-02 12:12 阅读(404) 评论(0) 点赞(0) 收藏(4)
上面的界面布局是值得学习的,页面间的切换也是不错的。
自己跟着做了下,代码上传了,可以下载【C#主界面设计】
类里面,编写固定的颜色数组,让界面切换的时候,实现页面的颜色变化【代码如下:】
internal class ThemeColor
{
public static List<string> ColorList = new List<string>()
{
"#3F51B5",
"#009688",
"#FF5722",
"#607D8B",
"#FF9800",
"#9C27B0",
"#2196F3",
"#EA676C",
"#E41A4A",
"#5978BB",
"#018790",
"#0E3441",
"#00B0AD",
"#721D47",
"#EA4833",
"#EF937E",
"#F37521",
"#A12059",
"#126881",
"#8BC240",
"#364D5B",
"#C7DC5B",
"#0094BC",
"#E4126B",
"#43B76E",
"#7BCFE9",
"#B71C46"
};
}
namespace ZHCHwindows_1
{
public partial class Form1 : Form
{
//Fields
private Button currentButton;
private Random random;
private int tempIndex;
public Form1()
{
InitializeComponent();
random = new Random();
}
//Methods
private Color SelectThemeColor()
{
int index = random.Next(ThemeColor.ColorList.Count);
//找到与当前界面颜色不同的
while(tempIndex == index)
{
index = random.Next(ThemeColor.ColorList.Count);
}
tempIndex = index;
string color = ThemeColor.ColorList[index];
return ColorTranslator.FromHtml(color);
}
}
}
1)我们为主题选择一个随机颜色(可选,你可以使用单一颜色来突出显示按钮)。
2)我们改变按钮的背景颜色。
3)我们改变按钮的字体颜色。
4)我们改变按钮的字体大小。
最终实现:
通过激活/突出显示按钮,我们增加了字体缩放效果的大小
private void ActivateButton(object btnSender)
{
if (btnSender != null)
{
if (currentButton != (Button)btnSender)
{
DisableButton();
Color color = SelectThemeColor();
currentButton = (Button)btnSender;
currentButton.BackColor = color;
currentButton.ForeColor = Color.White;
currentButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
}
}
}
private void DisableButton()
{
foreach (Control previousBtn in panelMenu.Controls) {
if (previousBtn.GetType() == typeof(Button))
{
previousBtn.BackColor = Color.FromArgb(51, 51, 76);
previousBtn.ForeColor = Color.Gainsboro;
previousBtn.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
}
}
}
这里直接给6个button创建点击事件,在点击事件中调用ActivateButton()
代码如下:【只选其一展示】
private void btnProducts_Click(object sender, EventArgs e)
{
ActivateButton(sender);
}
此时,左边的菜单栏已经配置完毕:
public static Color ChangeColorBrightness(Color color,double correctionFactor)
{
double red = color.R;
double green = color.G;
double blue = color.B;
//correction factor<0,则降低颜色亮度
if (correctionFactor < 0)
{
correctionFactor = 1 + correctionFactor;
red *= correctionFactor;
green *= correctionFactor;
blue *= correctionFactor;
}
else
{
red = (255-red)*correctionFactor+red;
green = (255-green)*correctionFactor+green;
blue = (255-blue)*correctionFactor+blue;
}
return Color.FromArgb(color.A,(byte)red, (byte)green, (byte)blue);
}
private void ActivateButton(object btnSender)
{
if (btnSender != null)
{
if (currentButton != (Button)btnSender)
{
DisableButton();
Color color = SelectThemeColor();
currentButton = (Button)btnSender;
currentButton.BackColor = color;
currentButton.ForeColor = Color.White;
currentButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
// 上方栏的颜色变化
panelTitleBar.BackColor = color;
panelLogo.BackColor = ThemeColor.ChangeColorBrightness(color,-0.3);
}
}
}
添加子窗体的容器面板
我们创造一个方法,实现在子panel中打开新的Forms
private Form activeForm;
/// <summary>
/// 打开子Form
/// </summary>
/// <param name="childForm"></param>
/// <param name="btnSender"></param>
private void OpenChildForm(Form childForm, object btnSender)
{
if(activeForm != null)
{
activeForm.Close();
}
ActivateButton(btnSender);
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
this.panelDesktopPane.Controls.Add(childForm);
this.panelDesktopPane.Tag = childForm;
childForm.BringToFront();
childForm.Show();
//让lblTitle的标题和子Form的标题一样
this.lblTitle.Text = childForm.Text;
}
private void btnProducts_Click(object sender, EventArgs e)
{
OpenChildForm(new Forms.FormProduct(), sender);
}
public static Color PrimaryColor { get; set; }
public static Color SecondaryColor { get; set; }
private void ActivateButton(object btnSender)
{
if (btnSender != null)
{
if (currentButton != (Button)btnSender)
{
DisableButton();
Color color = SelectThemeColor();
currentButton = (Button)btnSender;
currentButton.BackColor = color;
currentButton.ForeColor = Color.White;
currentButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
// 上方栏的颜色变化
panelTitleBar.BackColor = color;
panelLogo.BackColor = ThemeColor.ChangeColorBrightness(color,-0.3);
//
ThemeColor.PrimaryColor = color;
ThemeColor.SecondaryColor = ThemeColor.ChangeColorBrightness(color, -0.3);
}
}
}
public partial class FormProduct : Form
{
public FormProduct()
{
InitializeComponent();
}
private void FormProduct_Load(object sender, EventArgs e)
{
LoadTheme();
}
private void LoadTheme()
{
foreach (Control btns in this.Controls) {
if(btns.GetType() == typeof(Button))
{
Button btn = (Button)btns;
btn.BackColor = ThemeColor.PrimaryColor;
btn.ForeColor = Color.White;
btn.FlatAppearance.BorderColor = ThemeColor.SecondaryColor;
}
}
label4.ForeColor = ThemeColor.SecondaryColor;
label5.ForeColor = ThemeColor.PrimaryColor;
}
}
一个关闭按钮,用于关闭子Form,并重置为默认值。
public Form1()
{
InitializeComponent();
random = new Random();
btnCloseChildForm.Visible = false;
}
private void ActivateButton(object btnSender)
{
if (btnSender != null)
{
if (currentButton != (Button)btnSender)
{
DisableButton();
Color color = SelectThemeColor();
currentButton = (Button)btnSender;
currentButton.BackColor = color;
currentButton.ForeColor = Color.White;
currentButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
// 上方栏的颜色变化
panelTitleBar.BackColor = color;
panelLogo.BackColor = ThemeColor.ChangeColorBrightness(color,-0.3);
//
ThemeColor.PrimaryColor = color;
ThemeColor.SecondaryColor = ThemeColor.ChangeColorBrightness(color, -0.3);
btnCloseChildForm.Visible = true;
}
}
}
private void btnCloseChildForm_Click(object sender, EventArgs e)
{
//activeForm不为null,说明已经有一个子窗体被打开。
if (activeForm != null)
{
activeForm.Close();
Reset();
}
}
private void Reset()
{
DisableButton();
lblTitle.Text = "HOME";
panelTitleBar.BackColor = Color.FromArgb(0, 150, 136);
panelLogo.BackColor = Color.FromArgb(39, 39, 58);
currentButton = null;
btnCloseChildForm.Visible = false;
}
原文链接:https://blog.csdn.net/qq_41714549/article/details/141171993
作者:Hggh
链接:http://www.qianduanheidong.com/blog/article/534389/67cd798bebca2c5eb781/
来源:前端黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 前端黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-3
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!