[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[读书笔记]C#进行windows程序设计[01]

上一篇:[整理]一些日语词汇的输入
下一篇:[整理]测试ASP使用SA FileUp组件上传的一小段代码。

添加日期:2008/3/4 14:43:08 快速返回   返回列表 阅读4535次
(1)编译:
/target:exe 是控制台程序(Console Application)。
/target:winexe 是windows程序(Windows Application)。
当然,还有其它的,库、模块等等。

必须的dll:
/r:System.dll,System.Windows.Forms.dll,System.Drawing.dll
工程中需要引用这三个dll。

运行:
发布Release模式,程序结束时,控制台会有Press any key to continue,可以查看输出。
调试Debug模式,则控制台直接消失。

(2)
System.Windows.Forms.MessageBox类。
MessageBox.Show()静态方法。

DialogResult rtn = MessageBox.Show("xxxxxxxxxx","myForm",MessageBoxButtons.OKCancel);
switch(rtn)
{
    case DialogResult.OK:
        MessageBox.Show("OK");
        break;
    case DialogResult.Cancel:
        MessageBox.Show("Cancel");
        break;
}
MessageBox.Show(System.Environment.OSVersion.Platform.ToString());

(3)
System.Windows.Forms.Form类继承自:
Object--MarshalByRefObject--Component--Control--ScrollableControl--ContainerControl

(4)
using System.Windows.Forms;

class ShowForm
{
    public static void Main()
    {    
        Form A=new Form();
        Form B=new Form();

        A.Text="A";
        B.Text="B";
        B.Show();

        Application.Run(A);

        MessageBox.Show("end");
    }
}

两个窗口,B先显示,然后A出来。

此时,此段代码就暂停了,直至A窗口被关闭(总之要返回),继续执行,显示end。

关闭B窗口,对A没有任何影响。但是如果关闭A,则B也被关闭。

即Application.Run返回时,会同时关闭当前代码创建的所有其它窗口。

Application.Run自动使窗口显示,所以不用调用show方法。

(5)
事件触发机制,功能一般都在事件触发的方法中实现。
触发的事件在一个线程中,是依次执行的。?

(6)Paint事件
必要的时候要刷新窗口显示,重绘。

using System;
using System.Drawing;
using System.Windows.Forms;

class ShowForm
{
    public static void Main()
    {    
        Form A=new Form();
        A.Text="A";
        A.BackColor=Color.BlanchedAlmond;
        A.Width=500;
        A.Height=300;
        A.FormBorderStyle=FormBorderStyle.Fixed3D;
        A.Cursor=Cursors.Hand;
        A.StartPosition =FormStartPosition.CenterScreen;

        A.Paint += new PaintEventHandler(MyPaintHandler);

        Application.Run(A);
    }
    static void MyPaintHandler(object objSender,PaintEventArgs pea)
    {
        Form f = (Form)objSender;
        Graphics g = pea.Graphics;
        g.DrawString("Hello World!",f.Font,Brushes.Black,50,20);
        '输出字符,字体,笔刷,X坐标,Y坐标
        '坐标相对于内容区(不包括标题栏,菜单等等)的左上角。
    }
}

不要在Paint事件中放MessageBox.show,它会引起又一次Paint事件,不停的循环。
不要放置累积的工作,比如放大2倍,则每次都放大2倍。
可以有多个Paint事件的方法,按添加顺序依次调用。

(7)
继承Form类,通常将初始化工作放到构造器中。

using System;
using System.Drawing;
using System.Windows.Forms;

class myForm : Form
{
    public static void Main()
    {
        Application.Run(new myForm);
    }
    public myForm()
    {
        this.Text = "myForm";
        this.BackColor = Color.White;
    }
    protected override void OnPaint(PaintEventArgs pea)
    {
        base.OnPaint(pea); //调用基类的方法。

        //覆盖OnPaint方法,就不必添加Paint事件了。
        Graphics g=pea.Graphics;
        g.DrawString("Hello Forms!",Font,Brushes.Black,300,50);
    }
}
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved