简体   繁体   English

使用SetWindowLong命令更改树视图的方向时,右键单击(弹出菜单)不起作用

[英]Right click(popup menu) does not work when change diretion of treeview with SetWindowLong Command

When I use SetWindowLong command to change direction of treeview, popupmenu on its node dose not show. 当我使用SetWindowLong命令更改树视图的方向时,其节点上的popupmenu不会显示。 Full Code is here : 完整代码在这里:

Procedure SetWinControlBiDi(Control: TTreeView);
 var
  ExStyle: Longint;
 begin

  ExStyle := GetWindowLong(Control.Handle, GWL_EXSTYLE);

  SetWindowLong(Control.Handle, GWL_EXSTYLE, ExStyle or WS_EX_RTLREADING or WS_EX_RIGHT or WS_EX_LAYOUTRTL or WS_EX_NOINHERITLAYOUT );

 end;


procedure TMainForm.FormShow(Sender: TObject);
 begin

  SetWinControlBiDi(TreeView1);

 end;

The standard way to do this is to use the Delphi BiDiMode property. 执行此操作的标准方法是使用Delphi BiDiMode属性。 It's best to do it this way so that the VCL is aware that you want right-to-left. 最好这样做,以便VCL知道你想要从右到左。 You need to change the BiDiMode property on the popup menu too. 您还需要在弹出菜单上更改BiDiMode属性。

Now, the correct way to do this is not to change the properties on the individual components. 现在,正确的方法是不更改单个组件的属性。 Doing it that way is laborious and very error prone. 这样做是费力的,非常容易出错。 Set Application.BiDiMode somewhere in your application's initialization and the change will propagate through to all your components. 在应用程序初始化的某处设置Application.BiDiMode ,更改将传播到所有组件。

For example you can make the change in your application's .dpr file: 例如,您可以在应用程序的.dpr文件中进行更改:

Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.BiDiMode := bdRightToLeft;
Application.CreateForm(TMainForm, MainForm);
Application.Run;

You need to make sure that you have not modified any component's BiDiMode or ParentBiDiMode in any .dfm file. 您需要确保没有在任何.dfm文件中修改任何组件的BiDiModeParentBiDiMode If you have simply remove those lines from your .dfm file and that will allow the single application wide Application.BiDiMode setting to control everything. 如果您只是从.dfm文件中删除这些行,那么将允许单个应用程序范围的Application.BiDiMode设置来控制所有内容。


Your approach of setting GWL_EXSTYLE is problematic. 您设置GWL_EXSTYLE方法存在问题。 The VCL is in control of that setting and if you do need to change it, doing so in TForm.OnShow will lead to strange bugs. VCL控制着该设置,如果你确实需要更改它,在TForm.OnShow这样做会导致奇怪的错误。 Sometimes windows need to be re-created and when this happens your code to set GWL_EXSTYLE will not run and your tree view will revert to left-to-right. 有时需要重新创建窗口,当发生这种情况时,设置GWL_EXSTYLE的代码将不会运行,树视图将从左向右恢复。 If you do need to modify the window styles then you need to override TWinControl.CreateParams for the component. 如果确实需要修改窗口样式,则需要覆盖组件的TWinControl.CreateParams However, in this case the VCL has direct support for BiDi and that is the best solution. 但是,在这种情况下,VCL直接支持BiDi,这是最好的解决方案。

This is an alternative solution to show TPopupMenu In this case 这是显示TPopupMenu的替代解决方案。在这种情况下

1- Use OnMouseDown Event 1-使用OnMouseDown事件

2- Write this code to show a TPopupMenu when you click the right mouse button 2-编写此代码以在单击鼠标右键时显示​​TPopupMenu

 var
  pt : TPoint;

  begin
  pt := Mouse.CursorPos;

   if Button = mbRight then
        APopupMenu.Popup(pt.X, pt.Y);

Good luck ! 祝好运 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM