Вот обешанный пример. Программа сделана на WinAPI
//---------------------------------------------------------------------------
#include <windows.h>
#include <windowsx.h>
//---------------------------------------------------------------------------
static HWND MainWindow;
char szAppName[] = "LibraryApp";
char szTitle[] = "Library Sample Application";
HMENU menu=NULL; //Хэндл контекстной менюшки
HBRUSH brush[2];
int setBrush=0;
bool Register(HINSTANCE);
HWND Create(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void OnDestroy(HWND hwnd);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE hInst,
HINSTANCE hPrevInst,
LPSTR CmdParam,
int nCmdShow)
{
MSG Msg;
brush[0]=CreateSolidBrush(RGB(0,0,0));
brush[1]=CreateSolidBrush(RGB(255,255,192));
if (!Register(hInst))
return false;
MainWindow=Create(hInst,nCmdShow);
if (MainWindow==0)
return false;
while (GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
DeleteObject(brush[0]);
DeleteObject(brush[1]);
return Msg.wParam;
}
//---------------------------------------------------------------------------
bool Register(HINSTANCE hInst)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = brush[setBrush];
//Меню не связано с окном при регистрации
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
return (RegisterClass(&wc) !=0);
}
HWND Create(HINSTANCE hInst, int nCmdShow)
{
// Также при создании окна
HWND hWindow = CreateWindow(szAppName, szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInst, NULL);
if (hWindow != NULL)
{
ShowWindow(hWindow, nCmdShow);
UpdateWindow(hWindow);
}
return hWindow;
}
#define ID_CLOSE 100
#define ID_CHANGE 101
//Создаем контекстное меню
void InitMenu(void)
{
if (menu == NULL)
{
menu=CreatePopupMenu();
AppendMenu(menu, MF_STRING, ID_CHANGE, "Change Color");
AppendMenu(menu, MF_SEPARATOR, 0, NULL);
AppendMenu(menu, MF_STRING, ID_CLOSE, "Exit");
}
}
//Вызываем его при нажатии правой кнопки мыши
void OnRButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
{
InitMenu();
POINT po;
po.x=x;
po.y=y;
ClientToScreen(hwnd, &po);
TrackPopupMenu(menu,TPM_RIGHTBUTTON, po.x, po.y, 0, hwnd, NULL);
}
void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch (id)
{
case ID_CLOSE:
PostMessage(hwnd, WM_CLOSE, 0,0);
break;
case ID_CHANGE:
setBrush=1-setBrush;
SetClassLong(hwnd, GCL_HBRBACKGROUND, (LONG) brush[setBrush]);
InvalidateRect(hwnd,NULL, true);
break;
};
}
LRESULT CALLBACK WndProc(HWND hWindow, UINT Message,
WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
HANDLE_MSG(hWindow, WM_DESTROY, OnDestroy);
HANDLE_MSG(hWindow, WM_RBUTTONDOWN, OnRButtonDown);
HANDLE_MSG(hWindow, WM_COMMAND, OnCommand);
default:
return DefWindowProc(hWindow, Message, wParam, lParam);
}
}
void OnDestroy(HWND hwnd)
{
if (menu != NULL) DestroyMenu(menu);
menu=NULL;
PostQuitMessage(0);
}
//---------------------------------------------------------------------------
Так что, кроме хука, я не вижу способа ловить отрисовку контекстного меню из чужого окна.