gpt4 book ai didi

c# - KeyDown、KeyUp 和 KeyPressed 不会在托管 WPF 控件的 WinForms 应用程序上触发

转载 作者:行者123 更新时间:2023-12-04 02:23:56 42 4
gpt4 key购买 nike

我在 C# 中针对 .NET 4.5 做了一个测试 WinForms APP。该应用程序使用 System.Windows.Controls.Canvas 在黑色背景上绘制一个白色矩形。

我遇到了与键盘相关的事件。

完整源码如下

using System;
using System.Drawing;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using Brushes = System.Windows.Media.Brushes;
using Panel = System.Windows.Forms.Panel;
using Rectangle = System.Windows.Shapes.Rectangle;
using UserControl = System.Windows.Controls.UserControl;

namespace WpfHostApp
{
internal static class Program
{
/// <summary>
/// Entry point.
/// </summary>
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

var panel = new Panel { Dock = DockStyle.Fill };

var form = new Form {Size = new Size(800, 600), Text = "WPF Host Form"};
form.Controls.Add(panel);

var rectangle = new Rectangle {Fill = Brushes.White, Width = 100, Height = 100};

var canvas = new Canvas {Background = Brushes.Black};
canvas.Children.Add(rectangle);

var userControl = new UserControl {Content = canvas};

var elementHost = new ElementHost {Child = userControl, Dock = DockStyle.Fill};

panel.Controls.Add(elementHost);

form.KeyDown += (sender, args) => MessageBox.Show("Key pressed!");

Application.Run(form);
}
}
}

form.KeyDown 事件不会触发(实际上 form.Key* 不会触发),但如果我注释行 panel.Controls .Add(elementHost); 事件触发但显然没有绘制 Canvas 。

我看不出上面的示例代码中有什么会导致这个问题。罪魁祸首是什么?

编辑:

我已经添加了一个用于订阅 KeyDown 的递归函数...没有结果

Main() 中,在 form.KeyDown 之后:

foreach (Control control in form.Controls)
{
SuscribeKeyDown(control);
}

SuscribeKeyDown(Control 控件) 函数:

private static void SuscribeKeyDown(Control control)
{
control.KeyDown += (sender, args) => MessageBox.Show("Key pressed!");
foreach (Control controlNested in control.Controls)
{
SuscribeKeyDown(controlNested);
}
}

最佳答案

canvas.Focusable 设置为 true 并捕获 Canvas 的 KeyDown 事件:

[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

var panel = new System.Windows.Forms.Panel { Dock = DockStyle.Fill };

var form = new Form { Size = new Size(800, 600), Text = "WPF Host Form" };
form.Controls.Add(panel);

var rectangle = new System.Windows.Shapes.Rectangle { Fill = System.Windows.Media.Brushes.White, Width = 100, Height = 100 };

var canvas = new Canvas { Background = System.Windows.Media.Brushes.Black,
Focusable = true};
canvas.Children.Add(rectangle);

var userControl = new System.Windows.Controls.UserControl { Content = canvas };

var elementHost = new ElementHost { Child = userControl, Dock = DockStyle.Fill };

panel.Controls.Add(elementHost);

// form.KeyDown += (sender, args) => MessageBox.Show("Key pressed!");
canvas.KeyDown += (sender, args) => MessageBox.Show("Key pressed!");

Application.Run(form);
}

关于c# - KeyDown、KeyUp 和 KeyPressed 不会在托管 WPF 控件的 WinForms 应用程序上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24640018/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com