gdi+代码2

本文介绍了一个使用C#进行窗体绘制的例子,演示了如何创建窗体、填充背景色、绘制矩形、添加文字,并展示了字体样式设置及字符串对齐方式等技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace WindowsApplication3
{
    public partial class Form1 : Form
    {

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            int y = 0;
            g.FillRectangle(Brushes.White , ClientRectangle);//这句就是把目前的整个窗口汇成了一个白色的底面

            Rectangle rect = new Rectangle(0, y, 400, Font.Height);//这样写的却是很好了
            g.DrawRectangle(Pens.Blue, rect);
            g.DrawString("this is lest texts", Font, Brushes.Black, rect);////先画一个诓,然后写了一些字在上面

            y += Font.Height + 20;// 这样可以控制位置

            Font afont=new Font ("Arial",16, FontStyle .Bold |FontStyle .Italic);//这样就很有意思了
            rect=new Rectangle (0,y,400,afont .Height*4);

            StringFormat af = new StringFormat();
            af.Alignment = StringAlignment.Far;
            g.DrawRectangle (Pens.Yellow ,rect );
            string longstr = "■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■";
            longstr += "■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■";
            longstr += "this is also a right  texts■■■■■■■■■■■■■■■■■■■■";//就是这样的了
           
            g.DrawString (longstr ,afont ,Brushes .BlueViolet ,rect ,af );//注意这里用了af 和 format的形式来右写
            afont.Dispose();
            af.Dispose();//养成良好的习惯了



            base.OnPaint(e);
        }
        public Form1()
        {
            InitializeComponent();
            SetStyle(ControlStyles.Opaque, true);//控件被设置为不显示背景和不透明
            ////结果真的是很怪异的东西了。
            Bounds =new Rectangle (0,0,500,300);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值