Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <Window.Resources>
- <ResourceDictionary>
- <TextBlock x:Key="CursorGrab" Cursor="Resources/Cursors/grab.cur"/>
- <TextBlock x:Key="CursorMagnify" Cursor="Resources/Cursors/magnify.cur"/>
- </ResourceDictionary>
- </Window.Resources>
- if (selectedTool == "Hand")
- myCanvas.Cursor = ((TextBlock)this.Resources["CursorGrab"]).Cursor;
- else if (selectedTool == "Magnify")
- myCanvas.Cursor = ((TextBlock)this.Resources["CursorMagnify"]).Cursor;
- else
- myCanvas.Cursor = Cursor.Arrow;
- public Cursor ConvertToCursor(Visual visual, Point hotSpot)
- {
- int width = (int)visual.Width;
- int height = (int)visual.Height;
- // Render to a bitmap
- var bitmapSource = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
- resultSource.Render(visual);
- // Convert to System.Drawing.Bitmap
- var pixels = new int[width*height];
- bitmapSource.CopyPixels(pixels, width, 0);
- var bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppPargb);
- for(int y=0; y<height; y++)
- for(int x=0; x<width; x++)
- bitmap.SetPixel(x, y, Color.FromArgb(pixels[y*width+x]));
- // Save to .ico format
- var stream = new MemoryStream();
- new System.Drawing.Icon(resultBitmap.GetHIcon()).Save(stream);
- // Convert saved file into .cur format
- stream.Seek(2);
- stream.Write(2);
- stream.Seek(10);
- stream.Write((byte)(int)(hotSpot.X * width);
- stream.Write((byte)(int)(hotSpot.Y * height);
- stream.Seek(0);
- // Construct Cursor
- return new Cursor(stream);
- }
- <Grid x:Name="customCursor" Width="32" Height="32">
- ...
- </Grid>
- someControl.Cursor = ConvertToCursor(customCursor, new Point(0.5, 0.5));
- <Window Cursor=""C:WINDOWSCursorsdinosaur.ani"" />
Add Comment
Please, Sign In to add comment