GDI 输出文字、画点、线、三角形、矩形、圆、椭圆、多边形

效果图:

代码:

            hdc=BeginPaint(hwnd,&scPaint);
            if (hdc)
            {
                //输出文本
                SetTextColor(hdc,RGB(255,0,0));
                SetBkColor(hdc,RGB(0,255,0));
                SetBkMode(hdc,TRANSPARENT);
                TextOut(hdc,10,100,"Welcome!",8);
                //画点
                SetPixel(hdc,10,10,RGB(255,0,0));
                //画线,三角形
                HPEN gPen=CreatePen(PS_SOLID,1,RGB(0,255,0));
                HPEN oPen=(HPEN)SelectObject(hdc,gPen);
                MoveToEx(hdc,20,20,NULL);
                LineTo(hdc,100,100);
                LineTo(hdc,400,20);
                LineTo(hdc,20,20);
                //画矩形
                gPen=CreatePen(PS_SOLID,1,RGB(0,255,0));
                HBRUSH bBrush=CreateSolidBrush(RGB(0,0,255));
                oPen=(HPEN)SelectObject(hdc,gPen);
                SelectObject(hdc,bBrush);
                Rectangle(hdc,50,150,150,250);
                //FillRect
                RECT rect;
                rect.left=50;
                rect.top=270;
                rect.right=150;
                rect.bottom=370;
                FillRect(hdc,&rect,CreateSolidBrush(RGB(0,0,255)));
                //FrameRect
                rect.left=50;
                rect.top=380;
                rect.right=150;
                rect.bottom=480;
                FrameRect(hdc,&rect,CreateSolidBrush(RGB(255,0,0)));

                //画圆
                Ellipse(hdc,200,150,300,250);
                //画椭圆
                Ellipse(hdc,200,270,340,370);
                //画多边形
                POINT gPoint[5]={{420,10},{540,140},{600,100},{550,200},{420,10}};
                Polygon(hdc,gPoint,5);

                SelectObject(hdc,oPen);
                DeleteObject(gPen);
                DeleteObject(bBrush);

                EndPaint(hwnd,&scPaint);
                ReleaseDC(hwnd,hdc);
            }

 

 


 

 

转载于:https://www.cnblogs.com/gamesky/archive/2012/08/14/2638028.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 `GraphicsPath` 类来绘制矩形,具体步骤如下: 1. 创建一个 `GraphicsPath` 对象。 2. 使用 `AddArc` 方法添加矩形的四个角的弧线。 3. 使用 `AddLine` 方法在弧线之间绘制直线段。 4. 使用 `CloseFigure` 方法关闭路径。 5. 使用 `DrawPath` 方法将路径绘制到画布上。 下面是一个示例代码: ```csharp private void DrawRoundRect(Graphics g, Pen pen, float x, float y, float width, float height, float radius) { GraphicsPath path = new GraphicsPath(); // 添加左上角弧线 path.AddArc(x, y, radius * 2, radius * 2, 180, 90); // 添加上边线 path.AddLine(x + radius, y, x + width - radius, y); // 添加右上角弧线 path.AddArc(x + width - radius * 2, y, radius * 2, radius * 2, 270, 90); // 添加右边线 path.AddLine(x + width, y + radius, x + width, y + height - radius); // 添加右下角弧线 path.AddArc(x + width - radius * 2, y + height - radius * 2, radius * 2, radius * 2, 0, 90); // 添加下边线 path.AddLine(x + width - radius, y + height, x + radius, y + height); // 添加左下角弧线 path.AddArc(x, y + height - radius * 2, radius * 2, radius * 2, 90, 90); // 关闭路径 path.CloseFigure(); // 绘制路径 g.DrawPath(pen, path); } ``` 在 `Form` 的 `Paint` 事件中调用该方法即可: ```csharp private void Form1_Paint(object sender, PaintEventArgs e) { Pen pen = new Pen(Color.Red, 2); DrawRoundRect(e.Graphics, pen, 50, 50, 200, 100, 20); pen.Dispose(); } ``` 这将绘制一个红色边框、角半径为20的矩形

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值