Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Windows.Forms.MenuItem is not working #3

Open
Inmobilis opened this issue Dec 7, 2022 · 3 comments
Open

System.Windows.Forms.MenuItem is not working #3

Inmobilis opened this issue Dec 7, 2022 · 3 comments

Comments

@Inmobilis
Copy link

internal class Command : WeakReference

Should use "System.Windows.Forms.Command" in assembly "System.Windows.Forms".

Simple code:

internal class Command : IDisposable
{
    private static readonly Type CommandType = typeof(Control).Assembly.GetType(typeof(Command).FullName);
    private static readonly Reflection.PropertyInfo CommandIDProperty = CommandType.GetProperty(nameof(ID), typeof(Int32));
    private static readonly Reflection.MethodInfo CommandDisposeMethod = CommandType.GetMethod(nameof(Dispose), Type.EmptyTypes);

    private readonly Object cmd;

    public Command(ICommandExecutor target) => this.cmd = Activator.CreateInstance(CommandType, target);

    public Int32 ID => (Int32)CommandIDProperty.GetValue(this.cmd);

    public void Dispose() => CommandDisposeMethod.Invoke(this.cmd, null);
}
@rickbrew
Copy link
Contributor

rickbrew commented Mar 6, 2023

System.Windows.Forms.Command in System.Windows.Forms is internal and cannot be used by this assembly.

I've only gotten this package as far as compiling and working for Paint.NET's scenarios -- that is, getting specific old/legacy plugins to work.

If something is broken for your project, please describe your scenario and submit a pull request with a fix.

@Inmobilis
Copy link
Author

Yes, System.Windows.Forms.Command in System.Windows.Forms is internal, but we must use it, because Control.WmCommand use it to dispatch WM_COMMAND message.

For example,

var form = new Form();
form.MouseClick +=
    (sender, e) =>
        new ContextMenu(new[] { new MenuItem("Test", (sender, e) => MessageBox.Show(((MenuItem)sender).Text)) })
            .Show((Control)sender, e.Location);
form.Show();

click form, and then click the "Test" menuitem, nothing happen, message box not shown.

#5

@Unknown025
Copy link

Can confirm, the Click event on MenuItem does not work otherwise, making context menus unusable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants