Форум "Delphi"
Паскаль, Делфи
Как выбрать popup-меню для грида в режиме выполнения в зависимости от условия?На форме лежит грид и два попап-меню. В зависимости от условия должно выпадать либо одно меню, либо другое. Я так понимаю нужно перехватывать определенное событие, назначать ему свой обработчик и втискивать в него часть с условием и назначением нужного меню. Если так, то какое событие нужно перехватывать? Пробовал через procedure WMInitMenuPopup(var Message: TWMInitMenuPopup); message WM_INITMENUPOPUP; не получилось. Вобщем, буду благодарен за подсказку или пример.
|
|
#1 Phoroon © 12.12.06 23:04:11
Возьмем другое сообщение, у которого есть координаты. type ... procedure WMContextMenu(var Message: TWMCONTEXTMENU); message WM_CONTEXTMENU; ... implementation procedure TForm1.WMContextMenu(var Message: TWMCONTEXTMENU); begin if Message.XPos < 500 then PopupMenu1.Popup(Message.XPos, Message.YPos); if Message.XPos > 500 then PopupMenu2.Popup(Message.XPos, Message.YPos); end; Примечание: 1 В компоненте меню указывать не надо. 2 Координаты указываются относительно экрана. |
|
#2 Axis_of_Evil © 13.12.06 00:32:54
procedure TForm1.StringGrid1ContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean); begin if CheckBox1.Checked then PopupMenu1.Popup(MousePos.X, MousePos.Y) else PopupMenu2.Popup(MousePos.X, MousePos.Y); Handled := True; end; координаты надо подкорректировать (из оконных в глобальные) The OnContextPopup handler is called when the user uses the mouse or keyboard to request a popup menu. The OnContextPopup event is generated by a WM_CONTEXTMENU message, which is itself generated by the user clicking the right mouse button or by pressing SHIFT+F10 or the Applications key. This event is especially useful when the control does not have an associated popup menu (the PopupMenu property is not set) or if the AutoPopup property of the control’s associated popup menu is false. However, the OnContextPopup can also be used to override the automatic context menu that appears when the control has an associated popup menu with an AutoPopup property of true. In this last case, if the event handler displays its own menu, it should set the Handled parameter to true to suppress the default context menu. The handler’s MousePos parameter indicates the position of the mouse, in client coordinates.. If the event was not generated by a mouse click, MousePos is (-1,-1). /help/ |
|
а если три меню, одно из них - пустое. в зависимости от условия в OnPopUp модифицировать пустое меню по образу и подобию нужного. и координаты пересчитывать не нужно. или даже не модифицировать, а вызывать нужное, отталкиваясь от мышиных координат. или... событи ДбГрида есть мышиное, за него и цепляцца. токо это у меня не стандартный грид, но не важно, переделаешь procedure TmmDBGrid.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var Cell: TGridCoord; field: TField; p: TPoint; begin inherited MouseDown(Button, Shift, x, y); if (csDesigning in ComponentState) then Exit; if (ssRight in Shift) then begin Cell:= MouseCoord(x, y); if Cell.Y = 0 then begin p.x:= 0; p.y:= 0; p:= ClientToScreen(p); MenuColumn.Popup(p.X + X, p.Y + Y); end; end; end; кусок кода выдран и исправлен по живому, потому мугут быть ляпы. у меня это прикручено к загол |
|
к заголовкам колонок Грида. |
|
упс. вот этого - не нужно > inherited MouseDown(Button, Shift, x, y); if (csDesigning > in ComponentState) then Exit; это от компонента моего осталось |
|
Всем спасибо. Получилось так как хотел. |
Написать ответ |
|
