一种C# Winform的UI处理

发布于:2025-03-31 ⋅ 阅读:(23) ⋅ 点赞:(0)

效果

·圆角 + 阴影 +突出按钮

说明

这是一种另类的处理,不是多层窗口 也不是WPF 。这种方式的特点是比较简单,例如圆角、阴影、按钮等特别容易修改过。其实就是html + css + DirectXForm。

在VS中如下

圆角和阴影

然后编辑这个窗体的Html模板,例如圆角和阴影的调整可以修改CSS中的

    border-radius: 14px;
    box-shadow: 0px 8px 24px 0px rgba(0, 0, 0, 0.2);

我要想一个小圆角可以改成 border-radius: 6px;

中间的按钮

中间的按钮的CSS

.play_btn
{
    position: fixed;
    left: 50%;
    margin-left: -39px;
    cursor: pointer;
    bottom: 22px;    
    display: inline-block;
    width: 78px;
    height: 78px;
    background-color: rgb(255, 255, 255);     
    border-radius: 39px;
    box-shadow: 4px 8px 8px 0px rgba(80, 80, 80, 0.55); 
}
如果你想要个丑点的阴影 ,可以改成

box-shadow: 4px 8px 8px 0px rgba(255, 0, 0, 0.75);

反正就是CSS

窗体可以随便放控件 

全部代码

using DevExpress.Utils;
using DevExpress.Utils.Html;
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;

namespace MP3Cut
{
    public partial class Form1 : DevExpress.XtraEditors.DirectXForm
    {
        public Form1()
        {
            InitializeComponent();
            HtmlElementMouseDown += DemoDirectXForm_HtmlElementDown;
     
        }
        public DxHtmlElement element_mouse = null;
        void DemoDirectXForm_HtmlElementDown(object sender, DxHtmlElementMouseEventArgs e)
        {
          
            var args = e.MouseArgs as DXMouseEventArgs;
            if (e.Element == null || args == null)
                return;
            element_mouse = e.Element;
            args.Handled= click_proc(element_mouse);
           
           
        }
        bool click_proc(DxHtmlElement element)
        {
            if (element == null)
                return false;
            string id = element.Id;
            if ((string.Compare(id, "playbutton", true) == 0)
                || (string.Compare(id, "playbutton_img", true) == 0))
            {

                DxHtmlImageElement el = (DxHtmlImageElement)(this.FindElementById("playbutton_img"));
                string src = el.Src;

                if (string.Compare(src, "play", true) == 0)
                {
                    el.ClassName = "img_pause";
                    el.Src = "pause";                    
                }
                else
                {

                    el.ClassName = "img_play";
                    el.Src = "play";
                } 
                //directXFormContainerControl1.Refresh();  
                return true;
            }
            return false;
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
          
        }

        private void directXFormContainerControl1_DoubleClick(object sender, EventArgs e)
        {
         
        }

        private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (element_mouse != null)
            {                
                click_proc(element_mouse);
                this.Invalidate(true);
                this.Scale(1.000f);
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (element_mouse != null)
            {
                click_proc(element_mouse);
                this.Invalidate(true);
                this.Scale(1.000f);
            }
        }
    }
}

HTML

<div class="shadowframe">
	<div class="frame" id="frame">
		<div class="titlebar">
			<img class="logo" src="logo" />
			<div class="title">一个例子</div>
			
			<div class="searchbox">
				 
			</div> 
			<img class="button" src="Close" id="closebutton"/>
		</div>
		<div class="content" id="content">
			
		</div>
		<div class="footerbar"> 
		 <img class="button" src="begin" id="beginbutton"/>
		 <div  style="width:200px;display: inline-block;"></div>				
		 <img class="button" src="end" id="enbbutton"/>	
			
		</div>
			
	</div>

	<div class="play_btn" id="playbutton">
	   <img class="img_play" id="playbutton_img" src="play" />
	</div>	
</div>

CSS

body{
    padding: 30px;
}
.titlebar{
    padding: 11px 12px 11px 11px;
    display: flex;
    flex-direction: row;
    height: 40px;
}
.shadowframe{
    height: 100%;
    border-radius: 6px;
    box-shadow: 0px 8px 24px 0px rgba(0, 0, 0, 0.2);
}
.frame{
    height: 100%;
    display: flex;
    flex-direction: column;
    border-radius: 6px;
    border: 1px solid rgba(0, 0, 0, 0.2);
    background-color: rgb(252, 252, 253);
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.15);
}
.footerbar{
    padding: 11px 11px 11px 10px;
    align-items: center;  
    height: 42px;
    background-color:rgb(249,250,251);
    border-radius: 0px 0px 13px 13px;
    text-align: center;
    border-top: 1px solid rgb(229, 231, 235);
}
.contenttext{
    display: flex;
    justify-content: center;
    align-items:center;
    flex-direction: column;
    align-self: center;
    font: 14px 'Segoe UI';
    color: @DisabledText/0.5;
}
.content{
    flex-grow: 1;
}   
.panelspace{
    flex-grow:1;
}
.button {
    margin: 0px 4px 0px 4px;
    padding: 8px;
    opacity: 1;
    object-fit: none;
}
.button:hover{
    border-radius: 8px;
    background-color: @ControlText/0.2;
}
.button:active{
    opacity: 0.25;
    border-radius: 8px;
    background-color: @ControlText/0.2;
}
.logo{
    margin:-5px 15px 0px 0px;
    object-fit: none;
}
.searchbox{
    display: flex;
    flex-direction: row;    
    height: 40px;
    flex-grow: 1;
}
.play_btn
{
    position: fixed;
    left: 50%;
    margin-left: -39px;
    cursor: pointer;
    bottom: 22px;    
    display: inline-block;
    width: 78px;
    height: 78px;
    background-color: rgb(255, 255, 255);     
    border-radius: 39px;
    box-shadow: 4px 8px 8px 0px rgba(80, 80,80, 0.55);
   
}

.play_btn:hover{ 
    background-color: rgb(237,238,239);
}
.play_btn:active{ 
    background-color:  rgb(244,248,249);
}
.img_play {
    width: 42px;
    height: 42px;
    margin-top: 19px;
    margin-left: 24px;
    pointer-events: none;
}  
.img_pause {
    width: 24px;
    margin-top: 22px;
    margin-left: 27px;
    pointer-events: none;
}  
.title {
    padding: 5px;
    display: inline-block;
    font: 19px 'Segoe UI';
    font-weight: bold;
    color: rgb(100, 116, 139);
}