日期:2013-12-06  浏览次数:20372 次

1,MDI窗体
设有两个窗体frmMain,frmChild,则:
frmMain: 设IsMdiContainer属性为true
打开子窗口:
在相关事件中写如下代码:
frmChild child=new frmChild();
child.MdiParent=this;//this表示本窗体为其父窗体
child.Show();
在打开子窗体时,如果只允许有一个子窗体,可以加入如下判断:
if (this.ActiveMdiChild!=null)
{
this.ActiveMdiChild.Close(); //关闭已经打开的子窗体
//....
}
更改MDI主窗体背景
先声明一个窗体对象
private System.Windows.Forms.MdiClient m_MdiClient;
在Form_Load等事件中,添加如下代码:
int iCnt=this.Controls.Count;
for(int i=0;i<iCnt;i++)
{
if(this.Controls[i].GetType().ToString()=="System.Windows.Forms.MdiClient")
{
this.m_MdiClient=(System.Windows.Forms.MdiClient)this.Controls[i];
break;
}
}
this.m_MdiClient.BackColor=System.Drawing.Color.Silver;
具体可参见:http://cnblogs.com/Daview/archive/2004/05/06/8381.aspx

2,创建系统托盘菜单
2.1,创建一个contextMenu(cmnMain)菜单
2.2,添加一个NotifyIcon组件,设置ContextMenu属性为cmnMain
2.3,相应窗体改变事件(最小化等)
private void frmMain_SizeChanged(object sender,EventArgs e)
{
if (this.WindowState==FormWindowState.Minimized)
{
this.Hide();
noiMain.Visible=true;
}
}

2.4,相应用户单击系统托盘上contextmenu菜单事件
private void mniOpen(object sender,EventArgs e)
{
noiMain.Visible=false;
this.Show();
this.Focus();
}

2.5,响应用户双击系统托盘图标事件
private void noiMain_DoubleClick(object s,EventArgs e)
{
minOpen.PerformClick(); //相当与mniOpen按钮的单击事件
}
**注意添加相应的事件句柄**

3,创建不规则窗体
3.1,在窗体上创建不规则图象,可以用gdi+绘制,或在图象控件上使用图象填充
3.2,设置窗体的backcolor为colorA,然后设置TransparencyKey为colorA
3.3,设置FormBorderStyle为none;

4,创建顶部窗体
this.TopMost=true;//把窗体的TopMost设置为true


5,调用外部程序

using System.Diagnostics

Process proc=new Process();
proc.StartInfo.FileName=@"notepad.exe"; //注意路径
proc.StartInfo.Arguments="";
proc.Start();

//获得当前目录Directory.GetCurrentDirectory() (using System.IO)

6,Toolbar的使用
Toolbar控件通常需要imagelist控件结合使用(需要用到其中图标)
响应Toolbar单击事件处理程序代码:
switch(ToolbarName.Buttons.IndexOf(e.Button))
{
case 0: //第一个按钮
//code ...
break;
case 1: //第二个按钮
//code ...
break;
//other case code
default: //默认处理,但以上所有项都不符合时
//code ...
break;
}

7,弹出对话框获得相关返回值
在窗体的closing事件中运行如下代码,可以在用户关闭窗体时询问
DialogResult result=MessageBox.Show(this,"真的要关闭该窗口吗?","关闭提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
if (result==DialogResult.OK)
{
//关闭窗口
e.Cancel=false;
}
else
{
//取消关闭
e.Cancel=true;
}

8,打印控件
最少需要两个控件
PrintDocument
PrintPreviewDialog:预览对话框,需要printdocument配合使用,即设置document属性为
对应的printDocument
printdocument的printpage事件(打印或预览事件处理程序)代码,必须.

float fltHeight=0;
float fltLinePerPage=0;
long lngTopMargin=e.MarginBounds.Top;
int intCount=0;
string strLine;

//计算每页可容纳的行数,以决定何时换页
fltLinePerPage=e.MarginBounds.Height/txtPrintText.Font.GetHeight(e.Graphics);


while(((strLine=StreamToPrint.ReadLine()) != null) && (intCount<fltLinePerPage))
{
intCount+=1;
fltHeight=lngTopMargin+(intCount*txtPrintText.Font.GetHeight(e.Graphics));
e.Graphics.DrawString(strLine,txtPrintText.Font,Brushes.Green,e.MarginBounds.Left,fltHeight,new StringFormat());
}

//决定是否要换页
if (strLine!=null)
{
e.HasMorePages=true;
}
else
{
e.HasMorePages=false;
}
以上代码的StreamToPrint需要声明为窗体级变量:
private System.IO.StringReader StreamToPrint;

打开预览对话框代码(不要写在printpage事件中)