程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#用自定義或指定顏色填充矩形

C#用自定義或指定顏色填充矩形

編輯:C#入門知識

貌似Graphics.FillRectangle方法只能用Brush來填充。

GOOGLE了下有兩種方法:   一、用線性漸變填充

 

public void FillByColor(Rectangle rect,Color c,Graphics G)
{
System.Drawing.Drawing2D.LinearGradientBrush lBrush = new System.Drawing.Drawing2D.LinearGradientBrush(rect, c, c, 0);
G.FillRectangle(lBrush, rect);
}


參考文章:《如何:創建線性漸變》

     
二、用API實現指定顏色填充一個閉合區域(未測試) \\用API實現指定顏色填充
using System.Runtime.InteropServices;
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

[DllImport("gdi32.dll")]
public static extern IntPtr CreateSolidBrush(int crColor);

[DllImport("gdi32.dll")]
public static extern bool ExtFloodFill(IntPtr hdc, int nXStart, int nYStart, int crColor, uint fuFillType);

[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);

[DllImport("gdi32.dll")]
public static extern int GetPixel(IntPtr hdc, int x, int y);

public static uint FLOODFILLBORDER = 0;
public static uint FLOODFILLSURFACE = 1;

private void button1_Click(object sender, EventArgs e)
{
Graphics vGraphics = Graphics.FromHwnd(Handle);
vGraphics.DrawRectangle(Pens.Blue, new Rectangle(0, 0, 300, 300));
vGraphics.DrawRectangle(Pens.Blue, new Rectangle(50, 70, 300, 300));
IntPtr vDC = vGraphics.GetHdc();
IntPtr vBrush = CreateSolidBrush(ColorTranslator.ToWin32(Color.Red));
IntPtr vPreviouseBrush = SelectObject(vDC, vBrush);
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved