![](/img/trans.png)
[英]How to make s WPF menu look like a Windows Forms ToolStripMenu?
[英]Make WPF gradient in windows forms
我正在做一個復制,其中有WPF表單和Winform。 我在WPF中制作了很酷的背景漸變,但是我也需要它在Windows窗體中使用它。 我嘗試使用此代碼,但無法正常工作。 它以Windows形式繪制漸變,但它不像WPF那樣。
這是WPF的代碼:
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFFCFEFF" Offset="0" />
<GradientStop Color="#FFA6BAD0" Offset="1" />
<GradientStop Color="#FFE4EFF7" Offset="0.317" />
<GradientStop Color="#FFC8D4E3" Offset="0.585" />
<GradientStop Color="#FFB1C6D7" Offset="0.797" />
<GradientStop Color="#FFF7FBFD" Offset="0.146" />
<GradientStop Color="#FFD9E4EE" Offset="0.439" />
</LinearGradientBrush>
</Grid.Background>
這是我的Winform代碼(顏色相同):
public void Form_Background(object sender, PaintEventArgs e)
{
Color c1 = Color.FromArgb(255, 252, 254, 255);
Color c2 = Color.FromArgb(255, 247, 251, 253);
Color c3 = Color.FromArgb(255, 228, 239, 247);
Color c4 = Color.FromArgb(255, 217, 228, 238);
Color c5 = Color.FromArgb(255, 200, 212, 217);
Color c6 = Color.FromArgb(255, 177, 198, 215);
Color c7 = Color.FromArgb(255, 166, 186, 208);
LinearGradientBrush br = new LinearGradientBrush(this.ClientRectangle, Color.Black, Color.Black, 0, false);
ColorBlend cb = new ColorBlend();
cb.Positions = new[] { 0, (float)0.146, (float)0.317, (float)0.439, (float)0.585, (float)0.797 , 1};
cb.Colors = new[] { c1, c2, c3, c4, c5, c6, c7 };
br.InterpolationColors = cb;
// rotate
br.RotateTransform(90);
// paint
e.Graphics.FillRectangle(br, this.ClientRectangle);
}
希望你們能為我提供幫助,我需要非常快速地解決此問題,並且我不想使用圖像背景或將WPF控件放入Winforms中。
謝謝你的幫助!
我不確定是否能完全做到這一點,因為您沒有提供不同之處的屏幕截圖,但我會采取行動:
public void Form_Background(object sender, PaintEventArgs e)
{
Color c1 = Color.FromArgb(255, 252, 254, 255);
Color c2 = Color.FromArgb(255, 247, 251, 253);
Color c3 = Color.FromArgb(255, 228, 239, 247);
Color c4 = Color.FromArgb(255, 217, 228, 238);
Color c5 = Color.FromArgb(255, 200, 212, 217);
Color c6 = Color.FromArgb(255, 177, 198, 215);
Color c7 = Color.FromArgb(255, 166, 186, 208);
// Changed: c1 / c7 as start colors, and at 90 degrees. Removed later transform.
LinearGradientBrush br = new LinearGradientBrush(this.ClientRectangle, c1, c7, 90, true);
ColorBlend cb = new ColorBlend();
cb.Positions = new[] { 0, (float)0.146, (float)0.317, (float)0.439, (float)0.585, (float)0.797, 1 };
cb.Colors = new[] { c1, c2, c3, c4, c5, c6, c7 };
br.InterpolationColors = cb;
// removed rotate call
// paint
e.Graphics.FillRectangle(br, this.ClientRectangle);
}
這看上去很正確,將XAML設計器與我的WinForms輸出進行了比較(左側為XAML設計器,右側為運行WinForms應用):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.