先給個網址http://www.dotblogs.com.tw/chou/archive/2009/04/12/7986.aspx
產生隨機數的類Random
Random r =new Random();
num=r.Next(1,500);//產生一個1-500之間的隨機數。
使用C#實現移動無邊框窗體
如需瞭解更多請上http://hi.baidu.com/nirvanan/blog/item/e6519b501fe2312343a75b55.html
http://blog.163.com/linyongtang1000/blog/static/12033843220096225744977/
//聲明 API 函數
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
[DllImport("User32.dll", EntryPoint = "ReleaseCapture")]
public static extern int ReleaseCapture();
private const int WM_SYSCOMMAND = 0x0112;
internal const int SC_MOVE = 0xF010;
private const int HTCAPTION = 2;
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
if(MouseButtons.Left == e.Button)
{
//爲當前的應用程序釋放鼠標捕獲
ReleaseCapture();
//發送消息,讓系統誤以爲你在標題攔上按下鼠標
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
}
C#窗體的最大化/最小化/還原/最小化到托盤/NotifyIcon的代碼實現
1.最小化
WindowState = FormWindowState.Minimized;
//最小化時隱藏窗體
this.Visible=false;
2.最大化
WindowState == FormWindowState.Maximized;
3.還原爲正常
WindowState == FormWindowState.Normal;
4.在托盤顯示
打開 VS.net的工具箱,然後選擇NotifyIcon,拖到Form上,在屬性中Icon中設置圖象添加一個ContextMenu,輸入需要的選擇項在NotifyIcon中的ContextMenu屬性中可以設置剛添加的ContextMenu 點托盤上的圖標 就可以顯示選擇項
實例部分代碼:
#region 還原窗體
private void normalForm()
{
//this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
//this.ClientSize = new System.Drawing.Size(504, 267);
this.Visible = true;
//this.WindowState=FormWindowState.Normal;
}
#endregion
#region 最小化窗體並在托盤顯示,隱藏窗體
private void minForm()
{
WindowState = FormWindowState.Minimized;
this.Visible = false;
this.notifyIconCMPC.Visible = true;
this.myTimer.Enabled = false;
//this.Hide();
//設置氣球狀工具提示顯示的時間爲10秒
this.notifyIconCMPC.ShowBalloonTip(30);
}
#endregion
#region 重寫WndProc屏蔽掉關閉按鈕
protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;
if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
{
this.Visible = false;
return;
}
base.WndProc(ref m);
}
#endregion
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (this.WindowState == FormWindowState.Minimized || this.Visible == false)
{
this.normalForm();
}
else
{
minForm();
}
}
如何將窗體添加到容器(panel)中
from2 = new Form2(this);
from2.TopLevel = false;
from2.Parent = panel1;
from2.Show();
如何在控件的設計時得到窗體設計器中的所有控件
- public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
- {
- IContainer ic = context.Container;
- foreach (IComponent cp in ic.Components)
- {
- if (cp is TextBox)
- {
- //執行其他操作
- }
- }
- }