如何在 VB6 中将弹出菜单放置在按钮正下方

发布于 2024-07-26 05:33:59 字数 377 浏览 6 评论 0原文

我有一个 VB6 应用程序。 有一个主要形式。 它包含一个用户控件。 该用户控件有一个按钮控件。 当我单击按钮时,我希望弹出一个菜单,以便菜单的左上角位于按钮左下角的正下方。

如果我这样做:

frm.PopupMenu mnuBlah

菜单出现在鼠标所在的位置。

如果我尝试提供坐标,

frm.PopupMenu mnuBlah, btn.Left, btn.Top + btn.Height

数学结果会完全错误,并且菜单显示的内容会偏离目标。

我不太确定 PopupMenu 调用需要什么类型的坐标以及如何计算按钮下方的位置。

I have a VB6 app. There is a main form. It houses a user control. This user control has a button control. When I click the button, I want a menu to pop up so that the top left of the menu is right underneath the bottom left of the button.

if I do:

frm.PopupMenu mnuBlah

the menu comes up where the mouse is.

if I try to provide coordinates

frm.PopupMenu mnuBlah, btn.Left, btn.Top + btn.Height

the math comes out totally wrong and the menu is displayed way off target.

I am not quite sure what type of coordinates the PopupMenu call requires and how to calculate the position underneath the button.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很快妥协 2024-08-02 05:33:59

PopupMenu 需要弹出菜单的窗体的 ScaleMode 中的 X 和 Y 坐标。 在这种情况下,这可能意味着用户控件。

但这实际上取决于您处理 MouseDown 事件的位置。 (您不会通过 Click 事件获得坐标。)如果您的事件处理程序位于用户控件本身中,那么使用提供的坐标应该不会有任何问题。 但是,您发布的代码缺少 Flags 参数(应为 vbPopupMenuLeftAlign),这可以解释为什么您的坐标数学不起作用。 另外,您必须将菜单定义为用户控件本身的属性。

另一方面,如果您从用户控件中引发 MouseDown 事件,并在包含窗体中处理它,则弹出菜单必须是包含窗体的属性。 在这种情况下,您可以忽略X和Y参数,但您必须自己进行坐标计算。 按钮的左边缘相对于主窗体,将是用户控件的左边缘控件内按钮的偏移量之和。 类似的数学运算可以计算按钮的底部边缘。 换句话说:

Private Sub UserControl1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    PopupMenu mnuPopup, vbPopupMenuLeftAlign, _
        UserControl1.Left + UserControl1.Button.Left, _
        UserControl1.Top + UserControl1.Button.Top + UserControl1.Button.Height
End Sub

请注意,这还要求您将按钮 (Button) 公开为用户控件的属性,以便您可以访问其 Left、Top 和 Height 属性。 这很简单:

Public Property Get Button() As CommandButton
    Set Button = Command1
End Property

引发事件也很简单; 只需放在

Public Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

用户控件的顶部,然后

RaiseEvent MouseDown(Button, Shift, X, Y)

在用户控件中按钮的 MouseDown 事件中使用:

最后,要在主窗体中连接事件处理程序,请使用代码编辑窗口顶部的两个下拉列表。 从左侧选择用户控件,然后从右侧选择 MouseDown 事件。 这将插入事件处理程序,您可以使用上面的代码填充该事件处理程序。

至于哪种方式更好,在用户控件本身内部处理菜单,并在选择一个菜单时引发有意义的事件可以将事情分开。 用户控件处理 UI 工作,让主应用程序处理响应用户选择所需执行的任何操作,而无需担心菜单的放置。

所以你实际上不会引发 MouseDown 事件; 相反,您应该引发 OptionOneSelected、OptionTwoSelected 或 OptionThreeSelected 等事件。 (当然,更有意义的名称会更好。)

无论如何,我希望这还不算太晚......

PopupMenu wants X and Y coordinates in the ScaleMode of the form on which the menu is being popped up. In this case, that probably means the user control.

But it really depends on where you're handling the MouseDown event. (You don't get coordinates with the Click event.) If your event handler is in the user control itself, then you shouldn't have any problem using the supplied coordinates. However, the code you posted is missing the Flags parameter (should be vbPopupMenuLeftAlign), which could explain why your coordinate math isn't working. Plus, you have to define the menu as a property of the user control itself.

On the other hand, if you raise the MouseDown event out of the user control, and handle it in the containing form, the popup menu has to be a property of the containing form. In this case, you can ignore the X and Y parameters, but you have to do coordinate calculations yourself. The left edge of the button, relative to the main form, will be the sum of user control's left edge and the offset of the button within the control. Similar math will work to calculate the bottom edge of the button. In other words:

Private Sub UserControl1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    PopupMenu mnuPopup, vbPopupMenuLeftAlign, _
        UserControl1.Left + UserControl1.Button.Left, _
        UserControl1.Top + UserControl1.Button.Top + UserControl1.Button.Height
End Sub

Note that this will require that you also expose the button (Button) as a property of the user control so that you can access its Left, Top, and Height properties. That's easy enough:

Public Property Get Button() As CommandButton
    Set Button = Command1
End Property

Raising the event is simple enough as well; just put

Public Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

at the top of the user control, then use this:

RaiseEvent MouseDown(Button, Shift, X, Y)

in the MouseDown event of the button in the user control.

Finally, to wire up the event handler in the main form, use the two dropdowns at the top of the code editing window. Select the user control from the one on the left, then select the MouseDown event from the one on the right. This will insert the event handler, which you fill in with the code above.

As far as which way is better, handling the menu inside the user control itself, and raising meaningful events when one is selected keeps things separated. The user control handles the UI work, leaving the main application to handle whatever needs to be done in response to the user's selection, without worrying about placement of the menu.

So you wouldn't actually be raising the MouseDown event; rather you'd raise events like OptionOneSelected, OptionTwoSelected, or OptionThreeSelected. (More meaningful names would be much better, of course.)

Anyway, I hope this isn't too late to be helpful...

从来不烧饼 2024-08-02 05:33:59

您需要将 vbPopupMenuLeftAlign 标志传递给 PopupMenu 方法。 请注意,这与 PopupMenu 方法的文档相矛盾(微软真的可能犯了一个错误吗?),但它确实有效。

frm.PopupMenu mnuBlah, vbPopupMenuLeftAlign, btn.Left, btn.Top + btn.Height

You need to pass the vbPopupMenuLeftAlign flag to the PopupMenu method. Note, this contradicts the documentation on the PopupMenu method (could Microsoft actually have made a mistake?), but it works.

frm.PopupMenu mnuBlah, vbPopupMenuLeftAlign, btn.Left, btn.Top + btn.Height
帝王念 2024-08-02 05:33:59

您需要包含父容器的高度和左侧值(这通常是表单,但也可以是框架或其他容器)。

我想你正在寻找这样的东西:

btn.Parent.PopupMenu mnuBlah, vbPopupMenuLeftAlign , btn.Left, btn.Top + btn.Height

You need to include the height and left values for the parent container (this is usually the form, but could also be a frame, or some other container).

I think you're looking for something like this:

btn.Parent.PopupMenu mnuBlah, vbPopupMenuLeftAlign , btn.Left, btn.Top + btn.Height
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文