Search code examples
c#wpfwinformsprogress-barrect

WPF get Rectangle control bounds from progressbar


This must be a very noobie question so please don't judge me too hard! In Windows Form Applications something like this would easily get the bound of a progressbar:

progressBar1.Bounds.X(or)Y

How can I get the same in WPF? I can't find anything on this on internet. This is my progressbar OnClick method for the windows form app:

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));
}

This is my attempt on it's WPF version

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));
}

Solution

  • You may use progressBar1.Margin.Left - for X, progressBar1.Margin.Top - for Y. But there will be value relative to Container where ProgressBar is placed.