1

I have created a really simple digital clock, just as practice to help my vb skills. I have basically replicated the timer in bottom right hand corner of your screen, except I can move it around the screen, ONLY when it has a colored background. If I make the panel (parent of the timer) transparent, the application no longer allows me to move it around. I was wondering if it is possible to be able to move transparent objects with the mouse?

(The entire code below, pretty simple)

Public Class Form1

Dim X, Y As Integer Dim NewPoint As New System.Drawing.Point

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) End Sub

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click End Sub

Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Label1.Text = TimeOfDay End Sub

Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown X = Control.MousePosition.X - Me.Location.X Y = Control.MousePosition.Y - Me.Location.Y End Sub

Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove If e.Button = Windows.Forms.MouseButtons.Left Then NewPoint = Control.MousePosition NewPoint.X -= (X) NewPoint.Y -= (Y) Me.Location = NewPoint End If End Sub

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

End Sub

Private Shared Function hwnd() As Long Throw New NotImplementedException End Function

End Class

Daniel
  • 51
  • 1
  • 5

1 Answers1

0

Hope the following sample will help you.

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();
                }
            }
        }
    }
}
mtsiakiris
  • 190
  • 9