bool ShowTrayIcon() {
NOTIFYICONDATA nid = { sizeof(NOTIFYICONDATA) };
nid.hWnd = hwnd;
nid.uID = 1;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = WM_APP_TRAYICON;
nid.hIcon = LoadIcon(NULL, IDI_APPLICATION);
if (!nid.hIcon) {
return false;
}
wcscpy_s(nid.szTip, L"TrayApp");
return Shell_NotifyIcon(NIM_ADD, &nid) != FALSE;
}
void UpdateTrayMenu() {
HMENU hMenu = CreatePopupMenu();
for (size_t i = 0; i < shortcuts.size(); ++i) {
InsertMenu(hMenu, i, MF_BYPOSITION | MF_STRING, 1000 + i, shortcuts[i].name.c_str());
}
InsertMenu(hMenu, shortcuts.size(), MF_BYPOSITION | MF_STRING, 999, L"Add Shortcut");
InsertMenu(hMenu, shortcuts.size() + 1, MF_BYPOSITION | MF_STRING, 998, L"Exit");
SetForegroundWindow(hwnd);
POINT pt;
GetCursorPos(&pt);
// Koyu mod için menü stilini ayarla
MENUINFO mi = { sizeof(MENUINFO) };
mi.fMask = MIM_BACKGROUND | MIM_APPLYTOSUBMENUS;
mi.hbrBack = CreateSolidBrush(RGB(43, 43, 43)); // Koyu arka plan rengi
SetMenuInfo(hMenu, &mi);
// Koyu mod için menü öğesi stilini ayarla
MENUITEMINFO mii = { sizeof(MENUITEMINFO) };
mii.fMask = MIIM_STATE | MIIM_ID | MIIM_STRING | MIIM_FTYPE;
mii.fType = MFT_OWNERDRAW;
for (int i = 0; i < GetMenuItemCount(hMenu); i++) {
GetMenuItemInfo(hMenu, i, TRUE, &mii);
SetMenuItemInfo(hMenu, i, TRUE, &mii);
}
TrackPopupMenu(hMenu, TPM_RIGHTALIGN | TPM_BOTTOMALIGN, pt.x, pt.y, 0, hwnd, nullptr);
DestroyMenu(hMenu);
}
void LaunchShortcut(int index) {
if (index >= 0 && index < static_cast<int>(shortcuts.size())) {
ShellExecute(nullptr, L"open", shortcuts[index].path.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
}
}
void AddShortcutDialog() {
OPENFILENAME ofn;
WCHAR szFile[260] = { 0 };
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = L"Executable\0*.exe\0All\0*.*\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn) == TRUE) {
std::wstring fullPath(szFile);
std::wstring fileName = fullPath.substr(fullPath.find_last_of(L"\\") + 1);
AddShortcut(fileName, fullPath);
}
}
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
TrayApp* pThis;
if (uMsg == WM_NCCREATE) {
pThis = static_cast<TrayApp*>(reinterpret_cast<CREATESTRUCT*>(lParam)->lpCreateParams);
SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pThis));
}
else {
pThis = reinterpret_cast<TrayApp*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
}
if (pThis) {
return pThis->HandleMessage(hwnd, uMsg, wParam, lParam);
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
LRESULT HandleMessage(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CREATE:
return 0;
case WM_APP_TRAYICON:
if (lParam == WM_RBUTTONUP) {
UpdateTrayMenu();
}
return 0;
case WM_COMMAND:
if (LOWORD(wParam) >= 1000 && LOWORD(wParam) < 1000 + MAX_SHORTCUTS) {
LaunchShortcut(LOWORD(wParam) - 1000);
}
else if (LOWORD(wParam) == 999) {
AddShortcutDialog();
}
else if (LOWORD(wParam) == 998) {
PostQuitMessage(0);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_MEASUREITEM:
OnMeasureItem((LPMEASUREITEMSTRUCT)lParam);
return TRUE;
case WM_DRAWITEM:
OnDrawItem((LPDRAWITEMSTRUCT)lParam);
return TRUE;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
void OnMeasureItem(LPMEASUREITEMSTRUCT lpmis) {
lpmis->itemHeight = 30; // Menü öğesi yüksekliği
}
void OnDrawItem(LPDRAWITEMSTRUCT lpdis) {
COLORREF crText = RGB(255, 255, 255); // Beyaz metin rengi
COLORREF crBackground = RGB(43, 43, 43); // Koyu arka plan rengi
FillRect(lpdis->hDC, &lpdis->rcItem, CreateSolidBrush(crBackground));
if (lpdis->itemState & ODS_SELECTED) {
// Seçili öğe için farklı bir renk kullan
crBackground = RGB(65, 65, 65);
FillRect(lpdis->hDC, &lpdis->rcItem, CreateSolidBrush(crBackground));
}
SetTextColor(lpdis->hDC, crText);
SetBkMode(lpdis->hDC, TRANSPARENT);
WCHAR szString[256];
GetMenuString((HMENU)lpdis->hwndItem, lpdis->itemID, szString, sizeof(szString), MF_BYCOMMAND);
DrawText(lpdis->hDC, szString, -1, &lpdis->rcItem, DT_SINGLELINE | DT_VCENTER | DT_LEFT);
}