xaml 文件
<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="0" ResizeBorderThickness="20"/>
</WindowChrome.WindowChrome>
<Viewbox Margin="0,0,0,0">
<Grid Width="450" Height="70">
<Border Margin="0" BorderThickness="1" BorderBrush="Gray" CornerRadius="15">
<Border.Effect>
<DropShadowEffect Direction="0" ShadowDepth="0" BlurRadius="15" Opacity="0.5" Color="#FF5B5B5B"></DropShadowEffect>
</Border.Effect>
</Border>
<TextBlock x:Name="SpeedModeName" Foreground="White" Text="{Binding SpeedMode}" TextAlignment="Center" FontSize ="9" TextWrapping="Wrap" Height="44" Margin="10,10,425,10"/>
<Canvas Margin="30,0,365,0">
<TextBlock x:Name="ForAndBacControlName" Foreground="White" Width="55" Text="进退控制" TextAlignment="Center" FontSize ="8" TextWrapping="Wrap" Height="11" Canvas.Top="3" />
<Image x:Name="ForAndBacControlImage" Source="/Res/MainTerminalDisplay/Speed.png" Width="55" Height="44" Panel.ZIndex="0" Canvas.Top="15"/>
<TextBlock x:Name="SpeedControlNameS" Text="" Canvas.Top="16" Canvas.Left="10" Panel.ZIndex="1" TextAlignment="Center" VerticalAlignment="Center" Width="35" RenderTransformOrigin="0.639,0.686" Height="20"></TextBlock>
<TextBlock x:Name="SpeedControlNameN" Text="" Canvas.Top="36" Canvas.Left="10" Panel.ZIndex="1" FontSize ="9" TextAlignment="Center" VerticalAlignment="Center" Width="37" RenderTransformOrigin="0.502,1.201" Height="10"></TextBlock>
</Canvas>
<Canvas Margin="90,0,305,0">
<TextBlock x:Name="RotationControlName" Foreground="White" Width="55" Text="旋转控制" TextAlignment="Center" FontSize ="8" TextWrapping="Wrap" Height="11" Canvas.Top="3" />
<Image x:Name="RotationImage" Source="/Res/MainTerminalDisplay/Speed.png" Width="55" Height="44" Panel.ZIndex="0" Canvas.Top="15"/>
<TextBlock x:Name="RotationSName" Text="" Canvas.Top="16" Canvas.Left="10" Panel.ZIndex="1" TextAlignment="Center" VerticalAlignment="Center" Width="34"></TextBlock>
<TextBlock x:Name="RotationNName" Text="" Canvas.Top="36" Canvas.Left="10" Panel.ZIndex="1" FontSize ="9" TextAlignment="Center" VerticalAlignment="Center" Width="37"></TextBlock>
</Canvas>
<Canvas Margin="150,0,245,0">
<Image x:Name="CatheterControlImage" Source="/Res/MainTerminalDisplay/ActionBars.png" Width="55" Height="43" Panel.ZIndex="0" Canvas.Top="11"/>
<Label Height="63" FontSize="9" x:Name="LeftOperatorName" VerticalAlignment="Center" BorderThickness="1"
Content="" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="55"/>
</Canvas>
</Grid>
</Viewbox>
cs 文件
//最后的宽度(Last Width)
private double LastWidth ;
//最后的高度(Last Height)
private double LastHeight ;
private static float AspectRatio = 450.0f / 70.0f;
/// </summary>
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource source = HwndSource.FromVisual(this) as HwndSource;
if (source != null)
{
source.AddHook(new HwndSourceHook(WinProc));
}
}
public const Int32 WM_EXITSIZEMOVE = 0x0232;
/// <summary>
/// 重载窗口消息处理函数
/// (Overload window message processing function)
/// </summary>
private IntPtr WinProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, ref Boolean handled)
{
IntPtr result = IntPtr.Zero;
double width = SystemParameters.PrimaryScreenWidth;
double height = SystemParameters.PrimaryScreenHeight;
if (this.Width > width)
{
this.Width = width;
this.Height = this.Width / AspectRatio;
}
if (this.Height > height)
{
this.Height = height;
this.Width = this.Height * AspectRatio;
}
switch (msg)
{
//处理窗口消息 (Handle window messages)
case WM_EXITSIZEMOVE:
{
//上下拖拉窗口 (Drag window vertically)
if (this.Height != LastHeight)
{
this.Width = this.Height * AspectRatio;
}
// 左右拖拉窗口 (Drag window horizontally)
else if (this.Width != LastWidth)
{
this.Height = this.Width / AspectRatio;
}
LastWidth = (int)this.Width;
LastHeight = (int)this.Height;
this.Show();
break;
}
}
return result;
}