Download C-Tutorial

Transcript
224
Windows programming with lcc-win32
// Now ask the tree view control if there is an item at this position
hti = TreeView_HitTest(hwndTree,&testInfo);
// If nothing is found stop.
if (hti == (HTREEITEM)0) break;
// There is something. Show the context menu using the information returned by
// the control in the item handle hti.
hnewMenu = CreateContextMenu(hti);
// If the creation of the menu did not work stop.
if (hnewMenu == (HMENU)0) break;
menuItem = TrackPopupMenu(hnewMenu,
TPM_RIGHTBUTTON|TPM_TOPALIGN|TPM_RETURNCMD,
pt1.x,pt1.y,
0,hwnd,NULL);
DestroyMenu(hnewMenu);
// Here we would do some action indicated by the menuItem code
break;
}
This code begins with a cast of the lParam message parameter into a pointer to a NMHDR
structure. Then it examines the passed notification code. If it is a right click it fills a
TV_HITTESTINFO structure with the coordinates of the mouse, and translates those into the
coordinates of the tree control that this code supposes in a global variable “hwndTree”. The
control should return either NULL, meaning there is no item at that position, or the handle of
an item that was right-clicked with the mouse. If there was an item, a context menu is created,
shown, and the result is an integer indicating which menu item was chosen, if any.
This is one of the many ways you can interact with the window system. Each control will have
its own interface since they are all different and perform quite different actions.
You can see the above code in action when you click with the right button in an item of the
project workspace window in Wedit.
2.9.1
Using controls without a dialog box
You use the CreateWindow API with a predefined window class. You pass it as the parentwindow parameter the handle of the window where you want to put the control on. For
instance if you want to create somewhere in your window an edit field, you would write:
hEditWnd = CreateWindow("EDIT", // window class is “edit”
NULL,
WS_CHILD | WS_VISIBLE |
ES_MULTILINE |
WS_VSCROLL | WS_HSCROLL |
ES_AUTOHSCROLL | ES_AUTOVSCROLL,
0,
0,
(Rect.right - Rect.left), //Assume a rectangle structure
(Rect.bottom - Rect.top), // that contains the coordinates
hParentWnd,
(HMENU) 14376, // Window identifier
hInst, // Application instance
NULL);
The controls “edit”, “listbox” and others are already predefined when windows starts. You can
use them at any time. Some others however are NOT like that, and you need to call the API
InitCommonControlsEx() before you can use them.