PB12.5带图标菜单自定义对象

1.新建自定义非可视对象, uo_supermenu。

   新建main类型窗口w_popmenu.

   新建菜单 m_popmenu。


2.uo_supermenu完整脚本:

forward
global type uo_supermenu from nonvisualobject
end type
type rect from structure within uo_supermenu
end type
end forward


type rect from structure
integer bottom
integer left
integer right
integer top
end type


global type uo_supermenu from nonvisualobject
end type
global uo_supermenu uo_supermenu


type prototypes
FUNCTION ulong GetMenu(ulong hwnd) LIBRARY "user32.dll"


FUNCTION ulong GetSubMenu(ulong hMenu,ulong nPos) LIBRARY "user32.dll"


FUNCTION ulong TrackPopupMenu(ulong hMenu,ulong wFlags,ulong x,ulong y,ulong nReserved,ulong hwnd,ref Rect lprc) LIBRARY "user32.dll"
end prototypes


type variables
ulong il_popmenu_window_hwnd
end variables


forward prototypes
public subroutine of_init ()
public subroutine of_show_popmenu (long xpos, long ypos)
public subroutine of_close ()
end prototypes


public subroutine of_init ();if isvalid(w_popmenu) then
close(w_popmenu)
end if
open(w_popmenu)


il_popmenu_window_hwnd = handle(w_popmenu)
end subroutine


public subroutine of_show_popmenu (long xpos, long ypos);ulong hmenu,hsubmenu,hwnd
integer li_x,li_y


RECT l_rect
l_rect.left = 0
l_rect.top = 0
l_rect.right = 0
l_rect.bottom = 0


hmenu = getmenu(il_popmenu_window_hwnd)
hsubmenu = getsubmenu(hmenu, 0)


li_x = xpos 
li_y = ypos 




TrackPopupMenu(hsubMenu, 2, li_x, li_y, 0, il_popmenu_window_hwnd, l_rect)
end subroutine


public subroutine of_close ();if isvalid(w_popmenu) then
close(w_popmenu)
end if
end subroutine


on uo_supermenu.create
call super::create
TriggerEvent( this, "constructor" )
end on


on uo_supermenu.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on


event destructor;if isvalid(w_popmenu) then
close(w_popmenu)
end if
end event

/

w_popmenu完整脚本:

forward
global type w_popmenu from window
end type
end forward


global type w_popmenu from window
integer width = 119
integer height = 252
boolean titlebar = true
string title = "Untitled"
string menuname = "m_popmenu"
boolean controlmenu = true
boolean minbox = true
boolean maxbox = true
boolean resizable = true
long backcolor = 67108864
string icon = "AppIcon!"
boolean center = true
end type
global w_popmenu w_popmenu


type prototypes
FUNCTION ulong LoadImageW(ulong hintance, string filename,uint utype,int ix,int iy,uint fload)  LIBRARY "USER32.DLL"


FUNCTION boolean SetMenuItemBitmaps(ulong hmenu,uint upos,uint flags,ulong handle_bm1,ulong handle_bm2)  LIBRARY "USER32.DLL"


FUNCTION int GetSystemMetrics(  int nIndex ) LIBRARY "USER32.DLL"


FUNCTION int GetSubMenu(ulong hMenu,int pos) LIBRARY "USER32.DLL"


FUNCTION ulong GetMenu(ulong hWindow) LIBRARY "USER32.DLL"


end prototypes
type variables
//Win32
CONSTANT Integer IMAGE_BITMAP   = 0
CONSTANT Integer LR_LOADFROMFILE = 16
CONSTANT Integer SM_CXMENUCHECK  = 71
CONSTANT Integer SM_CYMENUCHECK = 72
CONSTANT Integer MF_BITMAP = 4
CONSTANT Integer MF_BYPOSITION = 1024


end variables


on w_popmenu.create
if this.MenuName = "m_popmenu" then this.MenuID = create m_popmenu
end on


on w_popmenu.destroy
if IsValid(MenuID) then destroy(MenuID)
end on


event open;
long ll_MainHandle
long ll_SubMenuHandle
long ll_X
long ll_Y
long ll_Bitmapcut
long ll_Bitmapcopy
long ll_Bitmappaste
long ll_Bitmapitl
long ll_bitmapcnt
long ll_bitmapunderline


this.visible = false




ll_MainHandle = GetMenu(Handle(this))




ll_SubMenuHandle = GetSubMenu(ll_MainHandle,0)




ll_x = GetSystemMetrics(SM_CXMENUCHECK) 
ll_y = GetSystemMetrics(SM_CYMENUCHECK) 




ll_Bitmapcut = LoadImageW(0,'cut.bmp',  IMAGE_BITMAP ,ll_x,ll_y,LR_LOADFROMFILE)
ll_Bitmapcopy = LoadImageW(0,'copy.bmp',  IMAGE_BITMAP ,ll_x,ll_y,LR_LOADFROMFILE)
ll_Bitmappaste = LoadImageW(0,'paste.bmp',  IMAGE_BITMAP ,ll_x,ll_y,LR_LOADFROMFILE)
ll_Bitmapitl = LoadImageW(0,'itl.bmp',  IMAGE_BITMAP ,ll_x,ll_y,LR_LOADFROMFILE)
ll_Bitmapcnt = LoadImageW(0,'big.bmp',  IMAGE_BITMAP ,ll_x,ll_y,LR_LOADFROMFILE)
ll_bitmapunderline = LoadImageW(0,'ul.bmp',  IMAGE_BITMAP ,ll_x,ll_y,LR_LOADFROMFILE)


SetMenuItemBitmaps(ll_SubMenuHandle,0,MF_BYPOSITION,ll_Bitmapcut,ll_Bitmapcut)
SetMenuItemBitmaps(ll_SubMenuHandle,1,MF_BYPOSITION,ll_Bitmapcopy,ll_Bitmapcopy)
SetMenuItemBitmaps(ll_SubMenuHandle,2,MF_BYPOSITION,ll_Bitmappaste,ll_Bitmappaste)
SetMenuItemBitmaps(ll_SubMenuHandle,4,MF_BYPOSITION,ll_Bitmapitl,ll_Bitmapitl)
SetMenuItemBitmaps(ll_SubMenuHandle,5,MF_BYPOSITION,ll_Bitmapcnt,ll_Bitmapcnt)
SetMenuItemBitmaps(ll_SubMenuHandle,6,MF_BYPOSITION,ll_Bitmapunderline,ll_Bitmapunderline)
end event
///

3.uo_supermenu 的方法:

of_init()
of_show_popmenu(long xpos, long ypos)    (这两个一起用, 在窗口或控件的rightbuttondown事件里用, 建立实例变量, 要注意销毁对象,不然程序退出后还在后台运行)


  

你可能感兴趣的:(PB12.5带图标菜单自定义对象)