C#编程技巧集
一 基礎(chǔ)和語法
二
五 GDI+與畫圖
?
?
C#在窗體上輸出字符串
?? 在窗體的Paint事件中,畫字符串“你好!”;
?? 函數(shù)Draw1按創(chuàng)建的畫刷和字體在給定位置輸出給定字符串;
?? 按下button1,輸出“哈哈哈,大笑三聲!”
?? 按下button2,所畫內(nèi)容是為了研究半角字符和全角字符的ascii碼是否相同,把一個ascii字符轉(zhuǎn)換成ascii碼(8位)和轉(zhuǎn)換成32位整型其值是否相等;
?? 按下button3, 輸出自己構(gòu)造的一個DataTable的內(nèi)容和記錄數(shù);
?? 按下button4,輸出自己構(gòu)造的另一個DataTable的內(nèi)容和記錄數(shù);
?
代碼如下:
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);
??????? }
??? }
}
總結(jié)
- 上一篇: VC++编程技巧集
- 下一篇: Windows命令行工具实验