AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

assign bitmap to any item in any AHk menu
Goto page Previous  1, 2
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
AGermanUser



Joined: 12 Feb 2005
Posts: 81

PostPosted: Tue Jan 24, 2006 10:04 pm    Post subject: Reply with quote

One short question. Wink

Quote:
Menu_AssignBitmap( p_menu, p_item, p_bm_unchecked, p_unchecked_face, p_bm_checked=false, p_checked_face=false )

Did you forget to initialize "p_unchecked_face" with false or was it meant the way you wrote it above? I thought about:
Quote:
Menu_AssignBitmap( p_menu, p_item, p_bm_unchecked, p_unchecked_face=false, p_bm_checked=false, p_checked_face=false )

This would make assigning bitmaps easier when you just don't want to use a 'checked' bitmap or this LR_LOADTRANSPARENT feature. Assigning a picture would then just mean:
Code:
Menu_AssignBitmap("Tray", "1", item1.bmp)

Otherwise AHK keeps always saying you didn't pass enough parameters.

BTW. commented your script again. Don't know if it's of any use, but it makes copying from forum to your AHK scripts easier. Smile

Code:
Menu_AssignBitmap(p_menu, p_item, p_bm_unchecked, p_unchecked_face=false, p_bm_checked=false, p_checked_face=false)
  {
    ; Thanks to shimanov for this function
    ; Details under http://www.autohotkey.com/forum/viewtopic.php?p=44577

    ; p_menu            = "MenuName" (e.g., Tray, etc.)
    ; p_item            = "MenuItemNumber" (e.g. 1, ...)
    ; p_bm_unchecked,
    ; p_bm_checked      = path to bitmap for unchecked 'n' checked menu entry/false
    ; p_unchecked_face,
    ; p_checked_face    = true/false (i.e., true = pixels with same color as
    ;                                 first pixel are transparent)

   
    static   menu_list, h_menuDummy
   
    If h_menuDummy=
    {
      menu_list = |
   
      ; Save current 'DetectHiddenWindows' mode to reset it later
      Old_DetectHiddenWindows := A_DetectHiddenWindows
      DetectHiddenWindows, on
     
      ; Retrieve scripts PID
      Process, Exist
      pid_this := ErrorLevel
     
      ; Create menuDummy and assign to Gui99
      Menu, menuDummy, Add
      Menu, menuDummy, DeleteAll
     
      Gui, 99:Menu, menuDummy
     
      ; Retrieve menu handle (menuDummy)
      h_menuDummy := DllCall( "GetMenu", "uint", WinExist( "ahk_class AutoHotkeyGUI ahk_pid " pid_this ) )
   
      ; Remove menu bar 'menuDummy'
      Gui, 99:Menu
     
      ; Reset 'DetectHiddenWindows' mode to old setting
      DetectHiddenWindows, %Old_DetectHiddenWindows%
    }
   
    ; Assign p_menu to menuDummy and retrieve menu handle
    If (! InStr(menu_list, "|" p_menu ",", false))
      {
        Menu, menuDummy, Add, :%p_menu%   
        menu_ix := DllCall( "GetMenuItemCount", "uint", h_menuDummy ) - 1
        menu_list = %menu_list%%p_menu%,%menu_ix%|
      }
    Else
      {
        menu_ix := InStr(menu_list, ",", false, InStr( menu_list, "|" p_menu ",", false)) + 1
        StringMid, menu_ix, menu_list, menu_ix, InStr(menu_list, "|", false, menu_ix) - menu_ix
      }
   
    h_menu := DllCall("GetSubMenu", "uint", h_menuDummy, "int", menu_ix)
   
    ; Load bitmap for unchecked menu entries
    If (p_bm_unchecked)
      {
        hbm_unchecked := DllCall( "LoadImage"
                                , "uint", 0
                                , "str", p_bm_unchecked
                                , "uint", 0                             ; IMAGE_BITMAP
                                , "int", 0
                                , "int", 0
                                , "uint", 0x10|(0x20*p_unchecked_face)) ; LR_LOADFROMFILE|LR_LOADTRANSPARENT
       
        If (ErrorLevel or ! hbm_unchecked)
          {
             MsgBox, [Menu_AssignBitmap: LoadImage: unchecked] failed: EL = %ErrorLevel%
             Return, false
          }
      }
   
    ; Load bitmap for checked menu entries
    If (p_bm_checked)
      {
        hbm_checked := DllCall( "LoadImage"
                              , "uint", 0
                              , "str", p_bm_checked
                              , "uint", 0                               ; IMAGE_BITMAP
                              , "int", 0
                              , "int", 0
                              , "uint", 0x10|(0x20*p_checked_face))     ; LR_LOADFROMFILE|LR_LOADTRANSPARENT
     
        If (ErrorLevel or ! hbm_checked)
          {
             MsgBox, [Menu_AssignBitmap: LoadImage: checked] failed: EL = %ErrorLevel%
             Return, false
          }
      }
   
    ; On success assign image to menu entry
    success := DllCall( "SetMenuItemBitmaps"
                      , "uint", h_menu
                      , "uint", p_item-1
                      , "uint", 0x400                                   ; MF_BYPOSITION
                      , "uint", hbm_unchecked
                      , "uint", hbm_checked )
                     
    If (ErrorLevel or ! success)
      {
        MsgBox, [Menu_AssignBitmap: SetMenuItemBitmaps] failed: EL = %ErrorLevel%
        Return, false
      }
   
    Return, true
  }

