Example #1
0
        public static void ToggleMouseCursorVisibility(MainWindow frmMain, Tools.Boolstate forced = Tools.Boolstate.Indeterminate)
        {
            if (((forced == Tools.Boolstate.True) && (!MouseCursorIsHidden)) || ((forced == Tools.Boolstate.False) && MouseCursorIsHidden))
            {
                return;
            }

            if ((forced == Tools.Boolstate.True) || MouseCursorIsHidden)
            {
                Native.SetSystemCursor(hCursorOriginal, OCR_SYSTEM_CURSORS.OCR_NORMAL);
                Native.DestroyIcon(hCursorOriginal);
                hCursorOriginal = IntPtr.Zero;

                MouseCursorIsHidden = false;
            }
            else
            {
                string fileName = null;

                try
                {
                    hCursorOriginal = frmMain.Cursor.CopyHandle();

                    if (curInvisibleCursor == null)
                    {
                        // Can't load from a memory stream because the constructor new Cursor() does not accept animated or non-monochrome cursors
                        fileName = Path.GetTempPath() + Guid.NewGuid().ToString() + ".cur";

                        using (FileStream fileStream = File.Open(fileName, FileMode.Create))
                        {
                            using (MemoryStream ms = new MemoryStream(Properties.Resources.blank))
                            {
                                ms.WriteTo(fileStream);
                            }

                            fileStream.Flush();
                            fileStream.Close();
                        }

                        curInvisibleCursor = new Cursor(Native.LoadCursorFromFile(fileName));
                    }

                    Native.SetSystemCursor(curInvisibleCursor.CopyHandle(), OCR_SYSTEM_CURSORS.OCR_NORMAL);

                    MouseCursorIsHidden = true;
                }
                catch
                {
                    // swallow exception and assume cursor set failed
                }
                finally
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(fileName))
                        {
                            if (File.Exists(fileName))
                            {
                                File.Delete(fileName);
                            }
                        }
                    }
                    catch { }
                }
            }
        }