[C#]桌面模式下GDI绘制(全透明背景)

有时候我们需要这样的需求:在桌面模式下当画板用,在上面写写画画,添加一些批注等。
很自然我们会想到用GDI绘制,可是用GDI,免不了要有一块“板”绘制,也就是免不了窗体,既然要在桌面模式下,那现在的问题就转到如何把窗体设置成全透明模式。透明,很容易想到窗体的Opacity属性,可是若把这个设成全透明,那完全没效果,根本找不到窗体在哪,运行就跟没运行一样,更不用说获得窗体焦点了,可见这条路是行不通的。
难道就没办法了吗?好在除了Opacity属性,窗体还有一个属性叫TransparencyKey,只要把该属性和窗体背景色(BackColor)设成一样的颜色,运行后的窗体也是全透明的,可是好不容易找到这条路,却发现还有另一重大问题,即运行后的窗体死活都得不到焦点,更别想在窗体上绘制任何东西,似乎又到了另一个“死胡同”,如何解决??

此时我想起之前写过一篇简易画板,按照原先的操作重复尝试了下,没想到意外的重大收获来得这么快!~就是不要直接在窗体上绘制,在窗体加一个PictureBox控件,在PictureBox上绘制就完全达到了所要的效果,先睹为快先!~


只有PictureBox(黑色方框)区域内才有绘制焦点,而其他区域(包括Label控件区域)获取不到焦点。

下面这是设计窗体


附上测试源码

        Bitmap originImg;
        Image finishImg;
        Graphics g;
        Pen p = new Pen(Color.Black, 5);
        bool startDraw;
        List<Point> lstPoint = new List<Point>();

        public Form1()
        {
            InitializeComponent();
            //设置双缓冲,避免闪烁
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
            this.UpdateStyles();

            //设置笔的属性,避免出现“毛刺”
            p.StartCap = LineCap.Round;
            p.EndCap = LineCap.Round;
            p.LineJoin = LineJoin.Round;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            originImg = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            g = Graphics.FromImage(originImg);
            //画布背景初始化为白底  
            g.Clear(Color.White);

            pictureBox1.Image = originImg;
            finishImg = (Image)originImg.Clone();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                startDraw = true;

                lstPoint.Clear();
                lstPoint.Add(e.Location);
                finishImg = (Image)originImg.Clone();
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (startDraw)
            {
                g = Graphics.FromImage(finishImg);
                g.SmoothingMode = SmoothingMode.AntiAlias; //抗锯齿  

                lstPoint.Add(e.Location);
                g = Graphics.FromImage(finishImg);
                g.DrawCurve(p, lstPoint.ToArray());

                reDraw();
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            startDraw = false;
            originImg = (Bitmap)finishImg;

            pictureBox1.Image = originImg;
        }
        /// <summary>  
        /// 重绘绘图区(二次缓冲技术)  
        /// </summary>  
        private void reDraw()
        {
            using (Graphics graphics = pictureBox1.CreateGraphics())
            {
                graphics.DrawImage(finishImg, new Point(0, 0));
            }
        }

总结:其实就是之前那篇简易画板加个透明窗体背景而已


特别说明:本文所提供的方案仅限XP系统下使用

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值