_________________
Cheers
BBCodeWriterToDo-ListCopyPassage
Back to top
View user's profile Send private message
shimanov



Joined: 25 Sep 2005
Posts: 612

PostPosted: Tue Jan 24, 2006 10:22 pm    Post subject: Reply with quote

AGermanUser wrote:
Did you forget to initialize "p_unchecked_face" with false or was it meant the way you wrote it above?


I decided to require explicit specification of one set of bitmap parameters, so that users would not forget they exist -- I could go either way on this one.

Quote:
commented your script again


Thanks. It is useful, and helpful, to those who desire to understand the internals of the code; however, it is unnecessary for those who simply want to use the function.

I intend to leave the code as posted...

to mitigate the ire of those who would count lines.
Back to top
View user's profile Send private message
sosaited



Joined: 24 Feb 2005
Posts: 233

PostPosted: Fri Mar 17, 2006 7:05 pm    Post subject: Reply with quote

Very nice function. Thanks for sharing it. I really am in need of Assigning bitmaps to my MIGHTY BIG ALL IN ONE MENU.. Laughing Now here are my questions and problems:

First of all, does this function really assign an image as a context menu image or it just replaces the "check mark"(Tick) to the bitmap (I am guessing this because of the true and false parameters of the function.) And if it does... is this possible to PROPERLY assign a bitmap/ico to a menu?.

and second.. i used it with my script:
Code:
....................
Menu, movies_control, Add, Smallville, :smallville
............
Menu, Main, Add, Movies-Tv Serials Control, :movies_control
Menu_AssignBitmap( "movies_control", 1, "smallville_sec.bmp", false )
..........
your function goes here

When i run it for the first time, it works perfect:



but when i run it again without closing the script it says:


and the menu is shown but the img does not appear.

and the last problem is that when i try to add an image to a main menu(can be seen in the image as Folder Control, Program Control etc) it shows the same error.. here's the code:
Code:
.............
Menu, Main, Add
Menu, Main, Add, -=EXIT=-, Exit
..........
Menu_AssignBitmap( "Main", 6, "movies.bmp", false )
.........


so can someone please answer my questions..
Thanks again
_________________
My small "thanks" to AHK in shape of these dedicated 3d images (Topic already in "General" Forum)
Back to top
View user's profile Send private message Send e-mail MSN Messenger
AGU
Guest





PostPosted: Fri Mar 17, 2006 7:58 pm    Post subject: Reply with quote

Don't know if I can help you, but I used this function in my AHK CopyPassage script within the context menu. And it works like a charm.

Concerning your problem I guess it may be because of a changed menu handler or PID of your GUI. But this is a very rough guess. I may be completely wrong.
Maybe you have to post your complete code/script in order to help you.

btw. I predefine the third parameter in the function definition as false so that I mustn't enter a third parameter all the time.
_________________
Cheers
AGU a.k.a AGermanUser
Back to top
sosaited



Joined: 24 Feb 2005
Posts: 233

PostPosted: Sat Mar 18, 2006 6:50 am    Post subject: Reply with quote

AGU wrote:
I guess it may be because of a changed menu handler or PID of your GUI


Sorry i forgot to tell .. in my script i dont have a GUI... just a MENU... maybe thats the reason?

AGU wrote:
but I used this function in my AHK CopyPassage script within the context menu. And it works like a charm.


Can you please send the only part of ur script which adds the bitmaps to menu's (Along the line which adds that specific menu)... and have u tried to add a bitmap to a Menu that has got more Submenu's? (Take a look at my posted image)
_________________
My small "thanks" to AHK in shape of these dedicated 3d images (Topic already in "General" Forum)
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Guest






PostPosted: Sat Mar 18, 2006 1:10 pm    Post subject: Reply with quote

Quote:
Can you please send the only part of ur script which adds the bitmaps to menu's (Along the line which adds that specific menu).


Definition des Kontextmenüs
Code:
; Create custom context menu
Menu, Context, Add, Open URL, CallUrl
Menu, Context, Add
Menu, Context, Add, Edit Note, EditNote
Menu, Context, Add, Delete Note, DeleteNote
Menu, Context, NoStandard

Menu_AssignBitmap("Context", "1", url_bmp)
Menu_AssignBitmap("Context", "3", edit_bmp)
Menu_AssignBitmap("Context", "4", delete_bmp)


Mit diesem Label wird das Menü aufgerufen
Code:
GuiContextMenu:
  RowSelected := LV_GetCount("S")
 
  If (A_GuiEvent = "RightClick" AND RowSelected)
    {
      Menu, Context, Show
    }
Return


Die Funktion "Menu_AssignBitmap" die ich verwende findest du hier:
http://file.autohotkey.net/AGermanUser/CopyPassage/functions.ahk
Quote:

.. and have u tried to add a bitmap to a Menu that has got more Submenu's? (Take a look at my posted image)
Nope, sorry. Haven't tried that yet.

Cheers
AGU
Back to top
shimanov



Joined: 25 Sep 2005
Posts: 612

PostPosted: Mon Mar 20, 2006 8:26 am    Post subject: Reply with quote

sosaited wrote:
Very nice function.


Thanks.

Quote:
does this function really assign an image as a context menu image


Yes.

Quote:
replaces the "check mark"(Tick) to the bitmap (I am guessing this because of the true and false parameters of the function.)


Each menu item has an associated "checked" state. Hence:

shimanov wrote:
p_bm_unchecked,
p_bm_checked = path to bitmap/false
p_unchecked_face,
p_checked_face = true/false (i.e., true = pixels with same color as first pixel are transparent)


Note:

MSDN wrote:
If both parameters are NULL, the system displays the default check-mark bitmap when the item is selected, and removes the bitmap when the item is not selected.


Quote:
When i run it for the first time, it works perfect
...
but when i run it again without closing the script it says [error]


Do you use "Menu, <Delete|DeleteAll>" in your script? If so, then the problem is that the function caches the menu handles associated with previously defined menus, and does not account for deleted menus.

Quote:
try to add an image
...
Menu_AssignBitmap( "Main", 6, "movies.bmp", false )


Item #6 refers to a separator line. Try:

Code:
Menu_AssignBitmap( "Main", 11, "movies.bmp", false )
Back to top
View user's profile Send private message
sosaited



Joined: 24 Feb 2005
Posts: 233

PostPosted: Wed Mar 22, 2006 9:04 pm    Post subject: Reply with quote

shimanov wrote:
Do you use "Menu, <Delete|DeleteAll>" in your script

Yes I do.
shimanov wrote:
If so, then the problem is that the function caches the menu handles associated with previously defined menus, and does not account for deleted menus.

I edited your function a little bit so just before it finishes, it DELETE's all Variable's.. Here's what i got:
If i add this at the end of your function: (i.e Delet all the variables)
Code:
   hbm_checked =
   success =
   hbm_unchecked =
   p_bm_unchecked =
   h_menu =
   p_item =
   p_bm_checked =
   h_menuDummy =
   menu_ix =
   menu_list =
   p_menu =
   p_unchecked_face =
   p_bm_checked =
   p_checked_face =

because the results are weird (at least for me) i am sending screenshots
The First "Run" works great:

The Second run works "weird" Smile : (The submenu "Arrow" Disappears but the Image is shown)


And then, I added another Bitmap to the MAIN menu (by applying your suggested method : Menu_AssignBitmap( "Main", 11, "movies.bmp", false ) )
First run (with bitmaps assigned to both Menu item's): (Bitmap assigned to only one ITEM, Submenu's working)



Surprisingly, Its second RUN gave error!:


but after i clicked OK, the menu was displayed like this: (ALL SUBMENU's gone.. even the one's that had nothing to do with the Menu_AssignBitmap Function)


and the cycle keeps repeating (in the case of Bitmaps assigned to both Menu Items) after second Run....

Now i am stuck.. i have no idea what is going on with this script, and my luck Laughing .. but i am sure as you are an expert shimanov and experienced in API .. and DllCall.. you can help me..

IF you need, Here are the files of my iMenu Script:
AHK Script File
Fav.ini (File required by script)
Movies.bmp
smallville_sec.bmp

Thanks
_________________
My small "thanks" to AHK in shape of these dedicated 3d images (Topic already in "General" Forum)
Back to top
View user's profile Send private message Send e-mail MSN Messenger
shimanov



Joined: 25 Sep 2005
Posts: 612

PostPosted: Thu Mar 23, 2006 12:55 am    Post subject: Reply with quote

Sorry, at present, I don't have time to characterize or analyze AHk's menu command behavior.

So, here is a quick fix:

change:
Code:
MButton::
...
Menu, open_sub, Add, %v_currentname1%, open
}

> if menu_Main_init=
> {
> menu_Main_init := true

Menu, Main, Add, %A_Space%%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%AESAM(Alpha v1.6), Info


change:
Code:
MButton::
...
Menu_AssignBitmap( "Main", "11", "movies.bmp", false )

> }

Menu, Main, Show


remove:
Code:
MButton::
...
> GetKeyState, v_mbuttonstate, MButton
> If v_mbuttonstate = U
>    Menu, Main, DeleteAll
Back to top
View user's profile Send private message
shimanov



Joined: 25 Sep 2005
Posts: 612

PostPosted: Thu Mar 23, 2006 5:18 am    Post subject: update: 2006.03.22 Reply with quote

shimanov wrote:
Sorry, at present, I don't have time to characterize or analyze AHk's menu command behavior.


Never mind. It wasn't really a bug, but it was annoying. And I can appreciate the need for dynamic menu creation.

changes:
    * function prototype: p_unchecked_face=false (Thanks AGU)
    * support dynamically created menus (Thanks sosaited)
Back to top
View user's profile Send private message
Grendel



Joined: 18 Nov 2005
Posts: 25
Location: Germany

PostPosted: Fri Mar 24, 2006 11:26 am    Post subject: Reply with quote

...and how can I use my .ICO files for menus ?
At the moment this is working with .BMP files only.
_________________
greets Grendel
Back to top
View user's profile Send private message
sosaited



Joined: 24 Feb 2005
Posts: 233

PostPosted: Fri Mar 24, 2006 10:21 pm    Post subject: Reply with quote

Thanks alot shimanov.. it works without any problems now...
_________________
My small "thanks" to AHK in shape of these dedicated 3d images (Topic already in "General" Forum)
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Guest






PostPosted: Fri May 19, 2006 10:42 pm    Post subject: Reply with quote

First off excellent work shimanov!!!

But im having a bit of trouble... I created a script that creates pictures under my startbutton that have custom menus that have applications in them. I use the attach bitmap function and it works great!...except that the pictures seem to be shifted or not fit. They are 16*16 so i would assume they would fit. Anyone know why this happens?



view the code (note this will be a script that i post as soon as i work out the bugs, so you can use it, but be warned! there are bugs)
Back to top
corrupt



Joined: 29 Dec 2004
Posts: 2397

PostPosted: Sat May 20, 2006 6:02 am    Post subject: Reply with quote

Anonymous wrote:
I use the attach bitmap function and it works great!...except that the pictures seem to be shifted or not fit. They are 16*16 so i would assume they would fit. Anyone know why this happens?

The pic looks ok to me... The alignment is likely due to the method used to add images. The images are added as custom checkmarks for menu items. The images are not automatically sized to fit AFAIK...
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group