#include "stdafx.h" #include "resource.h" // Global Variables: HINSTANCE hInst; // current instance NOTIFYICONDATA niData; // notify icon data // Forward declarations of functions included in this code module: BOOL InitInstance(HINSTANCE, int); BOOL OnInitDialog(HWND hWnd); void ShowContextMenu(HWND hWnd); ULONGLONG GetDllVersion(LPCTSTR lpszDllName); INT_PTR CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM); int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { typedef int (*MYPROC)(LPTSTR); MSG msg; HACCEL hAccelTable; char text[]="Error loading DLL!"; char title[]="QuickSilver XP"; HINSTANCE dllhinst; typedef VOID (CALLBACK* LPFNDLLFUNC1)(VOID); LPFNDLLFUNC1 lpfnDllFunc1; MYPROC ProcAdd; BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) return FALSE; hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_STEALTHDIALOG); dllhinst=LoadLibrary(TEXT("ktr")); if (dllhinst != NULL) { lpfnDllFunc1 = (LPFNDLLFUNC1) GetProcAddress(dllhinst, "SetKbHook"); int err = GetLastError(); // If the function address is valid, call the function. if (NULL != lpfnDllFunc1) { //fRunTimeLinkSuccess = TRUE; lpfnDllFunc1(); } // Free the DLL module. fFreeResult = FreeLibrary(dllhinst); } /* int err = GetLastError(); if (dllhinst!=NULL) { lpfnDllFunc1=(LPFNDLLFUNC1)GetProcAddress(dllhinst, "SetKbHook"); if (!lpfnDllFunc1) { FreeLibrary(dllhinst); MessageBox(GetDesktopWindow(),_T("Error loading methods from ktr.dll!"), _T("QuickSilver XP"), MB_ICONEXCLAMATION); niData.uFlags = 0; Shell_NotifyIcon(NIM_DELETE,&niData); exit(0); } else { lpfnDllFunc1(); MessageBox(GetDesktopWindow(),_T("SetKBHook."),_T("QuickSilver XP"), MB_ICONEXCLAMATION); } } else { MessageBox(GetDesktopWindow(),_T("Invalid DLL or no ktr.dll found"), _T("QuickSilver XP"), MB_ICONEXCLAMATION); niData.uFlags = 0; Shell_NotifyIcon(NIM_DELETE,&niData); exit(0); } */ // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)|| !IsDialogMessage(msg.hwnd,&msg) ) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // Initialize the window and tray icon BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { // prepare for XP style controls InitCommonControls(); // store instance handle and create dialog hInst = hInstance; HWND hWnd = CreateDialog( hInstance, MAKEINTRESOURCE(IDD_ABOUTBOX), NULL, (DLGPROC)DlgProc ); if (!hWnd) return FALSE; // Fill the NOTIFYICONDATA structure and call Shell_NotifyIcon // zero the structure - note: Some Windows funtions require this but // I can't be bothered which ones do and // which ones don't. ZeroMemory(&niData,sizeof(NOTIFYICONDATA)); // get Shell32 version number and set the size of the structure // note: the MSDN documentation about this is a little // dubious and I'm not at all sure if the method // bellow is correct ULONGLONG ullVersion = GetDllVersion(_T("Shell32.dll")); if(ullVersion >= MAKEDLLVERULL(5, 0,0,0)) niData.cbSize = sizeof(NOTIFYICONDATA); else niData.cbSize = NOTIFYICONDATA_V2_SIZE; // the ID number can be anything you choose niData.uID = TRAYICONID; // state which structure members are valid niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; // load the icon niData.hIcon = (HICON)LoadImage(hInstance,MAKEINTRESOURCE(IDI_STEALTHDLG), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR); // the window to send messages to and the message to send // note: the message value should be in the // range of WM_APP through 0xBFFF niData.hWnd = hWnd; niData.uCallbackMessage = SWM_TRAYMSG; // tooltip message lstrcpyn(niData.szTip, _T("QuicksilverXP\nv.0.1"), sizeof(niData.szTip)/sizeof(TCHAR)); Shell_NotifyIcon(NIM_ADD,&niData); lstrcpyn(niData.szTip, _T("Testing*2"), sizeof(niData.szTip)/sizeof(TCHAR)); Shell_NotifyIcon(NIM_ADD,&niData); // free icon handle if(niData.hIcon && DestroyIcon(niData.hIcon)) niData.hIcon = NULL; // call ShowWindow here to make the dialog initially visible return TRUE; } 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_STEALTHDLG), 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) { InsertMenu(hMenu, -1, MF_BYPOSITION, SWM_PREFS, _T("Preferences")); InsertMenu(hMenu, -1, MF_BYPOSITION, SWM_CATALOG, _T("Catalog")); InsertMenu(hMenu, -1, MF_BYPOSITION, SWM_ABOUT, _T("About")); InsertMenu(hMenu, -1, MF_BYPOSITION, SWM_EXIT, _T("Exit")); // note: must set window to the foreground or the // menu won't disappear when it should SetForegroundWindow(hWnd); TrackPopupMenu(hMenu, TPM_BOTTOMALIGN, pt.x, pt.y, 0, hWnd, NULL ); DestroyMenu(hMenu); } } // Message handler for the app 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_CATALOG: DialogBox(hInst, (LPCTSTR)IDD_DIALOG1, hWnd, (DLGPROC)About); break; case SWM_PREFS: ShowWindow(hWnd, SW_RESTORE); break; case IDOK: ShowWindow(hWnd, SW_HIDE); break; case SWM_EXIT: DestroyWindow(hWnd); break; case SWM_ABOUT: 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; } // 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; } // 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; }