PDA

View Full Version : [C++] Problemi classe NOTIFYICONDATA


skeleton
29-02-2012, 17:05
Buona sera a tutti, vi posto subito il codice che mi da problemi
#include <windows.h>


/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)

{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
NOTIFYICONDATA icon;

/* The Window structure */
wincl.hInstance = hInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);

/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_ERROR);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;

/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);


icon.cbSize = sizeof(NOTIFYICONDATA);
icon.hWnd = hwnd;
icon.uID = 100;
icon.uFlags = NIF_TIP | NIF_ICON | NIF_MESSAGE;

icon.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
strcpy(icon.szTip, "Titolo Tray Icon");
strcpy(icon.szInfo, "BOH");





/* Make the window visible on the screen */
ShowWindow (hwnd, SW_MINIMIZE);
ShowWindow (hwnd, SW_HIDE);
Shell_NotifyIcon(NIM_ADD, &icon);

/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}

/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}


/* This function is called by the Windows function DispatchMessage() */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

return 0;
}


lavoro in windows 7 e con il dev...l'errore è: 'struct NOTIFYICONDATA' has no member named 'szInfo'

com'è possibile? :doh:

Altra domanda: come posso cambiare l'icona del programma in tray, visto che non mi appare alcuna icona ma solo uno spazio vuoto?

Grazie in anticipo.

lishi
29-02-2012, 18:21
Guarda la documentazione di NOTIFYICONDATA

http://msdn.microsoft.com/en-us/library/windows/desktop/bb773352(v=vs.85).aspx

Se guardi szInfo c'è solo per windows 2000 e maggiore.

Prova a aggiungere prima di altro codice


#define WINVER 0x0500
#define _WIN32_WINNT 0x0500

skeleton
29-02-2012, 18:36
Guarda la documentazione di NOTIFYICONDATA

http://msdn.microsoft.com/en-us/library/windows/desktop/bb773352(v=vs.85).aspx

Se guardi szInfo c'è solo per windows 2000 e maggiore.

Prova a aggiungere prima di altro codice


#define WINVER 0x0500
#define _WIN32_WINNT 0x0500

avevo letto questa specificazione, ma avendo 7 non ho pensato ci fosse problema. Inoltre avevo già letto da qualche parte di inserire quei 2 define, ma il problema non si risolve comunque :doh:

lishi
29-02-2012, 18:51
Se guardi come è definita NOTIFYICONDATA troverai in mezzo:





#if (NTDDI_VERSION >= NTDDI_WIN2K)
WCHAR szTip[128];
DWORD dwState;
DWORD dwStateMask;
WCHAR szInfo[256];
union {
UINT uTimeout;
UINT uVersion; // used with NIM_SETVERSION, values 0, 3 and 4
} DUMMYUNIONNAME;
WCHAR szInfoTitle[64];
DWORD dwInfoFlags;
#endif


Con NTDDI_VERSION che dipende da _WIN32_WINNT

#define NTDDI_VERSION NTDDI_VERSION_FROM_WIN32_WINNT(_WIN32_WINNT)


Da cui il consiglio di inserire quelle due variabili.

è importante ovviamente che siano definite Prima che venga dichiarata la struttura NOTIFIYICONDATA.
Nel tuo caso (se quello è il tutto il tuo programma) metti i due define prima dell
#include <windows.h>

skeleton
29-02-2012, 19:11
Se guardi come è definita NOTIFYICONDATA troverai in mezzo:





#if (NTDDI_VERSION >= NTDDI_WIN2K)
WCHAR szTip[128];
DWORD dwState;
DWORD dwStateMask;
WCHAR szInfo[256];
union {
UINT uTimeout;
UINT uVersion; // used with NIM_SETVERSION, values 0, 3 and 4
} DUMMYUNIONNAME;
WCHAR szInfoTitle[64];
DWORD dwInfoFlags;
#endif


Con NTDDI_VERSION che dipende da _WIN32_WINNT

#define NTDDI_VERSION NTDDI_VERSION_FROM_WIN32_WINNT(_WIN32_WINNT)


Da cui il consiglio di inserire quelle due variabili.

è importante ovviamente che siano definite Prima che venga dichiarata la struttura NOTIFIYICONDATA.
Nel tuo caso (se quello è il tutto il tuo programma) metti i due define prima dell
#include <windows.h>

ok, ora è veramente tutto chiaro :), grazie mille. tuttavia apportando anche questa modifica il problema continua a presentarsi come se non fosse cambiato niente. comunque vorrei sottolineare che non riconosce solo szInfo, szInfoTitle e uVersion, almeno secondo le prove che ho eseguito :help:

