Click to See Complete Forum and Search --> : Tray Icon DLL help - almost there..


Saeven
August 11th, 2004, 01:23 AM
Hello everyone,

I'm trying to build myself a tray icon class with stuff I've found around the net. This will be a DLL called over from Java using JNI - so a class would be very nice for me to use. When it comes to Windows programming however, I'm a complete noob, and I would hoping the kind people here could help. I have these compiler errors remaining:


SaevenTray.h(26): error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'DLGPROC'
SaevenTray.h(36): error C2039: 'guidItem' : is not a member of '_NOTIFYICONDATAA'


Here is the class in question and related includes

SaevenTray.h

#pragma once
#include <windows.h>
#include "stdafx.h"
#include "resource.h"
#include "constants.h"


#define TRAYICONID 1 // ID number for the Notify Icon
#define SWM_TRAYMSG WM_APP // the message ID sent to our window

#define SWM_SHOW WM_APP + 1 // show the window
#define SWM_HIDE WM_APP + 2 // hide the window
#define SWM_EXIT WM_APP + 3 // close the window

class SaevenTray{
private:
HINSTANCE hInst;
NOTIFYICONDATA niData;

public:


SaevenTray( HINSTANCE hInstance ){
hInst = hInstance;
InitCommonControls();
HWND hWnd = CreateDialog( hInstance, MAKEINTRESOURCE( IDD_DLG_DIALOG ), NULL, (DLGPROC)DlgProc );
if (!hWnd)
return;

ZeroMemory( &niData, sizeof( NOTIFYICONDATA ) );

ULONGLONG ullVersion = GetDllVersion( _T( "Shell32.dll" ) );
if( ullVersion >= MAKEDLLVERULL(5,0,0,0 ) )
niData.cbSize = sizeof( NOTIFYICONDATA );
else
niData.cbSize = NOTIFYICONDATA_V2_SIZE;

niData.uID = TRAYICONID;

// state which structure members are valid
niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
niData.hIcon = (HICON)LoadImage( hInstance, MAKEINTRESOURCE( IDI_SAEVENDLG ), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR );
niData.hWnd = hWnd;
niData.uCallbackMessage = SWM_TRAYMSG;

// tooltip message
lstrcpyn(niData.szTip, _T("Time flies like an arrow but\n fruit flies like a banana!"), sizeof(niData.szTip)/sizeof(TCHAR));

Shell_NotifyIcon( NIM_ADD, &niData );

// free icon handle
if( niData.hIcon && DestroyIcon( niData.hIcon ) )
niData.hIcon = NULL;

return;
}

~SaevenTray(void){

}

INT_PTR CALLBACK DlgProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ){
int wmId, wmEvent;

switch (message)
{
case SWM_TRAYMSG:
switch(lParam)
{
case WM_LBUTTONDBLCLK:
ShowWindow(hWnd, SW_RESTORE);
break;
case WM_RBUTTONDOWN:
case WM_CONTEXTMENU:
ShowContextMenu(hWnd);
}
break;
case WM_SYSCOMMAND:
if((wParam & 0xFFF0) == SC_MINIMIZE)
{
ShowWindow(hWnd, SW_HIDE);
return 1;
}
else if(wParam == IDM_ABOUT)
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);

switch (wmId)
{
case SWM_SHOW:
ShowWindow(hWnd, SW_RESTORE);
break;
case SWM_HIDE:
case IDOK:
ShowWindow(hWnd, SW_HIDE);
break;
case SWM_EXIT:
DestroyWindow(hWnd);
break;
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
}
return 1;
case WM_INITDIALOG:
return OnInitDialog(hWnd);
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
niData.uFlags = 0;
Shell_NotifyIcon(NIM_DELETE,&niData);
PostQuitMessage(0);
break;
}
return 0;
}

BOOL OnInitDialog(HWND hWnd)
{
HMENU hMenu = GetSystemMenu(hWnd,FALSE);
if (hMenu)
{
AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hMenu, MF_STRING, IDM_ABOUT, _T("About"));
}
HICON hIcon = (HICON)LoadImage( hInst, MAKEINTRESOURCE( IDI_SAEVENDLG ), IMAGE_ICON, 0,0, LR_SHARED|LR_DEFAULTSIZE );
SendMessage( hWnd, WM_SETICON,ICON_BIG,(LPARAM)hIcon );
SendMessage( hWnd, WM_SETICON,ICON_SMALL,(LPARAM)hIcon );
return TRUE;
}

// Name says it all
void ShowContextMenu( HWND hWnd )
{
POINT pt;
GetCursorPos( &pt );
HMENU hMenu = CreatePopupMenu();
if( hMenu ){
if( IsWindowVisible(hWnd) )
InsertMenu( hMenu, -1, MF_BYPOSITION, SWM_HIDE, _T("Hide") );
else
InsertMenu( hMenu, -1, MF_BYPOSITION, SWM_SHOW, _T("Show") );

InsertMenu( hMenu, -1, MF_BYPOSITION, SWM_EXIT, _T("Exit") );
SetForegroundWindow( hWnd );
TrackPopupMenu( hMenu, TPM_BOTTOMALIGN, pt.x, pt.y, 0, hWnd, NULL );
DestroyMenu(hMenu);
}
}

// Get dll version number
ULONGLONG GetDllVersion(LPCTSTR lpszDllName)
{
ULONGLONG ullVersion = 0;
HINSTANCE hinstDll;
hinstDll = LoadLibrary(lpszDllName);
if(hinstDll)
{
DLLGETVERSIONPROC pDllGetVersion;
pDllGetVersion = (DLLGETVERSIONPROC)GetProcAddress(hinstDll, "DllGetVersion");
if(pDllGetVersion)
{
DLLVERSIONINFO dvi;
HRESULT hr;
ZeroMemory(&dvi, sizeof(dvi));
dvi.cbSize = sizeof(dvi);
hr = (*pDllGetVersion)(&dvi);
if(SUCCEEDED(hr))
ullVersion = MAKEDLLVERULL(dvi.dwMajorVersion, dvi.dwMinorVersion,0,0);
}
FreeLibrary(hinstDll);
}
return ullVersion;
}
};


constants.h

#include <windows.h>

// Message handler for about box.
LRESULT CALLBACK About( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam ){
switch ( message ){
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}


resource.h

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by Win32 Dialog.rc
//
#define IDR_MANIFEST 1
#define IDD_DLG_DIALOG 102
#define IDD_ABOUTBOX 103
#define IDM_ABOUT 104
#define IDC_MYICON 105
#define IDC_SAEVENDIALOG 106
#define IDI_SAEVENDLG 107
#define IDC_STATIC -1

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NO_MFC 1
#define _APS_NEXT_RESOURCE_VALUE 110
#define _APS_NEXT_COMMAND_VALUE 32771
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 110
#endif
#endif


Any help you can offer is appreciated.
I look forward to your replies.

What I really don't understand though, is the guidItem error. That error does not appear when the responsible code is set outside a class.

Many thanks!

kirants
August 11th, 2004, 01:16 PM
The callback function has to be a static function. It cannot be a normal member function of the class. A static function exists outside a class.