zoukankan      html  css  js  c++  java
  • WPF实现不规则窗体(C#)

    见到一些游戏的客户端窗口,形状是不规则的,一直觉得很炫。就想用winform做一个玩下,思路就是:去掉标题栏,用一张透明背景色的图片作为Form的背景。可是在实现的过程中我却悲愤的发现,winform的控件是支持透明背景色的,但是Form本身却不支持。后来看到一篇文章,貌似窗体的TransparencyKey属性(设置窗体将显示为透明的颜色)可以化解我的悲愤。原来我的思路是错的,真惭愧!但是这种方法在24位色以上的环境下也不能实现效果,为了解决这个问题,后面的代码可谓是贼长贼长的,看得我都不乐意了。更倒胃口的是,那代码居然还是截图的,想复制都没戏。咱可是个懒人,哪愿意写那么多啊!。 

      最后听说WPF做不规则窗体是相当简单的,正好自己也一直想了解WPF到底是个啥么玩意,于是就欣欣然动手做起来。嘿,还果不其然!! 
    具体步骤:
    1、让窗体支持透明,AllowsTransparency="True";
    2、去掉标题栏,WindowStyle="None";
       注:当 AllowsTransparency 为 True 时,WindowStyle.None 是 WindowStyle 的唯一有效值。
    3、设置背景为透明色,Background="Transparent";
    4、用一张不规则的图片作为Grid元素的背景。
    运行就能看到效果啦!
    前台Xaml代码:
        <Window x:Class="WPF_CS.Window_xyy"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="WPF不规则窗体" AllowsTransparency="True" WindowStyle="None"Background="Transparent" MouseLeftButtonDown="Window_MouseLeftButtonDown">
            <Grid Height="520" Width="520">
                <Image Source="/Wpf_CS;component/Images/xyy.png">
                    <Image.BitmapEffect>
                        <OuterGlowBitmapEffect GlowColor="White" GlowSize="2" />
                    </Image.BitmapEffect>
                </Image>
                <TextBlock Height="16" HorizontalAlignment="Left" Margin="90,430,0,0"Text="--2010年9月7日" FontSize="13" />
                <Button Width="50" Height="30" Background="#ADFF2F"HorizontalAlignment="Right" Margin="0,420,60,0" Click="btnClose_Click"FocusVisualStyle="{x:Null}">关 闭</Button>
            </Grid>
        </Window>
        OuterGlowBitmapEffect效果:围绕对象或颜色区域创建颜色光环;
        点击按钮时,会出现一个虚线边框,加上FocusVisualStyle="{x:Null}"可以去掉这个边框。
    后台C#代码:
        /// <summary>
        /// 拖拽窗体
        /// </summary>
        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

        {
            this.DragMove();
        }
        /// <summary>
        /// 关闭窗体
        /// </summary>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
             Application.Current.Shutdown();
        }

    窗体的拖动:
    因为没有标题栏,窗体变得无法拖动了。记得在winform里面,是利用windows API函数和windows的消息机制解决的,顺便也记录进来: 
    //声明API函数和消息

    [DllImport("user32.dll")]
    public static extern bool ReleaseCapture();
    [DllImport("user32.dll")]
    public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
    public const int WM_SYSCOMMAND = 0x0112;
    public const int SC_MOVE = 0xF010;
    public const int HTCAPTION = 0x0002;

    /// <summary>
    /// 实现窗体可拖动
    /// </summary>
    public void DragForm(IntPtr handle)
    {
        ReleaseCapture();
        SendMessage(handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
    }
    //然后在相关控件的MouseDown事件中加入:
       DragForm(this.Handle); 
    而WPF提供了DragMove方法,在相应控件的MouseLeftButtonDown事件函数中调用即可实现。
    效果图:

    图片

  • 相关阅读:
    微信分享功能开发代码整理
    js 完美兼容浏览器的复制功能
    linux 搭建svn服务器
    zendStudio安装Xdebug项目断点调试
    程序员开发常用英语词汇
    简单的一个远传采集并下载远传图片存储到本地示例
    form表单中经常用到的禁用获取值问题
    记录下url拼接的多条件筛选js
    better-scroll在vue中的使用
    移动端适配,引入lib-flexible,vue开发中将px转化为rem
  • 原文地址:https://www.cnblogs.com/yanpo/p/2286717.html
Copyright © 2011-2022 走看看