기본 화살표 커서 모양을 다른 커서 모양으로 변경하는 방법이다.
// Win32 API 선언
[DllImport("user32.dll")]
static extern bool SetSystemCursor(IntPtr hcur, uint id);
[DllImport("user32.dll")]
static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);
[DllImport("user32.dll")]
public static extern IntPtr CopyIcon(IntPtr pcur);
[DllImport("user32.dll")]
public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni);
// 커서 ID 정의
private const uint OCR_NORMAL = 32512;
private const uint OCR_IBEAM = 32513;
private const uint OCR_CROSS = 32515;
// 마우스 커서 모양 변경하기
try
{
this.Cursor = Cursors.Cross;
// For the Entire Application
Cursor.Current = Cursors.Cross;
// 시스템의 기본 화살표 커서를 십자 모양(Cross)으로 변경 예시
IntPtr crossCursor = LoadCursor(IntPtr.Zero, 32515); // 32515는 OCR_CROSS
SetSystemCursor(CopyIcon(crossCursor), OCR_NORMAL);
MessageBox.Show(OCR_NORMAL.ToString()); // 결과값 32512 = OCR_NORMAL -> OCR_CROSS 로 변경됨
}
catch (Exception ex)
{
// 에러 발생시 기본 모양으로 변경
SystemParametersInfo(0x0057, 0, IntPtr.Zero, 0);
MessageBox.Show(ex.Message);
}
finally
{
// 변경된 시스템의 커서 모양(Cross)을 기본 화살표로 변경
SystemParametersInfo(0x0057, 0, IntPtr.Zero, 0);
}
1. 폼에서 'Pick' 버튼을 클릭하면 마우스 커서 모양이 십자(Cross) 로 변경된다.
2. 메세지 창에 'OCR_NORMAL.ToString()' 결과 값이 출력되었고, 커서 모양이 십자 모양으로 변경되어 있다.
3. 확인 버튼을 클릭하면 커서 모양이 원래의 화살표 모양으로 복원된다.