void ShowMenu(){ POINT p; GetCursorPos(&p); if (go_NeedsReload()){ CreateCustomMenu(); } SetForegroundWindow(hwnd); // Win32 bug work-around TrackPopupMenu(hMenu, TPM_BOTTOMALIGN | TPM_LEFTALIGN, p.x, p.y, 0, hwnd, NULL); }
int main() { IBNibRef nibRef; WindowRef window; OSStatus err; MenuDefSpec defSpec; MenuRef menu; SInt32 gestaltResult; // Create a Nib reference passing the name of the nib file (without the .nib extension) // CreateNibReference only searches into the application bundle. err = CreateNibReference(CFSTR("main"), &nibRef); require_noerr( err, CantGetNibRef ); // Once the nib reference is created, set the menu bar. "MainMenu" is the name of the menu bar // object. This name is set in InterfaceBuilder when the nib is created. err = SetMenuBarFromNib(nibRef, CFSTR("MenuBar")); require_noerr( err, CantSetMenuBar ); // Add a Quit item if we're not running on X Gestalt( gestaltMenuMgrAttr, &gestaltResult ); if ( ( gestaltResult & gestaltMenuMgrAquaLayoutMask ) == 0 ) { MenuRef rootMenu = AcquireRootMenu(); MenuItemIndex item; GetMenuItemHierarchicalMenu( rootMenu, 2, &menu ); AppendMenuItemTextWithCFString( menu, CFSTR("Quit"), 0, kHICommandQuit, &item ); SetMenuItemCommandKey( menu, item, false, 'Q' ); ReleaseMenu( rootMenu ); } defSpec.defType = kMenuDefProcPtr; defSpec.u.defProc = NewMenuDefUPP( SampleMDEF ); // Create a standard menu CreateNewMenu( 200, 0, &menu ); SetMenuTitleWithCFString( menu, CFSTR("Sample") ); InsertMenu( menu, 0 ); AddSampleItems( menu ); // Create a custom menu CreateCustomMenu( &defSpec, 201, 0, &menu ); SetMenuTitleWithCFString( menu, CFSTR("Sample [Custom]") ); InsertMenu( menu, 0 ); AddSampleItems( menu ); // Create a standard menu CreateNewMenu( 202, 0, &menu ); SetMenuTitleWithCFString( menu, CFSTR("Shell") ); InsertMenu( menu, 0 ); AddShellItems( menu ); // Create a custom menu CreateCustomMenu( &defSpec, 203, 0, &menu ); SetMenuTitleWithCFString( menu, CFSTR("Shell [Custom]") ); InsertMenu( menu, 0 ); AddShellItems( menu ); // Create a standard menu CreateNewMenu( 204, 0, &menu ); SetMenuTitleWithCFString( menu, CFSTR("Fonts") ); InsertMenu( menu, 0 ); CreateStandardFontMenu( menu, 0, 0, 0, NULL ); // Create a custom menu CreateCustomMenu( &defSpec, 205, 0, &menu ); SetMenuTitleWithCFString( menu, CFSTR("Fonts [Custom]") ); InsertMenu( menu, 0 ); CreateStandardFontMenu( menu, 0, 0, 0, NULL ); // Then create a window. "MainWindow" is the name of the window object. This name is set in // InterfaceBuilder when the nib is created. err = CreateWindowFromNib(nibRef, CFSTR("MainWindow"), &window); require_noerr( err, CantCreateWindow ); // We don't need the nib reference anymore. DisposeNibReference(nibRef); // The window was created hidden so show it. ShowWindow( window ); // Call the event loop RunApplicationEventLoop(); CantCreateWindow: CantSetMenuBar: CantGetNibRef: return err; }