lishi
29-02-2012, 19:32
Prima prova a includere il file

#include <SDKDDKVer.h>

Prima di windows.h

Se nemmeno questo funziona prova a cercare sul tuo pc il file ShellAPI.h (normalmente sotto C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include ma non ho idea se il tuo ide si porta dietro un'altra versione)

Li dentro è dichiarato la struttura in questione.

da li si può cercare di capire se magari c'è qualcosa che non va, oppure sta usando una qualche vecchia versione del sdk di windows.

Può darsi che il compilatore sia impostato per usare gli precompiled header (capita se usi visual studio) in questo caso devi fare un rebuild all del progetto.

skeleton
29-02-2012, 19:48
Prima prova a includere il file

#include <SDKDDKVer.h>

Prima di windows.h

Se nemmeno questo funziona prova a cercare sul tuo pc il file ShellAPI.h (normalmente sotto C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include ma non ho idea se il tuo ide si porta dietro un'altra versione)

Li dentro è dichiarato la struttura in questione.

da li si può cercare di capire se magari c'è qualcosa che non va, oppure sta usando una qualche vecchia versione del sdk di windows.

Può darsi che il compilatore sia impostato per usare gli precompiled header (capita se usi visual studio) in questo caso devi fare un rebuild all del progetto.

ho provato con l'include ma non funziona.

la versione dell'ide è 6.0A e il codice è il seguente:
typedef struct _NOTIFYICONDATAA {
DWORD cbSize;
HWND hWnd;
UINT uID;
UINT uFlags;
UINT uCallbackMessage;
HICON hIcon;
#if (NTDDI_VERSION < NTDDI_WIN2K)
CHAR szTip[64];
#endif
#if (NTDDI_VERSION >= NTDDI_WIN2K)
CHAR szTip[128];
DWORD dwState;
DWORD dwStateMask;
CHAR szInfo[256];
union {
UINT uTimeout;
UINT uVersion; // used with NIM_SETVERSION, values 0, 3 and 4
} DUMMYUNIONNAME;
CHAR szInfoTitle[64];
DWORD dwInfoFlags;
#endif
#if (NTDDI_VERSION >= NTDDI_WINXP)
GUID guidItem;
#endif
#if (NTDDI_VERSION >= NTDDI_LONGHORN)
HICON hBalloonIcon;
#endif
} NOTIFYICONDATAA, *PNOTIFYICONDATAA;
typedef struct _NOTIFYICONDATAW {
DWORD cbSize;
HWND hWnd;
UINT uID;
UINT uFlags;
UINT uCallbackMessage;
HICON hIcon;
#if (NTDDI_VERSION < NTDDI_WIN2K)
WCHAR szTip[64];
#endif
#if (NTDDI_VERSION >= NTDDI_WIN2K)
WCHAR szTip[128];
DWORD dwState;
DWORD dwStateMask;
WCHAR szInfo[256];
union {
UINT uTimeout;
UINT uVersion; // used with NIM_SETVERSION, values 0, 3 and 4
} DUMMYUNIONNAME;
WCHAR szInfoTitle[64];
DWORD dwInfoFlags;
#endif
#if (NTDDI_VERSION >= NTDDI_WINXP)
GUID guidItem;
#endif
#if (NTDDI_VERSION >= NTDDI_LONGHORN)
HICON hBalloonIcon;
#endif
} NOTIFYICONDATAW, *PNOTIFYICONDATAW;
#ifdef UNICODE
typedef NOTIFYICONDATAW NOTIFYICONDATA;
typedef PNOTIFYICONDATAW PNOTIFYICONDATA;
#else
typedef NOTIFYICONDATAA NOTIFYICONDATA;
typedef PNOTIFYICONDATAA PNOTIFYICONDATA;
#endif // UNICODE

quindi a me interessa che NTDDI_VERSION sia >= di NTDDI_WIN2K, ma non va :doh:. Altra cosa, visto che mi hai segnalato la possibilità del rebuild all, io sto usando il dev

lishi
29-02-2012, 20:05
Guarda nelle opzioni del compilatore e prova a vedere se c'è opzione di specificare delle opzioni aggiuntivi e aggiungi

-D_WIN32_WINNT=0x0502

skeleton
29-02-2012, 20:15
Guarda nelle opzioni del compilatore e prova a vedere se c'è opzione di specificare delle opzioni aggiuntivi e aggiungi

-D_WIN32_WINNT=0x0502

continua a darmi lo stesso errore, non c'è scampo :doh:

lishi
29-02-2012, 20:22
Allora non ho più idee.

O meglio fatti un favore e scaricati visual studio express se vuoi programmare con le WINAPI.