我正在尝试在窗体内使用画布并缩放和平移该画布首先我放置元素主机然后将画布放在其中然后将画框放在画布中然后尝试缩放画布我尝试了各种方法但是任何控件的事件都没有执行我还写了所有鼠标滚轮事件但没有人执行所以请建议我下面的解决方案是我的代码添加控件和鼠标滚轮事件
elementHost1.Height = picVarify.Height;
elementHost1.Width = picVarify.Width;
elementHost1.Location = picVarify.Location;
touchcanvas = new System.Windows.Controls.Canvas();
WindowsFormsHost hst = new WindowsFormsHost();
hst.Name = "Host";
hst.Child = picVarify;
hst.Height = picVarify.Height;
hst.Width = picVarify.Width;
touchcanvas.Height = picVarify.Height;
touchcanvas.Width = picVarify.Width;
touchcanvas.Children.Add(hst);
zm = new ZoomAndPan.ZoomAndPanControl();
zm.Name = "zm";
zm.Content = touchcanvas;
zm.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(zoomAndPanControl_MouseWheel);
elementHost1.Child = zm;
touchcanvas.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(touchcanvas_MouseWheel);
hst.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(hst_MouseWheel);
picVarify.MouseWheel += new MouseEventHandler(picverify_MouseWheel);
答案 0 :(得分:0)
这里的代码对我来说很好。我在画布中添加了画布并设置了两个画布的背景属性。
XAML
<Window x:Class="WpfStack.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="rootGrid" Margin="10,0,0.4,3.8">
<Canvas Name="BaseCanvas" Background="AliceBlue" Margin="10,0,0,10">
</Canvas>
</Grid>
代码
public MainWindow()
{
InitializeComponent();
Canvas touchcanvas = new System.Windows.Controls.Canvas();
touchcanvas.Height =100;
touchcanvas.Width = 100;
touchcanvas.Background = Brushes.Transparent;
touchcanvas.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(touchcanvas_MouseWheel);
BaseCanvas.Children.Add(touchcanvas);
BaseCanvas.Background = Brushes.Transparent;
}
public void touchcanvas_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs args)
{
args.Handled = true;
}