WPF使用鼠标滚轮和Ctrl实现缩放和放大功能
效果如下:
WPF小项目合集
界面代码如下:
<Window x:Class="缩放.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:缩放"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" PreviewMouseWheel="Window_PreviewMouseWheel">
<Grid>
<Canvas Name="view" Width="800">
<!--<Rectangle Width="200" Height="100" Stroke="Black"
StrokeThickness="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>-->
<TextBox Width="{Binding Path=ActualWidth, ElementName=view}" Text="第一条
为了加强对建筑活动的监督管理,维护建筑市场秩序,保证建筑工程的质量和安全,促进建筑业健康发展,制定本法。
第二章 建 筑 许 可
第七条
建筑工程开工前,建设单位应当按照国家有关规定向工程所在地县级以上人民政府建设行政主管部门申请领取施工许可证;但是,国务院建设行政主管部门确定的限额以下的小型工程除外。 按照国务院规定的权限和程序批准开工报告的建筑工程,不再领取施工许可证。
第八条
申请理单位,按照其拥有的注册资本、专业技术人员、技术装备和已完成的建筑工程业绩等资质条件,划分为不同的资质等级,经资质审查合格,取得相应等级的资质证书后,方可在其资质等级许可的范围内从事建筑活动。
第十四条
从事建筑活中标的承包单位。建筑工程实行直接发包的,发包单位应当将建筑工程发包给具有相应资质条件的承包单位。
第二十三条证书,并在其资质等级许可的业务范围内承揽工程。 禁止建筑施工企业超越本企业资质等级许可的业务范围或者以任何形式用其他建筑施工企业的名义承揽工程。禁止建筑施工企业以任何形式允许其他单位或者个人使用本企业的资质证书、营业执照,以本企业的名义承揽工程。
第二十七条
大型建筑工程或者结构复杂的建筑工程,可以由两个以上的承包单位联合共同承包。共同承包的各方对承包合同的履行承担连带责任。 两个以上不同资质等级的单位实行联合共同承包的,应当按照资质等级低的单位的业务许可范围承揽工程。"
FontSize="14" TextWrapping="Wrap" Foreground="Blue" Padding="2"/>
</Canvas>
</Grid>
</Window>
后台代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace 缩放
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
double scale;
public MainWindow()
{
InitializeComponent();
scale = 1;
}
private void Window_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
if(Keyboard.IsKeyDown(Key.LeftCtrl))
{
if ( e.Delta < 0)
{
scale -= 0.1;
}
else
{
scale += 0.1;
}
// scale += (double)e.Delta / 35000;
ScaleTransform transfrom = new ScaleTransform();
transfrom.ScaleX = transfrom.ScaleY = scale;
this.view.RenderTransform = transfrom;
}
}
}
}