.NET/Windows API for .NET

FillRect - 채워진 사각형 그리기

클리엘 2019. 8. 5. 17:32
728x90

FillRect함수는 안이 채워진 사각형을 그리는 함수입니다.

Declare Function FillRect Lib "user32" Alias "FillRect" (ByVal hdc As Integer, ByRef lpRect As RECT, ByVal hBrush As Integer) As Integer

- VB.NET 선언

[DllImport("user32")]
public static extern int FillRect(int hdc, ref RECT lpRect, int hBrush);

- C# 선언

 

FillRect함수의 첫번째 인수는 실제 사각형을 그릴 Object의 Device Context를 지정해야 합니다.

 

[Windows API for .NET] - GetDC - Window및 Control의 Device Context 구하기

[Windows API for .NET] - GetWindowDC - 현재 Windows화면의 Device Context 구하기

 

FillRect 함수의 두번째 인수는 사각형을 그릴 위치와 크기에 대한 구조체를 기술하는 것으로 먼저 해당 값이 들어갈 구조체를 선언한 다음 선언된 구조체에 위치와 크기값을 정하고 인수로 넘겨줍니다.

마지막 세번째 인수부분에는 실제 사각형을 그려낼 Brush Handle을 지정해야 합니다. Brush는 그림을 그리는 형태에 따라 여러 종류가 있으며 그중에서 만일 Solid형태의 Brush Handle을 구하려면 CreateSolidBrush함수를 사용합니다.

 

[Windows API for .NET] - CreateSolidBrush - Solid형 Line을 그리는 Brush생성

 

만일 파란색으로 채워진 사각형을 왼쪽 10부터 가로 100만큼, 위쪽 20부터 세로 90만큼 그리려면 FillRect함수는 다음과 같이 호출될 수 있습니다.

Public Structure RECT
        Public left As Integer
        Public top As Integer
        Public right As Integer
        Public bottom As Integer
End Structure

Dim idc As Integer
Dim rect As RECT
Dim brh As Integer

idc = GetWindowDC(GetDesktopWindow())

rect.left = 10
rect.Top = 20
rect.right = 100
rect.bottom = 90

brh = CreateSolidBrush(RGB(0, 0, 255))

FillRect(idc, rect, brh)

- VB.NET 호출

public struct RECT
{
            public int left;
            public int top;
            public int right;
            public int bottom;
}

int idc;
RECT rect = default(RECT);
int brh;

idc = GetWindowDC(GetDesktopWindow());

rect.left = 10;
rect.top = 20;
rect.right = 100;
rect.bottom = 90;

brh = CreateSolidBrush(ColorTranslator.ToWin32(Color.Blue));

FillRect(idc, ref rect, brh);

- C# 호출

728x90