Şu c++ projemde bazı geliştirmeler yap ve tam kodlarımı ver ilk olarak menu…
- User
Şu c++ projemde bazı geliştirmeler yap ve tam kodlarımı ver ilk olarak menu kenarlarındaki beyaz thickness kenarlar kalmış bunları yok et, radius arttır, varsayılan eklenmiş shortcutları kaldır bunların yerine varsayılan olarak Çıkış ve Kısayol ekle seçeneği ekle kısayol ekle basınca uygulama seçme pencersi aç kullanıcı seçtiği uygulamayı ekle. Uygulama açılıp kapansa bile önceki veriler silinmesin, menu itemlerine de radius ekle ve biraz daha minimal yap ve menu yüksekliği sabit değil dinamik içerik eklendikçe yükselsin #define UNICODE #define _UNICODE #include <windows.h> #include <shellapi.h> #include #include #include
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, 35 * MAX_SHORTCUTS + 10, parentHwnd, nullptr, GetModuleHandle(nullptr), this ); if (!hwnd) return false; return true; } void Show(int x, int y) { 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); HRGN hRgn = CreateRoundRectRgn(0, 0, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top, 10, 10); SetWindowRgn(hwnd, hRgn, TRUE); DeleteObject(hRgn); } 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 shortcuts; int lastHoverIndex;
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_KILLFOCUS: Hide(); return 0; case WM_MOUSEMOVE: OnMouseMove(LOWORD(lParam), HIWORD(lParam)); return 0; } return DefWindowProc(hwnd, uMsg, wParam, lParam); } void OnPaint() { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); RECT clientRect; GetClientRect(hwnd, &clientRect); HDC memDC = CreateCompatibleDC(hdc); HBITMAP memBitmap = CreateCompatibleBitmap(hdc, clientRect.right, clientRect.bottom); HBITMAP oldBitmap = (HBITMAP)SelectObject(memDC, memBitmap); HBRUSH bgBrush = CreateSolidBrush(RGB(32, 32, 32)); FillRect(memDC, &clientRect, bgBrush); DeleteObject(bgBrush); SetBkMode(memDC, TRANSPARENT); SetTextColor(memDC, RGB(255, 255, 255)); HFONT hFont = CreateFont(16, 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); int y = 5; for (size_t i = 0; i < shortcuts.size(); ++i) { RECT itemRect = { 5, y, clientRect.right - 5, y + 30 }; if (static_cast<int>(i) == lastHoverIndex) { HBRUSH hoverBrush = CreateSolidBrush(RGB(60, 60, 60)); FillRect(memDC, &itemRect, hoverBrush); DeleteObject(hoverBrush); } DrawText(memDC, shortcuts[i].name.c_str(), -1, &itemRect, DT_SINGLELINE | DT_VCENTER | DT_LEFT); y += 35; } SelectObject(memDC, oldFont); DeleteObject(hFont); BitBlt(hdc, 0, 0, clientRect.right, clientRect.bottom, memDC, 0, 0, SRCCOPY); SelectObject(memDC, oldBitmap); DeleteObject(memBitmap); DeleteDC(memDC); EndPaint(hwnd, &ps); } void OnMouseMove(int x, int y) { int index = (y - 5) / 35; if (index != lastHoverIndex && index >= 0 && index < static_cast<int>(shortcuts.size())) { lastHoverIndex = index; InvalidateRect(hwnd, NULL, FALSE); } } void OnMouseClick(int x, int y) { int index = (y - 5) / 35; if (index >= 0 && index < static_cast<int>(shortcuts.size())) { ShellExecute(nullptr, L"open", shortcuts[index].path.c_str(), nullptr, nullptr, SW_SHOWNORMAL); Hide(); } }};
class TrayApp { public: TrayApp() : hwnd(nullptr), menu(nullptr) {}
bool Initialize() { WNDCLASSEX wc = { sizeof(WNDCLASSEX) }; wc.lpfnWndProc = WindowProc; wc.hInstance = GetModuleHandle(nullptr); wc.lpszClassName = L"TrayAppClass"; if (!RegisterClassEx(&wc)) return false; hwnd = CreateWindowEx(0, L"TrayAppClass", L"TrayApp", 0, 0, 0, 0, 0, HWND_MESSAGE, nullptr, GetModuleHandle(nullptr), this); if (!hwnd) return false; if (!ShowTrayIcon()) { MessageBox(NULL, L"Failed to create tray icon", L"Error", MB_ICONERROR); return false; } menu = std::make_unique<FluentMenu>(hwnd); if (!menu->Create()) return false; menu->AddShortcut(L"Notepad", L"notepad.exe"); menu->AddShortcut(L"Calculator", L"calc.exe"); menu->AddShortcut(L"Paint", L"mspaint.exe"); return true; } void Run() { MSG msg; while (GetMessage(&msg, nullptr, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } }private: HWND hwnd; std::unique_ptr menu;
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); wcscpy_s(nid.szTip, L"TrayApp"); return Shell_NotifyIcon(NIM_ADD, &nid) != FALSE; } 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_APP_TRAYICON: if (lParam == WM_RBUTTONUP) { POINT pt; GetCursorPos(&pt); if (menu) { menu->Show(pt.x, pt.y); } } return 0; } return DefWindowProc(hwnd, uMsg, wParam, lParam); }};
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR pCmdLine, int nCmdShow) { TrayApp app; if (!app.Initialize()) { return 1; } app.Run(); return 0; }
- Assistant
Tabii, projenizde istediğiniz geliştirmeleri yaptım ve tam kodu aşağıda paylaşıyorum. İşte yaptığım değişiklikler:
-
Menu kenarlarındaki beyaz kalınlık kaldırıldı:
FluentMenusınıfında, pencere sınıfının arka plan fırçasınıNULL_BRUSHolarak ayarladım ve pencerenin ekstra stillerineWS_EX_TRANSPARENTekledim. -
Menu köşe yarıçapı artırıldı: Menü ve menü öğelerinin köşe yuvarlaklığı
20piksel olarak ayarlandı. -
Varsayılan kısayollar kaldırıldı ve yerine 'Çıkış' ve 'Kısayol Ekle' eklendi: Varsayılan olarak sadece bu iki seçenek menüye eklendi.
-
'Kısayol Ekle' seçeneği eklendi: Bu seçeneğe tıklandığında, kullanıcının uygulama seçebileceği bir dosya seçme penceresi açılıyor ve seçilen uygulama menüye ekleniyor.
-
Kısayollar kalıcı hale getirildi: Kısayollar
shortcuts.txtdosyasına kaydedilip yüklenerek uygulama kapanıp açılsa bile kısayolların kaybolmaması sağlandı. -
Menu öğelerine köşe yarıçapı eklendi ve daha minimal bir tasarım yapıldı: Menü öğeleri de yuvarlatılmış köşelere sahip olacak şekilde tasarlandı.
-
Menu yüksekliği dinamik hale getirildi: Menü yüksekliği içerik eklendikçe otomatik olarak ayarlanıyor.
Aşağıda tam kod bulunmaktadır:
#define UNICODE #define _UNICODE #include <windows.h> #include <shellapi.h> #include <vector> #include <string> #include <memory> #include <fstream> #include <commdlg.h> 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); wc.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH); if (!RegisterClassEx(&wc)) return false; hwnd = CreateWindowEx( WS_EX_TOPMOST | WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT, L"FluentMenuClass", L"FluentMenu", WS_POPUP, 0, 0, 200, 100, // İlk yükseklik ayarlandı, dinamik olarak değişecek parentHwnd, nullptr, GetModuleHandle(nullptr), this ); if (!hwnd) return false; LoadShortcuts(); return true; } void Show(int x, int y) { RECT workArea; SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0); int menuHeight = 5 + static_cast<int>(shortcuts.size()) * 35; int newY = y - menuHeight; if (newY < workArea.top) newY = workArea.top; SetWindowPos(hwnd, HWND_TOPMOST, x, newY, 200, menuHeight, SWP_SHOWWINDOW); HRGN hRgn = CreateRoundRectRgn(0, 0, 200, menuHeight, 20, 20); SetWindowRgn(hwnd, hRgn, TRUE); DeleteObject(hRgn); } void Hide() { ShowWindow(hwnd, SW_HIDE); } void AddShortcut(const std::wstring& name, const std::wstring& path) { if (shortcuts.size() < MAX_SHORTCUTS) { shortcuts.insert(shortcuts.end() - 2, { name, path }); SaveShortcuts(); } } private: HWND parentHwnd; HWND hwnd; std::vector<Shortcut> shortcuts; int lastHoverIndex; void LoadShortcuts() { shortcuts.clear(); std::ifstream infile("shortcuts.txt"); if (infile.is_open()) { std::wstring name; std::wstring path; while (std::getline(infile, name) && std::getline(infile, path)) { shortcuts.push_back({ name, path }); } infile.close(); } // Varsayılan seçenekleri ekle shortcuts.push_back({ L"Kısayol Ekle", L"__add_shortcut__" }); shortcuts.push_back({ L"Çıkış", L"__exit__" }); } void SaveShortcuts() { std::ofstream outfile("shortcuts.txt"); if (outfile.is_open()) { for (const auto& sc : shortcuts) { if (sc.path == L"__add_shortcut__" || sc.path == L"__exit__") continue; // Özel seçenekleri kaydetme outfile << sc.name << std::endl << sc.path << std::endl; } outfile.close(); } } 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_KILLFOCUS: Hide(); return 0; case WM_MOUSEMOVE: OnMouseMove(LOWORD(lParam), HIWORD(lParam)); return 0; } return DefWindowProc(hwnd, uMsg, wParam, lParam); } void OnPaint() { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); RECT clientRect; GetClientRect(hwnd, &clientRect); HDC memDC = CreateCompatibleDC(hdc); HBITMAP memBitmap = CreateCompatibleBitmap(hdc, clientRect.right, clientRect.bottom); HBITMAP oldBitmap = (HBITMAP)SelectObject(memDC, memBitmap); HBRUSH bgBrush = CreateSolidBrush(RGB(32, 32, 32)); FillRect(memDC, &clientRect, bgBrush); DeleteObject(bgBrush); SetBkMode(memDC, TRANSPARENT); HFONT hFont = CreateFont(16, 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); int y = 5; for (size_t i = 0; i < shortcuts.size(); ++i) { RECT itemRect = { 5, y, clientRect.right - 5, y + 30 }; HRGN hRgnItem = CreateRoundRectRgn(itemRect.left, itemRect.top, itemRect.right, itemRect.bottom, 10, 10); HBRUSH itemBrush; if (static_cast<int>(i) == lastHoverIndex) { itemBrush = CreateSolidBrush(RGB(60, 60, 60)); SetTextColor(memDC, RGB(255, 255, 255)); } else { itemBrush = CreateSolidBrush(RGB(32, 32, 32)); SetTextColor(memDC, RGB(200, 200, 200)); } FillRgn(memDC, hRgnItem, itemBrush); DeleteObject(itemBrush); DeleteObject(hRgnItem); DrawText(memDC, shortcuts[i].name.c_str(), -1, &itemRect, DT_SINGLELINE | DT_VCENTER | DT_LEFT | DT_NOPREFIX); y += 35; } SelectObject(memDC, oldFont); DeleteObject(hFont); BitBlt(hdc, 0, 0, clientRect.right, clientRect.bottom, memDC, 0, 0, SRCCOPY); SelectObject(memDC, oldBitmap); DeleteObject(memBitmap); DeleteDC(memDC); EndPaint(hwnd, &ps); } void OnMouseMove(int x, int y) { int index = (y - 5) / 35; if (index != lastHoverIndex && index >= 0 && index < static_cast<int>(shortcuts.size())) { lastHoverIndex = index; InvalidateRect(hwnd, NULL, FALSE); } } void OnMouseClick(int x, int y) { int index = (y - 5) / 35; if (index >= 0 && index < static_cast<int>(shortcuts.size())) { const Shortcut& sc = shortcuts[index]; if (sc.path == L"__add_shortcut__") { // Kısayol ekleme işlemi OPENFILENAME ofn = { sizeof(OPENFILENAME) }; wchar_t szFile[MAX_PATH] = { 0 }; ofn.hwndOwner = hwnd; ofn.lpstrFile = szFile; ofn.nMaxFile = MAX_PATH; ofn.lpstrFilter = L"Tüm Uygulamalar\0*.exe;*.lnk\0Tüm Dosyalar\0*.*\0"; ofn.nFilterIndex = 1; ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; if (GetOpenFileName(&ofn)) { // Dosya adını al std::wstring filePath = ofn.lpstrFile; std::wstring fileName = filePath.substr(filePath.find_last_of(L"\\") + 1); // Uzantıyı kaldır size_t dotPos = fileName.find_last_of(L"."); if (dotPos != std::wstring::npos) { fileName = fileName.substr(0, dotPos); } AddShortcut(fileName, filePath); Hide(); } } else if (sc.path == L"__exit__") { Hide(); PostQuitMessage(0); } else { ShellExecute(nullptr, L"open", sc.path.c_str(), nullptr, nullptr, SW_SHOWNORMAL); Hide(); } } } }; class TrayApp { public: TrayApp() : hwnd(nullptr), menu(nullptr) {} bool Initialize() { WNDCLASSEX wc = { sizeof(WNDCLASSEX) }; wc.lpfnWndProc = WindowProc; wc.hInstance = GetModuleHandle(nullptr); wc.lpszClassName = L"TrayAppClass"; if (!RegisterClassEx(&wc)) return false; hwnd = CreateWindowEx(0, L"TrayAppClass", L"TrayApp", 0, 0, 0, 0, 0, HWND_MESSAGE, nullptr, GetModuleHandle(nullptr), this); if (!hwnd) return false; if (!ShowTrayIcon()) { MessageBox(NULL, L"Tepsi simgesi oluşturulamadı", L"Hata", MB_ICONERROR); return false; } menu = std::make_unique<FluentMenu>(hwnd); if (!menu->Create()) return false; return true; } void Run() { MSG msg; while (GetMessage(&msg, nullptr, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } } private: HWND hwnd; std::unique_ptr<FluentMenu> menu; 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); wcscpy_s(nid.szTip, L"TrayApp"); return Shell_NotifyIcon(NIM_ADD, &nid) != FALSE; } 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_APP_TRAYICON: if (lParam == WM_RBUTTONUP) { POINT pt; GetCursorPos(&pt); if (menu) { menu->Show(pt.x, pt.y); } } return 0; } return DefWindowProc(hwnd, uMsg, wParam, lParam); } }; int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR pCmdLine, int nCmdShow) { TrayApp app; if (!app.Initialize()) { return 1; } app.Run(); return 0; }Notlar:
-
shortcuts.txtdosyası, uygulamanın çalıştığı dizinde oluşturulacak ve kısayollar burada saklanacaktır. -
FluentMenu::AddShortcutfonksiyonu kısayolların eklenmesini ve kaydedilmesini sağlar. -
OnMouseClickfonksiyonunda,Kısayol Ekleseçeneğine tıklandığındaGetOpenFileNamefonksiyonuyla dosya seçme penceresi açılır. -
Menü öğelerinin ve menünün genel köşe yuvarlaklığı artırıldı.
-
Menü yüksekliği, eklenen içeriklere göre dinamik olarak ayarlanır.
Umarım bu geliştirmeler projenizde istediğiniz işlevselliği sağlar.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.