cpp Copy code
#include <windows.h>
#include <shellapi.h>
#include <vector>
#include <string>
#include <memory>
constexpr auto WM_APP_TRAYICON = WM_APP + 1;
constexpr auto MAX_SHORTCUTS = 10;
struct Shortcut {
std::wstring name;
std::wstring path;
};
class FluentMenu {
public:
FluentMenu(HWND parentHwnd) : parentHwnd(parentHwnd), hwnd(nullptr), shortcuts(), lastHoverIndex(-1) {}
bool Create() {
WNDCLASSEX wc = { sizeof(WNDCLASSEX) };
wc.lpfnWndProc = WindowProc;
wc.hInstance = GetModuleHandle(nullptr);
wc.lpszClassName = L"FluentMenuClass";
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
if (!RegisterClassEx(&wc))
return false;
hwnd = CreateWindowEx(
WS_EX_TOPMOST | WS_EX_TOOLWINDOW,
L"FluentMenuClass",
L"FluentMenu",
WS_POPUP,
0, 0, 200, 0,
parentHwnd,
nullptr,
GetModuleHandle(nullptr),
this
);
if (!hwnd)
return false;
return true;
}
void Show(int x, int y) {
UpdateMenuHeight();
RECT workArea;
SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);
RECT windowRect;
GetWindowRect(hwnd, &windowRect);
int height = windowRect.bottom - windowRect.top;
int newY = y - height;
if (newY < workArea.top) newY = workArea.top;
SetWindowPos(hwnd, HWND_TOPMOST, x, newY, 0, 0, SWP_NOSIZE | SWP_SHOWWINDOW);
}
void Hide() {
ShowWindow(hwnd, SW_HIDE);
}
void AddShortcut(const std::wstring& name, const std::wstring& path) {
if (shortcuts.size() < MAX_SHORTCUTS) {
shortcuts.push_back({ name, path });
}
}
private:
HWND parentHwnd;
HWND hwnd;
std::vector<Shortcut> shortcuts;
int lastHoverIndex;
void UpdateMenuHeight() {
int itemHeight = 28;
int totalHeight = (shortcuts.size() + 2) * itemHeight + 10;
SetWindowPos(hwnd, nullptr, 0, 0, 200, totalHeight, SWP_NOMOVE | SWP_NOZORDER);
}
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
FluentMenu* pThis = nullptr;
if (uMsg == WM_NCCREATE) {
pThis = static_cast<FluentMenu*>(reinterpret_cast<CREATESTRUCT*>(lParam)->lpCreateParams);
SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pThis));
}
else {
pThis = reinterpret_cast<FluentMenu*>(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_PAINT:
OnPaint();
return 0;
case WM_LBUTTONDOWN:
OnMouseClick(LOWORD(lParam), HIWORD(lParam));
return 0;
case WM_MOUSEMOVE:
OnMouseMove(LOWORD(lParam), HIWORD(lParam));
return 0;
case WM_KILLFOCUS:
Hide();
return 0;
case WM_LBUTTONUP:
if (hwnd != GetForegroundWindow()) {
Hide();
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
void OnPaint() {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
RECT clientRect;
GetClientRect(hwnd, &clientRect);
SetGraphicsMode(hdc, GM_ADVANCED);
int radius = 16;
HRGN hRgn = CreateRoundRectRgn(0, 0, clientRect.right, clientRect.bottom, radius, radius);
SetWindowRgn(hwnd, hRgn, TRUE);
DeleteObject(hRgn);
HDC memDC = CreateCompatibleDC(hdc);
HBITMAP memBitmap = CreateCompatibleBitmap(hdc, clientRect.right, clientRect.bottom);
HBITMAP oldBitmap = (HBITMAP)SelectObject(memDC, memBitmap);
HBRUSH bgBrush = CreateSolidBrush(RGB(0, 0, 0));
FillRect(memDC, &clientRect, bgBrush);
DeleteObject(bgBrush);
SetBkMode(memDC, TRANSPARENT);
SetTextColor(memDC, RGB(255, 255, 255));
HFONT hFont = CreateFont(18, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, L"Segoe UI");
HFONT oldFont = (HFONT)SelectObject(memDC, hFont);
const int margin = 10;
const int iconSize = 16;
int y = 5;
for (size_t i = 0; i < shortcuts.size(); ++i) {
RECT itemRect = { 0, y, clientRect.right, y + 28 };
if (static_cast<int>(i) == lastHoverIndex) {
HBRUSH hoverBrush = CreateSolidBrush(RGB(0, 102, 102));
FillRect(memDC, &itemRect, hoverBrush);
DeleteObject(hoverBrush);
}
HICON hIcon = LoadIconFromPath(shortcuts[i].path);
if (hIcon) {
DrawIconEx(memDC, margin, y + (itemRect.bottom - itemRect.top - iconSize) / 2, hIcon, iconSize, iconSize, 0, nullptr, DI_NORMAL);
DestroyIcon(hIcon);
}
RECT textRect = { margin + iconSize + 5, y, clientRect.right, y + 28 };
DrawText(memDC, shortcuts[i].name.c_str(), -1, &textRect, DT_SINGLELINE | DT_VCENTER | DT_LEFT);
y += 28;
}
if (!shortcuts.empty()) {
RECT separatorRect = { 0, y, clientRect.right, y + 1 };
HBRUSH separatorBrush = CreateSolidBrush(RGB(100, 100, 100));
FillRect(memDC, &separatorRect, separatorBrush);
DeleteObject(separatorBrush);
y += 2;
}
if (shortcuts.size() < MAX_SHORTCUTS) {
RECT addItemRect = { 0, y, clientRect.right, y + 28 };
if (lastHoverIndex == static_cast<int>(shortcuts.size())) {
HBRUSH hoverBrush = CreateSolidBrush(RGB(0, 102, 102));
FillRect(memDC, &addItemRect, hoverBrush);
DeleteObject(hoverBrush);
}
RECT addTextRect = { margin, y, clientRect.right, y + 28 };
DrawText(memDC, L"Kısayol Ekle", -1, &addTextRect, DT_SINGLELINE | DT_VCENTER | DT_LEFT);
y += 28;
}
RECT exitItemRect = { 0, y, clientRect.right, y + 28 };
if (lastHoverIndex == static_cast<int>(shortcuts.size()) + 1) {
HBRUSH hoverBrush = CreateSolidBrush(RGB(0, 102, 102));
FillRect(memDC, &exitItemRect, hoverBrush);
DeleteObject(hoverBrush);
}
RECT exitTextRect = { margin, y, clientRect.right, y + 28 };
DrawText(memDC, L"Çıkış", -1, &exitTextRect, DT_SINGLELINE | DT_VCENTER | DT_LEFT);
BitBlt(hdc, 0, 0, clientRect.right, clientRect.bottom, memDC, 0, 0, SRCCOPY);
SelectObject(memDC, oldBitmap);
DeleteObject(memBitmap);
SelectObject(memDC, oldFont);
DeleteObject(hFont);
DeleteDC(memDC);
EndPaint(hwnd, &ps);
}
void OnMouseClick(int x, int y) {
int itemHeight = 28;
int index = y / itemHeight;
if (index < static_cast<int>(shortcuts.size())) {
ShellExecute(nullptr, L"open", shortcuts[index].path.c_str(), nullptr, nullptr, SW_SHOW);
}
else if (index == static_cast<int>(shortcuts.size())) {
OpenIniFile();
}
else if (index == static_cast<int>(shortcuts.size()) + 1) {
PostQuitMessage(0);
}
}
void OnMouseMove(int x, int y) {
int itemHeight = 28;
int hoverIndex = y / itemHeight;
if (hoverIndex != lastHoverIndex) {
lastHoverIndex = hoverIndex;
InvalidateRect(hwnd, nullptr, TRUE);
}
}
HICON LoadIconFromPath(const std::wstring& path) {
HICON hIcon = nullptr;
if (!path.empty()) {
ExtractIconEx(path.c_str(), 0, nullptr, &hIcon, 1);
}
return hIcon;
}
void OpenIniFile() {
// Varsayılan metin düzenleyici ile Settings.ini dosyasını aç
ShellExecute(nullptr, L"open", L"Settings.ini", nullptr, nullptr, SW_SHOW);
}
};
class TrayApp {
public:
TrayApp() : hwnd(nullptr), menu(nullptr) {}
bool Create(HINSTANCE hInstance) {
WNDCLASSEX wc = { sizeof(WNDCLASSEX) };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"TrayAppClass";
if (!RegisterClassEx(&wc))
return false;
hwnd = CreateWindowEx(0, L"TrayAppClass", nullptr, 0, 0, 0, 0, 0, nullptr, nullptr, hInstance, this);
if (!hwnd)
return false;
menu = std::make_unique<FluentMenu>(hwnd);
if (!menu->Create())
return false;
NOTIFYICONDATA nid = {};
nid.cbSize = sizeof(nid);
nid.hWnd = hwnd;
nid.uID = 1;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = WM_APP_TRAYICON;
nid.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
wcscpy_s(nid.szTip, L"FluentMenu Uygulaması");
Shell_NotifyIcon(NIM_ADD, &nid);
return true;
}
void Run() {
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
private:
HWND hwnd;
std::unique_ptr<FluentMenu> menu;
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
TrayApp* pThis = nullptr;
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_APP_TRAYICON:
if (LOWORD(lParam) == WM_RBUTTONUP) {
POINT pt;
GetCursorPos(&pt);
menu->Show(pt.x, pt.y);
}
return 0;
case WM_COMMAND:
return 0;
case WM_DESTROY:
NOTIFYICONDATA nid = {};
nid.cbSize = sizeof(nid);
nid.hWnd = hwnd;
nid.uID = 1;
Shell_NotifyIcon(NIM_DELETE, &nid);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
};
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int nCmdShow) {
TrayApp app;
if (!app.Create(hInstance)) {
MessageBox(nullptr, L"Uygulama başlatılamadı!", L"Hata", MB_ICONERROR);
return 1;
}
app.Run();
return 0;
}