2
我發現一個程序,並試圖將它從C#移植到WPF,即時通訊做得很好,但我有UserControl的問題。我無法將其轉換爲WPF,因爲我的知識是有限的。在C#中,我們使用的OnPaint,在WPF的OnRender,我不能得到一個線索,如何實現在WPF 這裏同用戶控件的代碼:WPF移植的WinForms
public partial class DownloadBar : UserControl
{
public DownloadBar()
{
InitializeComponent();
}
public enum DownloadBarState
{
Setup,
Available,
Playable
}
protected double percent = 0.0f;
[Category("Download Bar Properties")]
[DefaultValue(true)]
public double Value
{
get
{
return percent;
}
set
{
if (value < 0)
value = 0;
else if (value > 100)
value = 100;
percent = value;
this.Invalidate();
}
}
protected DownloadBarState dBarState = DownloadBarState.Setup;
[Category("Download Bar Properties")]
public DownloadBarState BarState
{
get
{
return dBarState;
}
set
{
dBarState = value;
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
switch (dBarState)
{
case DownloadBarState.Setup:
g.DrawImage(Properties.Resources.dbar_setup, 0, 0);
g.DrawImage(Properties.Resources.red_progressbar, 5, 35, (int)((Value/100) * this.Width), 10);
break;
case DownloadBarState.Available:
g.DrawImage(Properties.Resources.dbar_available, 0, 0);
g.DrawImage(Properties.Resources.yellow_progressbar, 5, 35, (int)((Value/100) * this.Width), 10);
break;
case DownloadBarState.Playable:
g.DrawImage(Properties.Resources.dbar_playable, 0, 0);
g.DrawImage(Properties.Resources.DownloadProgressBarGreen, 5, 35, (int)((Value/100) * this.Width), 10);
break;
}
}
}
這裏是第二部分:
public partial class DownloadProgressBar : UserControl
{
public DownloadProgressBar()
{
InitializeComponent();
}
public enum ProgressBarColor
{
Green,
Blue,
Red,
Yellow
}
protected double percent = 0.0f;
[Category("Behavior")]
[DefaultValue(true)]
public double Value
{
get
{
return percent;
}
set
{
if (value < 0)
value = 0;
else if (value > 100)
value = 100;
percent = value;
progressBarPictureBox.Size = new Size((int)((value/100) * this.Width), this.Height);
this.Invalidate();
}
}
public void DownloadColor(ProgressBarColor color)
{
switch (color)
{
case ProgressBarColor.Green:
progressBarPictureBox.BackgroundImage = Properties.Resources.DownloadProgressBarGreen;
break;
}
}
}
閱讀關於Invalidate,並且在WPF中也沒有關於WPF OnRender的這種選項,但仍然不知道如何執行此操作。任何幫助真的很感激!
這裏是什麼即時試圖實現
'C#to WPF'這是什麼意思? – EZI
@EZI LOL ...我認爲OP意味着WPF的winforms。 –
是的,編輯的主題標題 –