您的位置:首页 > 产品设计 > UI/UE

对Visual Stuido .NET 2005 BEAT 2 的帮助文档的翻译(应用篇)

2005-12-16 13:49 525 查看

4.1 C#应用事例专题(Visual C#)

下面的专题包括Visual C# Express Edition中创建Windows窗体的代码事例。

4.1.1 应用部分

一、改变窗体的背景颜色

下面的例子改变窗体的背景颜色。

private void Form1_Click(object sender, System.EventArgs e)
{
this.BackColor = System.Drawing.Color.DarkBlue;
}
①编译
要求:
需要一个名为Form1的窗体和建立一个Form_Click事件。

二、创建一个其他形状的窗体

下面的例子创建了一个椭圆型的窗体。

protected override void OnPaint( System.Windows.Forms.PaintEventArgs e )
{
System.Drawing.Drawing2D.GraphicsPath shape = new System.Drawing.Drawing2D.GraphicsPath();
shape.AddEllipse(0, 0, this.Width, this.Height);
this.Region = new System.Drawing.Region(shape);
}



①编译
要求:
这个例子重载(overrides)OnPaint方法,从而改变了Windows窗体的形状。要使用这个代码,只要像画图代码(the drawing code)的声明一样,将上面的代码拷贝到方法中即可。
窗体类(Form Class)的Region属性是一个高级成员。

三、动态显示OpenFileDialog

下面的例子实例化一个OpenFileDialog而且展示了一个OpenFileDialog控件的使用。

OpenFileDialog openFile = new OpenFileDialog();
openFile.DefaultExt = "doc";
// The Filter property requires a search string after the pipe //( | )
openFile.Filter = "Word documents (*.doc)|*.doc";
openFile.ShowDialog();
if( openFile.FileNames.Length > 0 )
{
foreach( string filename in openFile.FileNames )
{
// Insert code here to process the files.
}
}

实现效果



①更多技巧
你可以用OpenFileDialog控件的FileOK事件确定用户已经选定了一个文件而且点击了OK按钮。
使用CheckFileExist,CheckPathExist,DefaultExtension,Filter,MultiSelect和ValidateNames属性可以减少运行时的错误。

四、动态创建一个窗口

下面的例子展示了如何从另一个窗口动态地创建一个窗口。

private void button1_Click(object sender, System.EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}



①编译
要求:
需要两个名为Form1和Form2的窗口。Form1包含了一个名为Button1的控件。把Button1的Event事件委托到Button1_Click。

五、在Windows窗体上画图

下面这个例子教我们怎么在一个窗口上画一个圆和正方形。

System.Drawing.Graphics graphics = this.CreateGraphics();
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);



①编译
这段代码将被添加到由System.Windows.Forms.Form继承来的类中。其中this指的是当前窗体的实例。

六、创建一个上下文菜单并且将它绑定到组件

下面的例子逐步创建一个上下文菜单( ContextMenu )并且将它绑定到控件上。

private void Form1_Load(object sender, System.EventArgs e)
{
System.Windows.Forms.ContextMenu contextMenu1;
contextMenu1 = new System.Windows.Forms.ContextMenu();
System.Windows.Forms.MenuItem menuItem1;
menuItem1 = new System.Windows.Forms.MenuItem();
System.Windows.Forms.MenuItem menuItem2;
menuItem2 = new System.Windows.Forms.MenuItem();
System.Windows.Forms.MenuItem menuItem3;
menuItem3 = new System.Windows.Forms.MenuItem();

contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {menuItem1, menuItem2, menuItem3});
menuItem1.Index = 0;
menuItem1.Text = "MenuItem1";
menuItem2.Index = 1;
menuItem2.Text = "MenuItem2";
menuItem3.Index = 2;
menuItem3.Text = "MenuItem3";

textBox1.ContextMenu = contextMenu1;
}



①编译
要求:
一个名为Form1的窗口和一个名为textbox1的TextBox的控件。把Form1的Load事件委托到Form_Load。

七、打印一个窗体

下面这个例子展示了打印一个当前窗口的拷贝。

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void CaptureScreen()
{
Graphics mygraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
private void printButton_Click(System.Object sender, System.EventArgs e)
{
CaptureScreen();
printDocument1.Print();
}

①编译
要求:
一个名为printDocument1的PrintDocument控件,并且有一个PrintPage事件。
一个名为PrintButton的Button控件,并且有一个Click事件。
高级技巧
②下面的情况会导致异常:
·没有权限访问打印机。
·没有权限使用不被管理的代码。
·没有安装打印机。

八、打印预览窗体

下面的例子展示了如何打印预览一个窗体。

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void CaptureScreen()
{
Graphics mygraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
private void printButton_Click(System.Object sender, System.EventArgs e)
{
CaptureScreen();
printPreviewDialog1.Show();
}

九、打印文本文件

下面的文件展示了如何打印一个文本文件。

System.IO.StreamReader fileToPrint;
System.Drawing.Font printFont;
private void printButton_Click(object sender, EventArgs e)
{
string printPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
fileToPrint = new System.IO.StreamReader(printPath + @"/myFile.txt");
printFont = new System.Drawing.Font("Arial", 10);
printDocument1.Print();
fileToPrint.Close();
}

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float yPos = 0f;
int count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
string line = null;
float linesPerPage = e.MarginBounds.Height/printFont.GetHeight(e.Graphics);
while (count < linesPerPage)
{
line = fileToPrint.ReadLine();
if (line == null)
{
break;
}
yPos = topMargin + count * printFont.GetHeight(e.Graphics);
e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
count++;
}
if (line != null)
{
e.HasMorePages = true;
}
}

①编译需求:一个名为printButton的Button控件,并且设定它的Click事件。一个名位printDocument1的PrintDocument控件,并且设定它的PrintPage事件。例程需要在你的桌面上有一个名为myfile.txt的文件。例程的代码会替代已经存在的事件委托。②高级技巧文件操作应该被try...catch...finally块包含。当你调用文件操作完成之后应该调用StreamReader.Close。下面的几种情况可能产生异常:·你没有访问打印机的权限。·你没有访问文件系统的权限。·你没有安装打印机。·你没有安装Arial字体。③安全为了能运行这段程序,你要有访问打印机和文件系统的权限。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