Results 1 to 7 of 7

Thread: [RESOLVED] Set a picturebox image as the form's icon...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2003
    Location
    Grenada
    Posts
    346

    Resolved [RESOLVED] Set a picturebox image as the form's icon...

    Basically as the title suggests, this is what I need to do.

    The scenario is this:
    I have a picturebox 16*16, and it has a randomly drawn pattern on it. I would like for when the user to click a button (or whatever other automated process there is) the form's icon will change to that of the picturebox...

    I HAVE search the site for the past 2 hours looking for a way to do this, and of all the results I can understand, they are similar to what I got from MSDN. Which is to use an Imagelist control.
    The thing is that I want this program to be free of additional ActiveX's and DLL references. In other words, fully independent and basic.

    So can anyone provide / assist me in a solution for setting a form's icon from a picturebox image?

    P.S. I would prefer it not to have to save the image to disk... UNLESS it's the ONLY way...

    Thanx.
    If my post has been helpful, then please rate it accordingly...
    If it has solved your question(s), then don't forget to mark the thread as "[Resolved]"... thank you.

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    11,044

    Re: Set a picturebox image as the form's icon...

    Wouldn't the ff. work?

    VB Code:
    1. Set Form1.Icon = Picture1.Picture
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Set a picturebox image as the form's icon...

    is you picture box image can be converted/saved as .ico file...then you wont be having any problem to assign it as your form's icon...
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2003
    Location
    Grenada
    Posts
    346

    Re: Set a picturebox image as the form's icon...

    Quote Originally Posted by dee-u
    Wouldn't the ff. work?

    VB Code:
    1. Set Form1.Icon = Picture1.Picture

    That was one of the first things I tried, and unfortunately it didn't work. But I tried it again to make sure, and I got the same result.

    When I create a runtime random image in the picturebox, I get no errors... but the icon doesn't change either.

    And if I load a 16*16 bitmap image file into the picture box, I get a Runtime #380 error: Invalid Property value.


    Maybe this might help out the situation more. The reason I am trying to add the picture box image to the form is so that I can somehow get an Icon Handle (or hIcon for short), and PictureBoxes cannot give you that functionality.

    ====EDIT====
    To ganeshmoorthy

    If you could provide the source code to save it as an icon, then that would help. Because I don't think that you can save a picturebox image as an icon using the SavePicture method... (unless I'm mistaken)
    If my post has been helpful, then please rate it accordingly...
    If it has solved your question(s), then don't forget to mark the thread as "[Resolved]"... thank you.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2003
    Location
    Grenada
    Posts
    346

    Re: Set a picturebox image as the form's icon...

    Quote Originally Posted by ganeshmoorthy
    is you picture box image can be converted/saved as .ico file...then you wont be having any problem to assign it as your form's icon...
    I took your suggestion and expanded on it... I succesfully created a function to set a form's Icon based on a 16*16 image in a picturebox without writing to disk... BUT the problem is that I cannot assign that icon to other forms... eg. Form2.icon = Form1.icon doesn't do anything...

    The only solution I had was saving the icon file as you said, and load it to each form (which I wanted to avoid in the first place), but no matter, it works in the long run...

    Below is the function I came up with to do this:

    VB Code:
    1. Private Const PICTYPE_ICON As Long = 3
    2.  
    3. Private Type IconInfo
    4.     fIcon               As Long
    5.     xHotspot            As Long
    6.     yHotspot            As Long
    7.     hBMMask             As Long
    8.     hBMColor            As Long
    9. End Type
    10.  
    11. Private Type Guid
    12.     Data1               As Long
    13.     Data2               As Integer
    14.     Data3               As Integer
    15.     Data4(7)            As Byte
    16. End Type
    17.  
    18. Private Type PictureInfo
    19.     cbSizeofStruct      As Long
    20.     picType             As Long
    21.     hImage              As Long
    22.     xExt                As Long
    23.     yExt                As Long
    24. End Type
    25.  
    26. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
    27. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    28. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
    29. Private Declare Function CreateIconIndirect Lib "user32" (icoinfo As IconInfo) As Long
    30. Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
    31. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    32. Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long
    33. Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (lpPictureInfo As PictureInfo, riid As Guid, ByVal fown As Long, ipic As IPicture) As Long
    34. Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
    35.  
    36. Public objPicture              As IPicture
    37.  
    38. Public Function ConvertToIcon(hDCSource As Long, hDCMask As Long) As IPicture
    39.     On Error Resume Next
    40.     Dim udtGuid                 As Guid
    41.     Dim udtIconInfo             As IconInfo
    42.     Dim lngNewBitmap            As Long
    43.     Dim lngNewBitmapDC          As Long
    44.     Dim lngNewMask              As Long
    45.     Dim lngNewMaskDC            As Long
    46.     Dim udtPictureInfo          As PictureInfo
    47.     Dim lngPreviousBitmap       As Long
    48.     Dim lngPreviousMask         As Long
    49.     lngNewMaskDC = CreateCompatibleDC(hDC)
    50.     lngNewMask = CreateCompatibleBitmap(lngNewMaskDC, 16, 16)
    51.     lngPreviousMask = SelectObject(lngNewMaskDC, lngNewMask)
    52.     BitBlt lngNewMaskDC, 0, 0, 16, 16, hDCMask, 0, 0, vbSrcCopy
    53.     SelectObject lngNewMaskDC, lngPreviousMask
    54.     lngNewBitmapDC = CreateCompatibleDC(0)
    55.     lngNewBitmap = CreateCompatibleBitmap(hDCSource, 16, 16)
    56.     lngPreviousBitmap = SelectObject(lngNewBitmapDC, lngNewBitmap)
    57.     BitBlt lngNewBitmapDC, 0, 0, 16, 16, hDCSource, 0, 0, vbSrcCopy
    58.     SelectObject lngNewBitmapDC, lngPreviousBitmap
    59.     With udtGuid
    60.          .Data1 = &H20400
    61.          .Data4(0) = &HC0
    62.          .Data4(7) = &H46
    63.     End With
    64.     With udtIconInfo
    65.         .fIcon = 1
    66.         .hBMColor = lngNewBitmap
    67.         .hBMMask = lngNewMask
    68.         .xHotspot = 8
    69.         .yHotspot = 8
    70.     End With
    71.     With udtPictureInfo
    72.         .cbSizeofStruct = Len(udtPictureInfo)
    73.         .picType = PICTYPE_ICON
    74.         .hImage = CreateIconIndirect(udtIconInfo)
    75.     End With
    76.     OleCreatePictureIndirect udtPictureInfo, udtGuid, 1, objPicture
    77.     Set ConvertToIcon = objPicture
    78.     DestroyIcon udtPictureInfo.hImage
    79.     DeleteObject lngNewBitmap
    80.     DeleteObject lngNewMask
    81.     DeleteDC lngNewBitmapDC
    82.     DeleteDC lngNewMaskDC
    83.     DeleteObject lngPreviousBitmap
    84.     DeleteObject lngPreviousMask
    85.     Set objPicture = Nothing
    86. End Function

    And you just have 2 pictureboxes on your form (one for the Icon, and the other for the masked / transparent pattern). Then just either call the function Form1.icon = ConvertToIcon(PictBitmap.hDC, PictMask.hDC) to change the form's icon, or SavePicture(ConvertToIcon(PictBitmap.hDC, PictMask.hDC), "C:\Converted.ico")

    And it works...

    Thanks anyway ganeshmoorthy and dee-u for your time and effort
    If my post has been helpful, then please rate it accordingly...
    If it has solved your question(s), then don't forget to mark the thread as "[Resolved]"... thank you.

  6. #6
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,728

    Re: [RESOLVED] Set a picturebox image as the form's icon...

    Much easier way to Convert2Icon.
    Add an imagelist and use it's ExtractIcon method.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2003
    Location
    Grenada
    Posts
    346

    Re: [RESOLVED] Set a picturebox image as the form's icon...

    I know about that method, but I wanted to make my entire application based on modules, class modules and usercontrols... no outside ActiveX object / added references...

    If I were to use that method, it would require me adding the MSWinCom controls... and that would defeat my aim. To make the my application utilizing the least memory and NO outside conponents...

    So far I'm on that track....
    If my post has been helpful, then please rate it accordingly...
    If it has solved your question(s), then don't forget to mark the thread as "[Resolved]"... thank you.

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