Witam. Dzisiejszej nocy wygrzebałem informacje na temat menu kontekstowego i się nim nieco pobawiłem. Rezultatem mojej zabawy jest coś takiego:
main.cpp:
#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK NewWndProc (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "CodeBlocksWindowsApp";
HWND tekst1;
HINSTANCE *hInstance;
WNDPROC StaraProcedura;
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
hInstance = &hThisInstance;
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Code::Blocks Template Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
tekst1 = CreateWindowEx( 0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL, 50, 50, 150, 20,hwnd, NULL, hThisInstance, NULL );
StaraProcedura = (WNDPROC) SetWindowLong( tekst1, GWL_WNDPROC,( LONG ) NewWndProc );
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK NewWndProc( HWND hwnd, UINT mesg, WPARAM wParam, LPARAM lParam )
{
switch( mesg )
{
case WM_RBUTTONDOWN:
{
WINDOWINFO Info;
Info.cbSize = sizeof(WINDOWINFO);
GetWindowInfo(hwnd, &Info);
int x = 0; // NIE MAM POJECIA CO UCZYNIC ;)
HMENU menu = LoadMenu(*hInstance, MAKEINTRESOURCE(200));
HMENU sub = GetSubMenu(menu, 0);
TrackPopupMenu(sub, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_NONOTIFY, Info.rcWindow.left+x, Info.rcWindow.top+20, 0, hwnd, NULL);
DestroyMenu(menu);
break;
}
}
return CallWindowProc( StaraProcedura, hwnd, mesg, wParam, lParam );
}
resource.rc
200 MENU
{
POPUP "1"
{
MENUITEM "1.1", 201
POPUP "1.2"
{
MENUITEM "1.2.1", 202
MENUITEM "1.2.2", 203
}
}
}
Po kliknięciu prawym przyciskiem myszy na Edita, za pomocą Subclassingu wywołuję menu kontekstowe. W procedurze dialogowej NewWndProc() jest taka zmienna "x".
Moim celem jest to, aby menu te było przesunięte o jakąś wartość w prawo (Przesunięcie określa właśnie ta zmienna). Chcę, aby wartość była zależna od tego, ile jest tekstu w kontrolce (czyli początek menu był pod ostatnią literką).
Mój koncept na to jest taki, że w tablicy zapiszę sobie długości każdego znaku w pikselach (czcionka się nie zmienia), i po każdym kliknięciu będę odczytywał tekst i znak po znaku dodawał ilość pikseli. Nie wiem jednak czy to jest dobry sposób, gdyż na różnych komputerach różnie mi się tekst wyświetlał i prawdę mówiąc pojęcia nie mam ile pikseli ma każdy znak ;)
Czy jest jakiś inny sposób na określenie wartości zmiennej "x" ??
Edit:
Po zmienieniu czcionki na:
HFONT hFont=CreateFont(0,0,0,0,0,0,0,0,0,0,0,0,0,"Lucida Console");
SendMessage(tekst1,WM_SETFONT,(WPARAM)hFont,1);
wzór na zmienną x:
int x = GetWindowTextLength(tekst1) * 11;
daje pożądane wyniki. Jednak ja chcę używać czcionki Arial. Tam znaki mają różną długość :( help