2012-12-05 78 views
2

我正在做一個適用於WPF窗體和winforms的應用程序。我在WPF中創建了一個很酷的背景漸變,但我也需要它在我的窗體中使用它。 我試着用這段代碼,但它不工作。它在Windows窗體中繪製漸變,但不像WPF那樣。使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中。

感謝您的幫助!

回答

1

我不知道這是否會得到它正是因爲你沒有包含一個什麼樣的不同的截圖,但我會採取刺傷:

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應用): gradients

+0

非常感謝你!工作! – Andres