[C#][VB.NET]自製桌面小玩意
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If PictureBox1.Capture = True Then '如果滑鼠按著拖曳
'設定新的視窗位置
Me.Top = e.Y + nOldWndTop - nClickY
Me.Left = e.X + nOldWndLeft - nClickX
'更新紀錄的視窗位置
nOldWndLeft = Me.Left
nOldWndTop = Me.Top
End If
End Sub</pre></div><p> </p><p>C#</p><p> </p><div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:f0c57834-e54e-4456-896a-5ebc58634d32" class="wlWriterSmartContent"><pre class="c#" name="code">
private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
{
//紀錄滑鼠點選時的視窗位置與滑鼠點選位置
nOldWndLeft = this.Left;
nOldWndTop = this.Top;
nClickX = e.X;
nClickY = e.Y;
}
private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (pnlTitleBar.Capture == true) //如果滑鼠按著拖曳
{
//‘設定新的視窗位置
this.Top = e.Y + nOldWndTop - nClickY;
this.Left = e.X + nOldWndLeft - nClickX;
//更新紀錄的視窗位置
nOldWndLeft = this.Left;
nOldWndTop = this.Top;
}
} Step12.完成 完整範例如下:VB.NET Public Class Form1 Dim nOldWndLeft As Integer Dim nOldWndTop As Integer Dim nClickX As Integer Dim nClickY As Integer
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
'紀錄滑鼠點選時的視窗位置與滑鼠點選位置
nOldWndLeft = Me.Left
nOldWndTop = Me.Top
nClickX = e.X
nClickY = e.Y
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If PictureBox1.Capture = True Then '如果滑鼠按著拖曳
'設定新的視窗位置
Me.Top = e.Y + nOldWndTop - nClickY
Me.Left = e.X + nOldWndLeft - nClickX
'更新紀錄的視窗位置
nOldWndLeft = Me.Left
nOldWndTop = Me.Top
End If
End Sub
End Class C# using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace WindowsFormsApplication1 { public partial class Form1 : Form { int nOldWndLeft; int nOldWndTop; int nClickX; int nClickY;
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
//紀錄滑鼠點選時的視窗位置與滑鼠點選位置
nOldWndLeft = this.Left;
nOldWndTop = this.Top;
nClickX = e.X;
nClickY = e.Y;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (pictureBox1.Capture == true) //如果滑鼠按著拖曳
{
//'設定新的視窗位置
this.Top = e.Y + nOldWndTop - nClickY;
this.Left = e.X + nOldWndLeft - nClickX;
//更新紀錄的視窗位置
nOldWndLeft = this.Left;
nOldWndTop = this.Top;
}
}
}
} Conclusion這篇以一個簡單的小範例試範了如何利用.NET自製類似widget的桌面小玩意。雖然是簡單的概念,但是卻十分的實用。像是桌曆、桌面量尺、電子寵物…等,都是可以利用此概念來寫出來的。全看自我的想像力。有興趣的可以試著擴充此範例,像是加上滑鼠移上去或按下時彎彎會換動作、或是讓彎彎在電腦上跑來跑去..等。