WPF从进度条获取矩形控件边界
WPF get Rectangle control bounds from progressbar
这一定是一个非常菜鸟的问题,所以请不要太苛刻地评判我!
在 Windows 表单应用程序中,像这样的东西很容易获得进度条的界限:
progressBar1.Bounds.X(or)Y
如何在 WPF 中获得相同的内容?我在互联网上找不到任何关于此的内容。
这是 windows 表单应用程序的进度条 OnClick
方法:
private void progressBar1_MouseClick(object sender, MouseEventArgs e)
{
float absoluteMouse = (PointToClient(MousePosition).X - progressBar1.Bounds.X);
float calcFactor = progressBar1.Width / (float)100;
float relativeMouse = absoluteMouse / calcFactor;
double maxlength = BASS_ChannelBytes2Seconds(BassHandle, (ulong)PlayBackLength);
double percents = (maxlength * relativeMouse) / 100;
UInt64 pos = BASS_ChannelSeconds2Bytes(BassHandle, percents);
BASS_ChannelSetPosition(BassHandle, pos, 0);
progressBar1.Value = Convert.ToInt32(relativeMouse);
int current = BASS_ChannelGetPosition(BassHandle, 0);
TimeLabel.Text = FormatTime(BASS_ChannelBytes2Seconds(BassHandle, (uint)current)) + "/" + FormatTime(BASS_ChannelBytes2Seconds(BassHandle, (uint)PlayBackLength));
}
这是我对 WPF
版本的尝试
private void progressBar1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
double absoluteMouse = (PointFromScreen(GetMousePosition()).X - progressBar1.?);
double calcFactor = progressBar1.Width / (float)100;
double relativeMouse = absoluteMouse / calcFactor;
double maxlength = Imports.BASS_ChannelBytes2Seconds(GlobalVariables.BassHandle, (ulong)GlobalVariables.PlayBackLength);
double percents = (maxlength * relativeMouse) / 100;
UInt64 pos = Imports.BASS_ChannelSeconds2Bytes(GlobalVariables.BassHandle, percents);
Imports.BASS_ChannelSetPosition(GlobalVariables.BassHandle, pos, 0);
progressBar1.Value = Convert.ToInt32(relativeMouse);
int current = Imports.BASS_ChannelGetPosition(GlobalVariables.BassHandle, 0);
TimeLabel.Text = FormatTime(Imports.BASS_ChannelBytes2Seconds(GlobalVariables.BassHandle, (uint)current)) + "/" + FormatTime(Imports.BASS_ChannelBytes2Seconds(GlobalVariables.BassHandle, (uint)GlobalVariables.PlayBackLength));
}
您可以使用 progressBar1.Margin.Left - 代表 X,progressBar1.Margin.Top - 代表 Y。
但是相对于放置ProgressBar的Container会有价值。
这一定是一个非常菜鸟的问题,所以请不要太苛刻地评判我! 在 Windows 表单应用程序中,像这样的东西很容易获得进度条的界限:
progressBar1.Bounds.X(or)Y
如何在 WPF 中获得相同的内容?我在互联网上找不到任何关于此的内容。
这是 windows 表单应用程序的进度条 OnClick
方法:
private void progressBar1_MouseClick(object sender, MouseEventArgs e)
{
float absoluteMouse = (PointToClient(MousePosition).X - progressBar1.Bounds.X);
float calcFactor = progressBar1.Width / (float)100;
float relativeMouse = absoluteMouse / calcFactor;
double maxlength = BASS_ChannelBytes2Seconds(BassHandle, (ulong)PlayBackLength);
double percents = (maxlength * relativeMouse) / 100;
UInt64 pos = BASS_ChannelSeconds2Bytes(BassHandle, percents);
BASS_ChannelSetPosition(BassHandle, pos, 0);
progressBar1.Value = Convert.ToInt32(relativeMouse);
int current = BASS_ChannelGetPosition(BassHandle, 0);
TimeLabel.Text = FormatTime(BASS_ChannelBytes2Seconds(BassHandle, (uint)current)) + "/" + FormatTime(BASS_ChannelBytes2Seconds(BassHandle, (uint)PlayBackLength));
}
这是我对 WPF
版本的尝试
private void progressBar1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
double absoluteMouse = (PointFromScreen(GetMousePosition()).X - progressBar1.?);
double calcFactor = progressBar1.Width / (float)100;
double relativeMouse = absoluteMouse / calcFactor;
double maxlength = Imports.BASS_ChannelBytes2Seconds(GlobalVariables.BassHandle, (ulong)GlobalVariables.PlayBackLength);
double percents = (maxlength * relativeMouse) / 100;
UInt64 pos = Imports.BASS_ChannelSeconds2Bytes(GlobalVariables.BassHandle, percents);
Imports.BASS_ChannelSetPosition(GlobalVariables.BassHandle, pos, 0);
progressBar1.Value = Convert.ToInt32(relativeMouse);
int current = Imports.BASS_ChannelGetPosition(GlobalVariables.BassHandle, 0);
TimeLabel.Text = FormatTime(Imports.BASS_ChannelBytes2Seconds(GlobalVariables.BassHandle, (uint)current)) + "/" + FormatTime(Imports.BASS_ChannelBytes2Seconds(GlobalVariables.BassHandle, (uint)GlobalVariables.PlayBackLength));
}
您可以使用 progressBar1.Margin.Left - 代表 X,progressBar1.Margin.Top - 代表 Y。 但是相对于放置ProgressBar的Container会有价值。