Results 1 to 10 of 10

Thread: [RESOLVED] Adjustable cursor.

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Resolved [RESOLVED] Adjustable cursor.

    Hi,

    I'm trying to create an adjustable size cursor.

    I figured I'd copy a .png file, (embedded in Resources) to a PictureBox, then maybe copy the (Zoom) image to a cursor.
    I had hoped that I could then re-size the PictureBox, then copy the new size image to the cursor.

    This works:
    Code:
    Imports System.IO
    
    Dim Cur As Bitmap = (DirectCast(My.Resources.ResourceManager.GetObject("Ring80BW"), Bitmap))
    vb.net Code:
    1. Private Sub MakeCursor()
    2.         Dim ptrCur As IntPtr = Cur.GetHicon
    3.  
    4.         Csr = New Cursor(ptrCur)
    5.         Me.Cursor = Csr
    6.     End Sub
    But with PictureBox1.Image = Ring80BW, this does not:
    Code:
    Dim Cur As Bitmap = PictureBox1.Image
    I'm stuck because the 'Image' in the box can't be converted to a 'Bitmap' despite 'em both being bitmaps!
    Sadly I can't find a 'C' (convert) code to do this.

    Is there a way around this?


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,755

    Re: Adjustable cursor.

    The Bitmap has a constructor argument that accepts an Image: https://docs.microsoft.com/en-us/dot...Drawing_Image_

    So you are able to do:
    Code:
    Dim Cur As Bitmap = New Bitmap(PictureBox1.Image)
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Re: Adjustable cursor.

    Quote Originally Posted by dday9 View Post
    The Bitmap has a constructor argument that accepts an Image: https://docs.microsoft.com/en-us/dot...Drawing_Image_

    So you are able to do:
    Code:
    Dim Cur As Bitmap = New Bitmap(PictureBox1.Image)
    Thanks Dday9,

    Dim Cur As Bitmap = New Bitmap(PictureBox1.Image) gives no problem with Intellisence. Come to run it however:
    System.NullReferenceException
    HResult=0x80004003
    Message=Object reference not set to an instance of an object.
    Source=Cursor Size Trial
    StackTrace:
    at Cursor_Size_Trial.Form1..ctor() in D:\~VB Trials\Cursor Size Trial\Form1.vb:line 11
    Took a look at your URL, and from there I tried:
    Code:
    Dim Cur As New Bitmap(GetType(PictureBox), "PictureBox.bmp")
    That ran successfully but from a 100px x 100px Picturebox (80px x 80px .png image) I get a 32px x 32px cursor so changing the size of the PictureBox isn't going to achieve my goal.
    I may have to resort to ignoring the PictureBox and just having a series of different sized images, which could hardly be considered adjustable.

    Poppa
    Along with the sunshine there has to be a little rain sometime.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,755

    Re: Adjustable cursor.

    Object reference not set to an instance of an object suggests that the Image was not set when you went to get the property. To confirm, add this:
    Code:
    If (PictureBox1.Image Is Nothing) Then
        MessageBox.Show("Image not set.")
    Else
        Dim Curr = New Bitmap(PictureBox1.Image)
    End If
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Re: Adjustable cursor.

    Thanks Dday9,

    I tried that and had no MessageBox.
    So I re-coded to the code below. This works fine up to a point, the cursor is 80px square and Clicking the 'Test' button, PictureBox1 changes from 100px Sqr. to 180px Sqr. but of course the cursor stays the same.
    I obviously need to discover how to copy P'Box1 image to a bitmap, this may take a while.


    Poppa

    vb.net Code:
    1. Imports System.IO
    2.  
    3.     Public Class Form1
    4.  
    5.     Dim Csr As Cursor
    6.     Dim Cur As Bitmap
    7.     Private Sub Form1_Load() Handles MyBase.Load
    8.  
    9.         MakeCursor()
    10.     End Sub
    11.  
    12.     Private Sub ExitButton_Click() Handles ExitButton.Click
    13.         Me.Close()
    14.     End Sub
    15.  
    16.     Private Sub Test_Click() Handles Test.Click
    17.         Dim siz As Integer = Cur.Height * 2
    18.  
    19.         PictureBox1.Height = siz
    20.         PictureBox1.Width = siz
    21.         Me.Cursor.Dispose()
    22.         MakeCursor()
    23.     End Sub
    24.  
    25.     Private Sub MakeCursor()
    26.         Cur = New Bitmap(PictureBox1.Image)
    27.         Dim ptrCur As IntPtr = Cur.GetHicon
    28.  
    29.         Csr = New Cursor(ptrCur)
    30.         Me.Cursor = Csr
    31.     End Sub
    32.  
    33. End Class
    Along with the sunshine there has to be a little rain sometime.

  6. #6
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Adjustable cursor.

    Quote Originally Posted by Poppa Mintin View Post
    I obviously need to discover how to copy P'Box1 image to a bitmap, this may take a while.
    PictureBox1.DrawToBitmap

    BB

  7. #7

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Re: Adjustable cursor.

    Thanks BB, I can't get the syntax for that command.

    Meanwhile I've been trying the following:
    vb.net Code:
    1. Private Sub CaptureImage(ctrl As Control, fileName As String)
    2.         Me.TopMost = True
    3.         Dim posn As Rectangle = ctrl.Bounds
    4.         Dim pt As Point = ctrl.PointToScreen(posn.Location)
    5.         Dim map As New Bitmap(posn.Width, posn.Height)
    6.         Using gr As Graphics = Graphics.FromImage(map)
    7.             gr.CopyFromScreen(New Point(pt.X - ctrl.Location.X, pt.Y - ctrl.Location.Y), Point.Empty, posn.Size)
    8.             'gr.CopyFromScreen(New Point(ctrl.Location.X, ctrl.Location.Y), Point.Empty, posn.Size)
    9.             'gr.CopyFromScreen(New Point(pt.X, pt.Y), Point.Empty, posn.Size)
    10.         End Using
    11.         Me.TopMost = False
    12.         map.Save("C:\Temp\ImageTrial.png")
    13.     End Sub
    Where this subroutine is called with: ctrl = PictureBox1, and Filename = "ImageTrial".
    Prior to the addition of the 'TopMost' command, the images produced contained parts of the Form1.vb.
    Since then you can see the three 'CopyFromScreen' options I've tried, all of 'em produce an image file in the selected folder, but none of 'em contain any image.
    Stepping through the program, I can see that all the locations, and sizes collected are exactly the same as 'Watch' gives independently for P'Box1.
    Very disappointing.


    Poppa
    Last edited by Poppa Mintin; Mar 15th, 2021 at 08:21 PM. Reason: Typo
    Along with the sunshine there has to be a little rain sometime.

  8. #8
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Adjustable cursor.

    Quote Originally Posted by Poppa Mintin View Post
    Thanks BB, I can't get the syntax for that command.
    Code:
           Dim bmp As Bitmap
           With PictureBox1
                .BorderStyle = BorderStyle.None 'if necessary
                bmp = New Bitmap(.Width, .Height)
                .DrawToBitmap(bmp, .ClientRectangle)
                .BorderStyle = BorderStyle.Fixed3D 'or whatever, if wanted
           End With
    Then have your lurid way with bmp. Don't forget to dispose of it.

    BB

  9. #9

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Re: Adjustable cursor.

    Thanks BB,

    That's brilliant, I'd struggled with that until 2AM but still got nowhere.

    All that work and I'm still no better off, yes I can copy the image, yes I can make the cursor with the bitmap...
    Sadly of course the bitmap isn't a png so there's no transparency. Obviously I'm also going to have to change the multiplication factor considerably, and have an up and down option. Currently, four clicks and the cursor is as big as Form1 height !

    I'm off to play with TransparencyKey now... See if I can get it to only affect the cursor. Ha !

    Thanks again BB.

    vb.net Code:
    1. Imports System.IO
    2.  
    3.     Public Class Form1
    4.  
    5.     Dim Csr As Cursor
    6.     Dim Cur As Bitmap
    7.     Dim map As Bitmap
    8.  
    9.     Private Sub Form1_Load() Handles MyBase.Load
    10.         MakeCursor()
    11.     End Sub
    12.  
    13.     Private Sub Copy_Click() Handles Copy.Click
    14.         CaptureImage(PictureBox1)
    15.     End Sub
    16.  
    17.     Private Sub ExitButton_Click() Handles ExitButton.Click
    18.         Me.Close()
    19.     End Sub
    20.  
    21.     Private Sub Test_Click() Handles Test.Click
    22.         Dim siz As Integer = Cur.Width * 2 ' The cursor is square.
    23.  
    24.         PictureBox1.Height = siz
    25.         PictureBox1.Width = siz
    26.         Me.Cursor.Dispose()
    27.         MakeCursor()
    28.     End Sub
    29.  
    30.     Private Sub MakeCursor()
    31.         CaptureImage(PictureBox1)
    32.         Cur = New Bitmap(map)
    33.         Dim ptrCur As IntPtr = Cur.GetHicon
    34.         Csr = New Cursor(ptrCur)
    35.         Me.Cursor = Csr
    36.         map.Dispose()
    37.     End Sub
    38.  
    39.     Private Sub CaptureImage(ctrl As Control)
    40.         With ctrl
    41.             map = New Bitmap(.Width, .Height)
    42.             .DrawToBitmap(map, .ClientRectangle)
    43.         End With
    44.  
    45.         '   Temp.
    46.         map.Save("C:\Temp\ImageTrial.png")
    47.     End Sub
    48.  
    49. End Class

    Poppa

    PS

    Since this thread is now solved I'll mark it resolved.
    If I run into problems with transparency, I'll start a new thread.

    Pop
    Last edited by Poppa Mintin; Mar 16th, 2021 at 07:17 AM. Reason: PS Added
    Along with the sunshine there has to be a little rain sometime.

  10. #10

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Re: [RESOLVED] Adjustable cursor.

    Just to add the final result in case someone is looking to do the same thing...

    vb.net Code:
    1. '   Trial to change cursor size.    Started: 15:03:2021.
    2. '   Trial completed.                         17:03:2021.
    3.  
    4. Imports System.Drawing.Imaging
    5. Imports System.IO
    6.  
    7. Public Class Form1
    8.  
    9.     Dim Csr As Cursor
    10.     Dim Cur As Bitmap
    11.     Dim map As Image
    12.     ReadOnly img As Image = New Bitmap(My.Resources.Ring80BW)
    13.  
    14.     Private Sub Form1_Load() Handles MyBase.Load
    15.         Reset()
    16.     End Sub
    17.  
    18.     '       Subroutines Alphabetical.
    19.  
    20.     Private Sub DownClick() Handles TestDown.Click
    21.         ResizeCursor(CInt(map.Height * 0.9))
    22.     End Sub
    23.  
    24.     Private Sub ExitButton_Click() Handles ExitButton.Click
    25.         Me.Close()
    26.     End Sub
    27.  
    28.     Private Sub MakeCursor()
    29.         Me.Cursor.Dispose()
    30.         Cur = New Bitmap(map)
    31.         Dim ptrCur As IntPtr = Cur.GetHicon
    32.         Csr = New Cursor(ptrCur)
    33.         Me.Cursor = Csr
    34.     End Sub
    35.  
    36.     Private Sub Reset() Handles Standard.Click
    37.         map = img
    38.         MakeCursor()
    39.     End Sub
    40.  
    41.     Public Sub ResizeCursor(ByVal siz As Integer)
    42.         map = New Bitmap(img, siz, siz)
    43.         Me.Cursor.Dispose()
    44.         MakeCursor()
    45.     End Sub
    46.  
    47.     Private Sub UpClick() Handles TestUP.Click
    48.         ResizeCursor(CInt(map.Height * 1.1))
    49.     End Sub
    50.  
    51. End Class
    Last edited by Poppa Mintin; Mar 17th, 2021 at 12:07 PM. Reason: Revised code.
    Along with the sunshine there has to be a little rain sometime.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width