C#编程技巧集

一 基础和语法

五 GDI+与画图

 

 

C#在窗体上输出字符串

   在窗体的Paint事件中,画字符串“你好!”;
   函数Draw1按创建的画刷和字体在给定位置输出给定字符串;
   按下button1,输出“哈哈哈,大笑三声!”
   按下button2,所画内容是为了研究半角字符和全角字符的ascii码是否相同,把一个ascii字符转换成ascii码(8位)和转换成32位整型其值是否相等;
   按下button3, 输出自己构造的一个DataTable的内容和记录数;
   按下button4,输出自己构造的另一个DataTable的内容和记录数;

 

C#编程技巧集_第1张图片

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace str1
{
    public partial class Form1 : Form
    {
        DataTable namesTable,namesTable2;

        public Form1()
        {
            InitializeComponent();
            this.Paint += new System.Windows.Forms.PaintEventHandler(Form1_Paint);
            namesTable = new DataTable("JHYDJF");
            namesTable2 = new DataTable("JHYDJF2");
        }       

        private void button1_Click(object sender, EventArgs e)
        {
            string a = "哈哈哈,大笑三声!";
            Draw1(a,50,50);           
        }
       
        void Draw1(string str1,int xPos=0,int yPos=0)
        {
            SolidBrush aBrush = new SolidBrush(Color.Blue);
            Font aFont = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Millimeter);

            Graphics graphic = this.CreateGraphics();
            graphic.DrawString(str1, aFont, aBrush, xPos, yPos);
        }

        void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Font f = new Font("宋体", 10);
            g.DrawString("你好!", f, Brushes.Black, 20, 20);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            char a = '(';
            char b = '(';

            byte a1 = Convert.ToByte(a);
            int b1 = Convert.ToInt32(b);
            int ia1 = Convert.ToInt32(a);
            Draw1(Convert.ToString(a1),100,100);
            Draw1(Convert.ToString(b1),100,130);
            Draw1(Convert.ToString(ia1), 100, 160);


        }

        private void CreateDSet()
        {
           

            DataColumn numColumn = new DataColumn();
            numColumn.DataType = System.Type.GetType("System.String");
            numColumn.ColumnName = "num";
            namesTable.Columns.Add(numColumn);

            DataColumn feColumn = new DataColumn();
            feColumn.DataType = System.Type.GetType("System.Int32");
            feColumn.ColumnName = "fe";
            namesTable.Columns.Add(feColumn);

         }

        private void button3_Click(object sender, EventArgs e)
        {
            DataRow row;
            CreateDSet();
            for (int i = 0; i < 5; i++)
            {
               
                row = namesTable.NewRow();
                row["num"] = "15652303852";
                row["fe"] = "1";
                namesTable.Rows.Add(row);
            }

            for (int j = 0; j < 5; j++)
            {
                Draw1(namesTable.Rows[j][0].ToString(), 100, 200 + j * 35);
                Draw1(namesTable.Rows[j][1].ToString(), 400, 200 + j * 35);
            }
            Draw1(namesTable.Rows.Count.ToString(), 50, 200 );
        }

        private void CreateDSet2()
        {
            DataColumn numColumn2 = new DataColumn();
            numColumn2.DataType = System.Type.GetType("System.String");
            numColumn2.ColumnName = "num2";
            namesTable2.Columns.Add(numColumn2);

            DataColumn feColumn2 = new DataColumn();
            feColumn2.DataType = System.Type.GetType("System.Int32");
            feColumn2.ColumnName = "fe2";
            namesTable2.Columns.Add(feColumn2);

            DataRow row;
            for (int i = 0; i < 5; i++)
            {
                row = namesTable2.NewRow();
                row["num2"] = "15652303852";
                row["fe2"] = "1";
                namesTable2.Rows.Add(row);
            }
            namesTable2.Rows[2][0] = "13900000001";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            CreateDSet2();
            for (int i = 0; i < 5; i++)
            {
                Draw1(namesTable2.Rows[i][0].ToString(), 100, 450 + i * 30);
                Draw1(namesTable2.Rows[i][1].ToString(), 500, 450 + i * 30);
            }
            Draw1(namesTable2.Rows.Count.ToString(), 50, 450);

        }

    }
}

你可能感兴趣的:(程序演示)