Skip to content

Instantly share code, notes, and snippets.

@Yangff
Last active June 1, 2021 05:33
Show Gist options
  • Save Yangff/b95bc687bd52e11531cf35a923910527 to your computer and use it in GitHub Desktop.
Save Yangff/b95bc687bd52e11531cf35a923910527 to your computer and use it in GitHub Desktop.
#include <windows.h>
#include <tchar.h>
#include <windowsx.h>
bool mouseHover = false;
bool menuUp = false;
HWND hWMenu, hWndMain;
LRESULT CALLBACK WndProcMenu(
_In_ HWND hWnd,
_In_ UINT message,
_In_ WPARAM wParam,
_In_ LPARAM lParam
) {
if (!menuUp)
return DefWindowProc(hWnd, message, wParam, lParam);
switch (message)
{
case WM_NCACTIVATE:
if (!wParam) {
menuUp = false;
ShowWindow(hWnd, HIDE_WINDOW);
PostMessage(hWndMain, WM_NCACTIVATE, 0, 0);
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
LRESULT CALLBACK WndProc(
_In_ HWND hWnd,
_In_ UINT message,
_In_ WPARAM wParam,
_In_ LPARAM lParam
) {
PAINTSTRUCT ps;
HDC hdc;
TCHAR MenuText[] = _T("RClick For Menu");
RECT rMENU = { 10,10,130,45 };
switch (message)
{
case WM_PAINT: {
hdc = BeginPaint(hWnd, &ps);
COLORREF c;
if (mouseHover) {
c = 0xa8a8a8;
}
else {
c = 0xc1c1c1;
}
HBRUSH b = CreateSolidBrush(c);
FillRect(hdc, &rMENU, b);
DeleteObject(b);
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc,
15, 15,
MenuText, _tcslen(MenuText));
EndPaint(hWnd, &ps);
break;
}
case WM_MOUSEMOVE: {
int xPos = GET_X_LPARAM(lParam);
int yPos = GET_Y_LPARAM(lParam);
if (xPos >= 10 && xPos <= 130 && yPos >= 10 && yPos <= 45) {
if (!mouseHover) {
mouseHover = true;
RedrawWindow(hWnd, NULL, NULL,
RDW_INVALIDATE | RDW_INTERNALPAINT);
}
}
else {
if (mouseHover) {
mouseHover = false;
RedrawWindow(hWnd, NULL, NULL,
RDW_INVALIDATE | RDW_INTERNALPAINT);
}
}
break;
}
case WM_RBUTTONUP: {
int xPos = GET_X_LPARAM(lParam);
int yPos = GET_Y_LPARAM(lParam);
POINT p = { xPos, yPos };
ClientToScreen(hWnd, &p);
menuUp = true;
SetWindowLongPtr(hWMenu, GWLP_HWNDPARENT, (LONG)hWnd);
SetWindowPos(hWMenu,
(HWND_TOPMOST),
p.x, p.y, 0, 0,
SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOOWNERZORDER);
break;
}
case WM_NCACTIVATE:
//paint the window as active when custom menu starts
if (!wParam && menuUp)
return DefWindowProc(hWnd, WM_NCACTIVATE, TRUE, lParam);
return DefWindowProc(hWnd, message, wParam, lParam);
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
) {
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = _T("MenuHolder");
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProcMenu;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = _T("MenuWindow");
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
static TCHAR szTitle[] = _T("test");
HWND hWnd = CreateWindow(
_T("MenuHolder"),
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
500, 100,
NULL,
NULL,
hInstance,
NULL
);
hWndMain = hWnd;
hWMenu = CreateWindowEx(
WS_EX_TOOLWINDOW | WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE,
_T("MenuWindow"),
szTitle,
WS_POPUP | WS_BORDER,
0, 0,
100, 500,
hWnd,
NULL,
hInstance,
NULL
);
if (!hWnd || !hWMenu)
{
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
ShowWindow(hWnd,
nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
bool skip = false;
if (!skip) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment