【问题标题】:How do I make a movable transparent form in Visual Basic?如何在 Visual Basic 中制作可移动的透明表单?
【发布时间】:2013-03-26 09:55:37
【问题描述】:

我创建了一个非常简单的数字时钟,作为练习来帮助我提高 vb 技能。我基本上复制了屏幕右下角的计时器,除了我可以在屏幕上移动它,只有当它有彩色背景时。如果我使面板(计时器的父级)透明,则应用程序不再允许我移动它。我想知道是否可以用鼠标移动透明物体?

(下面的整个代码,很简单)

Public Class Form1

将 X、Y 调暗为整数 将 NewPoint 调暗为新 System.Drawing.Point

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 结束子

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 Label1.Click 结束子

Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 Timer1.Tick Label1.Text = TimeOfDay 结束子

Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) 处理 Panel1.MouseDown X = Control.MousePosition.X - Me.Location.X Y = Control.MousePosition.Y - Me.Location.Y 结束子

Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) 处理 Panel1.MouseMove 如果 e.Button = Windows.Forms.MouseButtons.Left 那么 NewPoint = Control.MousePosition 新点.X -= (X) 新点.Y -= (Y) Me.Location = 新点 万一 结束子

Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) 处理 Panel1.Paint

结束子

私有共享函数 hwnd() 只要 抛出新的 NotImplementedException 结束函数

结束类

【问题讨论】:

    标签: transparent movable


    【解决方案1】:

    希望以下示例对您有所帮助。

    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Text;
    using System.Windows.Forms;
    
    namespace WhoAmI
    {
        public partial class Form1 : Form
        {
            private Point _clickPoint;
            private bool _doMove = false;
    
            protected override void OnPaint(PaintEventArgs e)
            {
                //base.OnPaint(e);
    
                SolidBrush text_Brush = new SolidBrush(Color.FromArgb(255, Color.YellowGreen));
                RectangleF text_box = new RectangleF(0, 0, this.Size.Width, this.Size.Height);
                StringFormat f = new StringFormat();
                f.Alignment = StringAlignment.Center;
                f.LineAlignment = StringAlignment.Center;
    
                //e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
                e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
                e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
                //e.Graphics.Clear(Color.Transparent);
                e.Graphics.DrawRectangle(new Pen(Color.YellowGreen, 1), new Rectangle(0, 0, this.Size.Width, this.Size.Height));
                //e.Graphics.FillRectangle(Brushes.Transparent, new Rectangle(0, 0, this.Size.Width - 1, this.Size.Height - 1));
                e.Graphics.DrawString(System.Environment.MachineName, new Font("Arial", 18, FontStyle.Bold), text_Brush, text_box, f);
            }
    
            //Completely shown but not clickable!
            //protected override CreateParams CreateParams
            //{
            //    get
            //    {
            //        CreateParams parms = base.CreateParams;
            //        parms.ExStyle |= 0x20;  // Turn on WS_EX_TRANSPARENT
            //        return parms;
            //    }
            //}
    
            protected override void OnPaintBackground(PaintEventArgs e)
            {
                //base.OnPaintBackground(e);      
                e.Graphics.FillRectangle(Brushes.Transparent, new Rectangle(0, 0, this.Size.Width - 1, this.Size.Height - 1));
            }
    
            public Form1()
            {
                InitializeComponent();
    
                this.BackColor = Color.White;
                this.TransparencyKey = this.BackColor;
                //this.Opacity = 1F;
            }
    
            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                this._doMove = true;
    
                this._clickPoint = new Point(e.X, e.Y);
            }
            private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (this._doMove)
                {
                    if (e.Button == MouseButtons.Left)
                    {
                        this.Location = new Point(this.Left + e.X - this._clickPoint.X, this.Top + e.Y - this._clickPoint.Y);
                    }
                }
            }
            private void Form1_MouseUp(object sender, MouseEventArgs e)
            {
                this._doMove = false;
            }
            private void Form1_MouseLeave(object sender, EventArgs e)
            {
                this._doMove = false;
            }
            private void Form1_MouseClick(object sender, MouseEventArgs e)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    if (MessageBox.Show("Exit this app?", "Who Am I?", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    {
                        this.Close();
                        this.Dispose();
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-15
      • 2015-12-02
      • 1970-01-01
      相关资源
      最近更新 更多