先给个网址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) { //执行其他操作 } } }