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;
}
}
}
}