Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

systray menu

4 réponses
Avatar
acropole
Bonjour,

Je suis en train de faire un menu qui apparaitra dans la barre des
taches.
Ca marche plutot bien sauf que :

1 - l'ic=F4ne n'apparait pas m=EAme si l'emplacement est disponible et que
les actions de la souris y sont disponnibles. Tout ce passe comme si
l'application ne trouvait pas le visuel de l'ic=F4ne.

2 - j'ai mis une action close qui appelle DestroyMenu(hmenu); Elle
marche et fait disparaitre le menu, mais en suite il n'est plus
disponible.

Je suis sous xp familial.

Voici le code source complet, cr=E9=E9 en partie par le appwizard :

// neurocom.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "neurocom.h"
#include "shellapi.h"
#include "strsafe.h"

#define MAX_LOADSTRING 100
#define MY_WM_NOTIFYICON WM_USER+1

NOTIFYICONDATA TrayIcon;
HICON m_hIcon;

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name

// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HHOOK hHook; // Handle du hook global

// keyboard hook
LRESULT CALLBACK HookProc (int nCode, WPARAM wParam, LPARAM lParam)
{
// Si une touche a =E9t=E9 press=E9e
if ((nCode =3D=3D HC_ACTION) && (wParam =3D=3D WM_KEYDOWN))
{
// Structure de r=E9cup=E9ration d'infos sur la touche tap=E9e
// MOUSEHOOKSTRUCT - HARDWAREHOOKSTRUCT - DESKTOP_HOOKCONTROL -
SHELLHOOKINFO - DEBUGHOOKINFO
KBDLLHOOKSTRUCT hookstruct =3D *((KBDLLHOOKSTRUCT*)lParam);

// Si la touche press=E9e est la touche PrintScreen
if(hookstruct.vkCode =3D=3D VK_LSHIFT)
{
MessageBox(NULL, _T("VK_LSHIFT Pressed"), _T("alert"), MB_OK);
}
if(hookstruct.vkCode =3D=3D VK_ESCAPE){
PostQuitMessage(0);
}
}
// Renvoi des messages au syt=E8me pour permettre d'autres hooks
return CallNextHookEx(hHook, nCode, wParam, lParam);
}

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_NEUROCOM, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable =3D LoadAccelerators(hInstance, MAKEINTRESOURCE
(IDC_NEUROCOM));

hHook =3D SetWindowsHookEx(WH_KEYBOARD_LL, HookProc, hInstance, NULL);

while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
UnhookWindowsHookEx(hHook);
Shell_NotifyIcon(NIM_DELETE,&TrayIcon);

return (int) msg.wParam;
}



//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this
code
// to be compatible with Win32 systems prior to the
'RegisterClassEx'
// function that was added to Windows 95. It is important to call
this function
// so that the application will get 'well formed' small icons
associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize =3D sizeof(WNDCLASSEX);

wcex.style =3D CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc =3D WndProc;
wcex.cbClsExtra =3D 0;
wcex.cbWndExtra =3D 0;
wcex.hInstance =3D hInstance;
wcex.hIcon =3D LoadIcon(hInstance, MAKEINTRESOURCE(IDI_NEUROCOM));
wcex.hCursor =3D LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground =3D (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName =3D MAKEINTRESOURCE(IDC_NEUROCOM);
wcex.lpszClassName =3D szWindowClass;
wcex.hIconSm =3D LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global
variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst =3D hInstance; // Store instance handle in our global variable

hWnd =3D CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance,
NULL);

if (!hWnd) return FALSE;

