popupmenu ohne fenster



  • hi, ich möchte eine minianwendung schreiben, die ein menu öffnet ohne ein fester offen zu haben.
    Das ist an sich nicht so schwer.

    Das menu öffne ich mit TrackPopupMenu.
    Das Problem ist, dass die funktion blockert und scheinbar nur durch einen klick auf eins der menuitems beendet werden kann.

    gibt es eine möglichkeit (oder andere Art und Weise ein Menu zu öffnen) sich in die messageprocedur des Menus zu klinken, um zb. Tastaturevents zu verarbeiten?

    #include "stdafx.h"
    #include "resource.h"
    
    #include "stdio.h"
    
    // Global Variables:
    HINSTANCE hInst;								// current instance
    TCHAR* szTitle = TEXT("test");					// The title bar text
    
    // Forward declarations of functions included in this code module:
    ATOM				MyRegisterClass(HINSTANCE hInstance);
    BOOL				InitInstance(HINSTANCE, int);
    LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
    
    HMENU s_hmenu;
    const int s_count = 10;
    char  s_items[s_count][10];
    static const int IDBASE = 5236;
    HWND  s_hwnd;
    
    void createFileMenu()
    {
       s_hmenu = CreatePopupMenu();
    
       for(int i=0; i<s_count; ++i)
       {
         sprintf(s_items[i], "item %d", i);
         AppendMenu(s_hmenu, MF_STRING, IDBASE+i, s_items[i]);
       }
    }
    
    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
    	UNREFERENCED_PARAMETER(hPrevInstance);
    	UNREFERENCED_PARAMETER(lpCmdLine);
    
    	MSG msg;
    
    	// Initialize global strings
    	MyRegisterClass(hInstance);
    
    	// Perform application initialization:
    	if (!InitInstance (hInstance, nCmdShow))
    	{
    		return FALSE;
    	}
    
      createFileMenu();
    
      POINT curCurPos;
      GetCursorPos(&curCurPos);
      TrackPopupMenu(s_hmenu, TPM_LEFTALIGN	|TPM_LEFTBUTTON, curCurPos.x, curCurPos.y, 0, s_hwnd, NULL);
    
    	// Main message loop:
    	while (GetMessage(&msg, NULL, 0, 0))
    	{
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    	}
    
    	return (int) msg.wParam;
    }
    
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
    	WNDCLASSEX wcex;
    
    	wcex.cbSize = sizeof(WNDCLASSEX);
    
    	wcex.style			  = 0;
    	wcex.lpfnWndProc	= WndProc;
    	wcex.cbClsExtra		= 0;
    	wcex.cbWndExtra		= 0;
    	wcex.hInstance		= hInstance;
    	wcex.hIcon			= 0;
    	wcex.hCursor		= 0;
    	wcex.hbrBackground	= 0;
    	wcex.lpszMenuName	= 0;
    	wcex.lpszClassName	= szTitle;
    	wcex.hIconSm		= 0;
    
    	return RegisterClassEx(&wcex);
    }
    
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
    
       hInst = hInstance; // Store instance handle in our global variable
    
       s_hwnd = CreateWindow(szTitle, szTitle, WS_OVERLAPPEDWINDOW,
                             CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
    
       if (!s_hwnd)
       {
          return FALSE;
       }
    
       return TRUE;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	int wmId, wmEvent;
    	PAINTSTRUCT ps;
    	HDC hdc;
    
    	switch (message)
    	{
    	case WM_COMMAND:
    		wmId    = LOWORD(wParam);
    		wmEvent = HIWORD(wParam);
    		// Parse the menu selections:
        if(    (wmId >= IDBASE)
            && (wmId <= s_count + IDBASE) )
        {
          int id = wmId-IDBASE;
          MessageBox(s_hwnd, s_items[id], "item", 0);
          ExitProcess(0);
        }
    		break;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    	default:
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    

Anmelden zum Antworten