| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1788 人关注过本帖, 1 人收藏
标题:一组窗体代码
只看楼主 加入收藏
qq2889577966
Rank: 4
等 级:业余侠客
威 望:5
帖 子:66
专家分:277
注 册:2021-4-14
结帖率:100%
收藏(1)
 问题点数:0 回复次数:4 
一组窗体代码
别搜baidu了,很多都有问题---
比如:图像窗体,添加不了组件,事件响应没有;无边框阴影窗体得作图;毛玻璃只适用win7,win10没效果等等。

这组代码是我常用的,解决了使用中的各类问题,直接拿过去用,无DLL的坑,全部cs源码示例。在win10上测试没问题,没装win7效果不知道,自己测。
在附件下载。
包括:
winForm毛玻璃
winForm图像化窗体
Wpf毛玻璃
Wpf无边框阴影
winForm无边框阴影窗体

窗体代码.rar (657.91 KB)


比如winForm无边框阴影窗体代码
程序代码:
/*

 * 无边框阴影窗体

 * 适用win10、win7,XP不适用

 * qiaoke_song@*/
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace contextUI
{
    public partial class ConextForm : Form
    {
        public bool Sizeable = true;

        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
        (
            int nLeftRect,
            int nTopRect,
            int nRightRect,
            int nBottomRect,
            int nWidthEllipse,
            int nHeightEllipse
        );

        [DllImport("dwmapi.dll")]
        public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);

        [DllImport("dwmapi.dll")]
        public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

        [DllImport("dwmapi.dll")]
        public static extern int DwmIsCompositionEnabled(ref int pfEnabled);

        private bool m_aeroEnabled = false;
        private const int CS_DROPSHADOW = 0x00020000;
        private const int WM_NCPAINT = 0x0085;

        public struct MARGINS
        {
            public int leftWidth;
            public int rightWidth;
            public int topHeight;
            public int bottomHeight;
        }

        protected override CreateParams CreateParams
        {
            get
            {
                m_aeroEnabled = CheckAeroEnabled();

                CreateParams cp = base.CreateParams;
                if (!m_aeroEnabled)
                    cp.ClassStyle |= CS_DROPSHADOW;

                return cp;
            }
        }

        private bool CheckAeroEnabled()
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                int enabled = 0;
                DwmIsCompositionEnabled(ref enabled);
                return (enabled == 1) ? true : false;
            }
            return false;
        }

        const int HTLEFT = 10;
        const int HTRIGHT = 11;
        const int HTTOP = 12;
        const int HTTOPLEFT = 13;
        const int HTTOPRIGHT = 14;
        const int HTBOTTOM = 15;
        const int HTBOTTOMLEFT = 0x10;
        const int HTBOTTOMRIGHT = 17;

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            switch (m.Msg)
            {
                case WM_NCPAINT:
                    if (m_aeroEnabled)
                    {
                        var v = 2;
                        DwmSetWindowAttribute(this.Handle, 2, ref v, 4);
                        MARGINS margins = new MARGINS()
                        {
                            bottomHeight = 1,
                            leftWidth = 1,
                            rightWidth = 1,
                            topHeight = 1
                        };
                        DwmExtendFrameIntoClientArea(this.Handle, ref margins);

                    }
                    break;
                case 0x0084:
                    if (Sizeable)
                    {
                        base.WndProc(ref m);
                        Point vPoint = new Point((int)m.LParam & 0xFFFF, (int)m.LParam >> 16 & 0xFFFF);
                        vPoint = PointToClient(vPoint);
                        if (vPoint.X <= 5)
                        {
                            if (vPoint.Y <= 5)
                            {
                                m.Result = (IntPtr)HTTOPLEFT;
                            }
                            else
                            if (vPoint.Y >= ClientSize.Height - 5)
                            {
                                m.Result = (IntPtr)HTBOTTOMLEFT;
                            }
                            else
                            {
                                m.Result = (IntPtr)HTLEFT;
                            }
                        }
                        else
                        if (vPoint.X >= ClientSize.Width - 5)
                        {
                            if (vPoint.Y <= 5)
                            {
                                m.Result = (IntPtr)HTTOPRIGHT;
                            }
                            else
                            if (vPoint.Y >= ClientSize.Height - 5)
                            {
                                m.Result = (IntPtr)HTBOTTOMRIGHT;
                            }
                            else
                            {
                                m.Result = (IntPtr)HTRIGHT;
                            }
                        }
                        else
                        if (vPoint.Y <= 5)
                        {
                            m.Result = (IntPtr)HTTOP;
                        }
                        else
                        {
                            if (vPoint.Y >= ClientSize.Height - 5)
                            {
                                m.Result = (IntPtr)HTBOTTOM;
                            }
                        }

                    }
                    break;

                default:
                    break;
            }
        }
    }
}




[此贴子已经被作者于2022-10-23 18:19编辑过]

搜索更多相关主题的帖子: 窗体 public const int IntPtr 
2022-04-18 15:35
kloco303
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-2-9
收藏
得分:0 
收到的鲜花
  • 龙胆草2022-12-29 14:25 送鲜花  1朵  
2022-10-22 14:26
龙胆草
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:6
帖 子:55
专家分:230
注 册:2022-6-17
收藏
得分:0 
效果不错,谢谢分享.
2022-12-29 14:57
fhqynij
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-11-20
收藏
得分:0 
下载了,可以运行
2023-04-12 09:44
hongdactc
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-1-2
收藏
得分:0 
效果不错,谢谢分享,喜欢的
2023-05-10 17:18
快速回复:一组窗体代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032167 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved