Tip: MenuWidth() procedure

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Tip: MenuWidth() procedure

Post by BackupUser »

Code updated For 5.20+

Restored from previous forum. Originally posted by PB.

PureBasic has MenuHeight(), but no MenuWidth(), so here's my own! :)

Code: Select all

    ; MenuWidth() procedure by PB -- do whatever you want with it. :)
    ; This example demonstrates a window having the width of its menu.
    ; (It's not exactly the menu width, due to Windows OS limitations).
    ;
    Procedure MenuWidth()
      MyMenu=GetMenu_(WindowID(0)) : ItemSize.RECT
      For ItemNum=0 To GetMenuItemCount_(MyMenu)-1
        GetMenuItemRect_(WindowID(0),MyMenu,ItemNum,ItemSize)
        width+(ItemSize\right-ItemSize\left)
      Next
      ProcedureReturn width+GetSystemMetrics_(#SM_CYHSCROLL)
    EndProcedure
    ;
    If OpenWindow(0,100,200,0,GetSystemMetrics_(#SM_CYCAPTION),"test",#WS_VISIBLE|#PB_Window_SystemMenu) And CreateMenu(0,WindowID(0))
      MenuItem(0,"#1") : MenuItem(0,"No. Two") : MenuItem(0,"Number Three") : MenuItem(0,"IV")
      ResizeWindow(0,100,200,MenuWidth(),MenuHeight()) : ShowWindow_(WindowID(0),#SW_SHOW)
      Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
    EndIf