ZeroMemory(&TrayIcon, sizeof(NOTIFYICONDATA));
TrayIcon.cbSize =3D sizeof(NOTIFYICONDATA);
TrayIcon.hWnd =3D hWnd;
TrayIcon.uID =3D IDI_NEUROCOM;
TrayIcon.hIcon =3D LoadIcon(hInstance, IDI_APPLICATION);
TrayIcon.uCallbackMessage =3D MY_WM_NOTIFYICON;
TrayIcon.uFlags =3D NIF_ICON | NIF_MESSAGE | NIF_TIP;
StringCchCopy(TrayIcon.szTip, ARRAYSIZE(TrayIcon.szTip), L"Neurocom
Network");

Shell_NotifyIcon(NIM_ADD,&TrayIcon)? S_OK : E_FAIL;
UpdateWindow(hWnd);

return TRUE;
}

//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

switch (message)
{
case WM_COMMAND:
wmId =3D LOWORD(wParam);
wmEvent =3D HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case IDM_CLOSE:
HMENU hmenu;
hmenu =3D GetMenu(hWnd);
DestroyMenu(hmenu);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case MY_WM_NOTIFYICON :
if(lParam =3D=3D WM_LBUTTONUP){
ShowWindow(hWnd,SW_SHOW);
}else if(lParam =3D=3D WM_RBUTTONUP){
HMENU hmenu;
HMENU hpopup;
POINT pos;
GetCursorPos(&pos);
hmenu =3D GetMenu(hWnd);
hpopup =3D GetSubMenu(hmenu, 0);
SetForegroundWindow(hWnd);
TrackPopupMenuEx(hpopup, 0, pos.x, pos.y, hWnd,
NULL);
DestroyMenu(hmenu);
}
return 0;
case WM_PAINT:
hdc =3D BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

4 réponses

Avatar
Marc
"acropole" a écrit dans le message de news:


TrayIcon.hIcon = LoadIcon(hInstance, IDI_APPLICATION);



Tu as bien vérifié que TrayIcon.hIcon n'était pas NULL ?
Avatar
Stephane13
acropole wrote:

1 - l'icône n'apparait pas même si l'emplacement est disponible et que
les actions de la souris y sont disponnibles. Tout ce passe comme si
l'application ne trouvait pas le visuel de l'icône.



le handle de l'icone est certainement = 0


2 - j'ai mis une action close qui appelle DestroyMenu(hmenu); Elle
marche et fait disparaitre le menu, mais en suite il n'est plus
disponible.



C'est logique, si tu détruis ton menu, il va disparaitre ...
pourquoi le détruis-tu ?
Avatar
acropole
On 17 avr, 11:50, Stephane13 wrote:
acropole wrote:
> 1 - l'icône n'apparait pas même si l'emplacement est disponible et que
> les actions de la souris y sont disponnibles. Tout ce passe comme si
> l'application ne trouvait pas le visuel de l'icône.

le handle de l'icone est certainement  = 0



> 2 - j'ai mis une action close qui appelle DestroyMenu(hmenu); Elle
> marche et fait disparaitre le menu, mais en suite il n'est plus
> disponible.

C'est logique, si tu détruis ton menu, il va disparaitre ...
pourquoi le détruis-tu ?



Effectivement l'icone était null. Je m'en suis rendu compte aprés.
J'ai trouvé la solution en chargent directement IDI_NEUROCOM et il
apparait correctement.
Pour le menu je fais destroyMenu() parce que c'est ce que j'ai lu dans
ce tutoriel : http://chgi.developpez.com/windows/trayicon/
Je n'ai pas vu de fonction HideMenu, je vais vérifier.

Merci.
Avatar
Stephane13
acropole wrote:

Pour le menu je fais destroyMenu() parce que c'est ce que j'ai lu dans
ce tutoriel : http://.../.../trayicon/
Je n'ai pas vu de fonction HideMenu, je vais vérifier



Mouais, pas terrible ce "tutoriel", DestroyMenu() ça se fait presque
jamais, à part pour remplacer un menu existant et encore, on fait
ModifyMenu()
Pour la systray, suis plutôt l'exemple TrayNot de Microsoft du SDK qui
est dans ...SamplesWinUIShell