PDA

View Full Version : Changing the mouse cursor



Christian_Rud
November 14, 2007, 11:25:53
Hello,

is there any possibility to change the mouse cursor back to the standard (arrow) cursor when using your icsetcursor.dll?

I'm using C# for my project.

Christian Linkenheld

Stefan Geissler
November 14, 2007, 12:19:54
Hello,

I am very sorry, but it seems, this is not possible with this simple DLL. The DLL uses following C functions in order to find the IC Window and set the cursor:


long WINAPI FindICWindow(HWND ParentWindow, char* szCursorName)
{
HWND hHICWnd = NULL;
long h = 0;


EnumChildWindows(ParentWindow,EnumChildProc,(LPARA M) &hHICWnd);
if(hHICWnd != NULL)
{
if( hCursor )
{
DestroyCursor( (HCURSOR)hCursor );
}
hCursor = LoadImage(0,szCursorName,IMAGE_CURSOR,0,0,LR_LOADF ROMFILE|LR_DEFAULTSIZE);
if( hCursor )
{
SetClassLong(hHICWnd, GCL_HCURSOR, (LONG) hCursor);

h = 1;
}
}

return h;
}

SetClassLong is the interesting Win32 API call.

The callback function for EnumChildWindows is:


BOOL CALLBACK EnumChildProc( HWND hwnd, LPARAM lParam )
{
char cClassName[255];
GetClassName( hwnd, cClassName,254);
if( strncmp(cClassName,"ATL:",4) == 0 || strcmp(cClassName,"VideoRenderer") == 0)
{
*((long*)lParam) = (LPARAM)hwnd;
if( strncmp(cClassName,"ATL:",4) == 0 )
{
EnumChildWindows(hwnd,EnumChildProc,lParam);
}

return FALSE;
}
return TRUE;
}


May be you can implement something like this directly in C#, then you would be able to set the cursor as required.

Christian_Rud
November 16, 2007, 11:57:34
Hello,

thank you for your hints.
I found a C#-Solution using PInvoke. It switches the cursor between the predefined arrow and cross shapes. It is done a little bit by the fast way - but at the moment it works.



[DllImport("user32")]
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);

[DllImport("user32.dll")]
static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);

[DllImport("user32.dll")]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

[DllImport("user32.dll")]
internal static extern int SetClassLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

public static int IDC_CROSS = 32515;
public static int IDC_ARROW = 32512;
private Boolean defCursor = true;
public static int GCL_HCURSOR = -12;




public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

private void btnSwitchCursor_Click(object sender, EventArgs e) //
{
EnumWindowProc childProc;
if (defCursor)
{
defCursor = false;
childProc = new EnumWindowProc(EnumWindowCross);
}
else
{
defCursor = true;
childProc = new EnumWindowProc(EnumWindowArrow);
}
IntPtr fenster = (IntPtr)0;

EnumChildWindows(this.Handle, childProc, fenster);
}




private static bool EnumWindowCross(IntPtr handle, IntPtr pointer)
{
int chars = 256;
StringBuilder cl_name = new StringBuilder(256);
GetClassName(handle, cl_name, chars);
if (cl_name.ToString() == "VideoRenderer")
SetClassLong(handle, GCL_HCURSOR, LoadCursor(IntPtr.Zero, IDC_CROSS));

return true;
}

private static bool EnumWindowArrow(IntPtr handle, IntPtr pointer)
{
int chars = 256;
StringBuilder cl_name = new StringBuilder(256);
GetClassName(handle, cl_name, chars);
if (cl_name.ToString() == "VideoRenderer")
SetClassLong(handle, GCL_HCURSOR, LoadCursor(IntPtr.Zero, IDC_ARROW));

return true;
}

Christian Linkenheld

Stefan Geissler
November 16, 2007, 16:53:17
Christian

Danke, Thank you for posting.