2013-03-26 92 views
1

我創建了一個非常簡單的數字時鐘,就像練習幫助我的vb技能一樣。我已經基本上將定時器複製到屏幕的右下角,除了我可以在屏幕上移動它,只有當它具有彩色背景時。如果我使面板(定時器的父級)透明,應用程序不再允許我移動它。我想知道是否可以用鼠標移動透明物體?如何在Visual Basic中創建可移動的透明窗體?

(下面的整個代碼,非常簡單)

Public Class Form1 

昏暗X,Y作爲整數 昏暗NewPoint作爲新System.Drawing.Point

私人小組TextBox1_TextChanged(BYVAL發件人爲System.Object的,BYVALË作爲System.EventArgs) 結束子

私人小組Label1_Click(BYVAL發件人爲System.Object的,BYVALË作爲System.EventArgs)把手Label1.Click 結束子

私人小組Timer1_Tick_1(BYVAL發件人爲System.Object的,BYVALË作爲System.EventArgs)把手Timer1.Tick Label1.Text =的TimeOfDay 結束子

私人小組Panel1_MouseDown(BYVAL發件人爲對象,BYVALÈ作爲System.Windows.Forms.MouseEventArgs)把手Panel1.MouseDown X = Control.MousePosition.X - Me.Location.X Y = Control.MousePosition.Y - Me.Location.Y 結束子

私人小組Panel1_MouseMove(ByVal sender As Object,ByVal e As System.Windows.Forms.MouseEventArgs)處理Panel1.MouseMov Ë 如果e.Button = Windows.Forms.MouseButtons.Left然後 NewPoint = Control.MousePosition NewPoint.X - =(X) NewPoint.Y - =(Y) Me.Location = NewPoint 結束如果結束 子

私人小組Panel1_Paint(BYVAL發件人爲System.Object的,BYVALË作爲System.Windows.Forms.PaintEventArgs)把手Panel1.Paint

結束子

私人共享功能HWND()只要 拋出新的NotImplementedException 端功能

末級

回答

0

希望下面的示例會幫助你。

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(); 
       } 
      } 
     } 
    } 
}