Programming/Windows API
CreateCursor함수는 새로운 Mouse Cursor를 생성하는 함수입니다.

Declare Function CreateCursor Lib "user32" Alias "CreateCursor" (ByVal hInstance As Integer, ByVal nXhotspot As Integer, ByVal nYhotspot As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal lpANDbitPlane() As Byte, ByVal lpXORbitPlane() As Byte) As Integer
▶VB.NET 선언

Dim andbitMskCur(0 To 127) As Byte
Dim xorbitMskCur(0 To 127) As Byte

andbitMskCur = New Byte() {&HFF, &HFC, &H3F, &HFF, &HFF, &HC0, _
                                    &H1F, &HFF, &HFF, &H0, &H3F, &HFF, _
                                    &HFF, &HC3, &HFF, &HFF, &HFF, &HC3, _
                                    &HFF, &HFF, &HFF, &HC3, &HFF, &HFF, _
                                    &HFF, &HC3, &HFF, &HFF, &HFF, &HC3, _
                                    &HFF, &HFF, &HFF, &HC3, &HFF, &HFF, _
                                    &HFF, &HC3, &HFF, &HFF, &HFF, &HC3, _
                                    &HFF, &HFF, &HFF, &HC3, &HFF, &HFF, _
                                    &HFF, &HC3, &HFF, &HFF, &HFF, &HC3, _
                                    &HFF, &HFF, &HFF, &HC3, &HFF, &HFF, _
                                    &HFF, &HC3, &HFF, &HFF, &HFF, &HC3, _
                                    &HFF, &HFF, &HFF, &HC3, &HFF, &HFF, _
                                    &HFF, &HC3, &HFF, &HFF, &HFF, &HC3, _
                                    &HFF, &HFF, &HFF, &HC3, &HFF, &HFF, _
                                    &HFF, &HC3, &HFF, &HFF, &HFF, &HC3, _
                                    &HFF, &HFF, &HFF, &HC3, &HFF, &HFF, _
                                    &HFF, &HC3, &HFF, &HFF, &HFF, &HC3, _
                                    &HFF, &HFF, &HFF, &HC3, &HFF, &HFF, _
                                    &HFF, &HC3, &HFF, &HFF, &HFF, &HC3, _
                                    &HFF, &HFF, &HFF, &HC3, &HFF, &HFF, _
                                    &HFF, &HC3, &HFF, &HFF, &HFF, &HFF, _
                                    &HFF, &HFF}

xorbitMskCur = New Byte() {&H0, &H0, &H0, &H0, &H0, &H3, _
                                    &HC0, &H0, &H0, &H3F, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0, &H0, &H0, &H0, &H0, _
                                    &H0, &H0}

CreateCursor(Me.Handle, 19, 19, 32, 32, andbitMskCur, xorbitMskCur)

▶VB.NET 호출

[DllImport("user32.dll")]
private static extern int CreateCursor(IntPtr hInstance, int nXhotspot, int nYhotspot, int nWidth, int nHeight, byte[] lpANDbitPlane, byte[] lpXORbitPlane);
▶C# 선언

byte[] andbitMskCur = new byte[] { 0xFF, 0xFC, 0x3F, 0xFF,
                                                0xFF, 0xC0, 0x1F, 0xFF,
                                                0xFF, 0x00, 0x3F, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xC3, 0xFF, 0xFF,
                                                0xFF, 0xFF, 0xFF, 0xFF
            };

byte[] xorbitMskCur = new byte[] { 0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x03, 0xC0, 0x00,
                                                0x00, 0x3F, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00
            };

CreateCursor(this.Handle, 19, 19, 32, 32, andbitMskCur, xorbitMskCur);

▶C# 호출

CreateCursor함수의 첫번째 인수는 Cursor를 생성하는 Program의 Instance를 지정하고 두번째, 세번째 인수로는 실제 Mouse Pointer의 중심으로부터 생성되는 수평, 수직위치를 지정합니다. 네번째 다섯번째 인수는 생성 Cursor의 가로, 세로에 대한 크기값이며 여섯번째, 일곱번째 인수는 생성하고자 하는 Cursor의 모양에 해당하는 Bitmap배열입니다.

이 Bitmap배열의 값에 따라 Cursor의 모양이 달라지는데 실제 CreateCursor함수를 사용하면 결과값으로 생성된 Cursor의 Handle을 반환하게 됩니다. 바로 이 값을 가지고 SetSystemCursor함수를 통해 Cursor를 표시하는 것입니다.

Dim cursorHandle As Integer = CreateCursor(Me.Handle, 19, 19, 32, 32, andbitMskCur, xorbitMskCur)
SetSystemCursor(cursorHandle, 32512)
▶VB.NET - CreateCursor와 SetSystemCursor사용 예제

int cursorHandle = CreateCursor(this.Handle, 19, 19, 32, 32, andbitMskCur, xorbitMskCur);
SetSystemCursor(cursorHandle, 32512);
▶C# - CreateCursor와 SetSystemCursor사용 예제

SetSystemCursor에 대한 자세한 내용은 아래 글을 참고하여 주십시오.

[Develop/Windows API] - SetSystemCursor - 새로운 Mouse Cursor의 설정
0 0