Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7626007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:16:04+00:00 2026-05-31T05:16:04+00:00

Is it possible to compare the relative Z order (which one will draw on

  • 0

Is it possible to compare the relative Z order (which one will draw on top) of two WPF controls regardless of their relation in the logical/visual tree structure and what layout containers are used?

I need this to be able to determine the visible regions of a control that might be overlapped by others. My plan is to start with the control geometry and intersect with the geometries of all the controls that will draw on top using CombinedGeometry having the GeometryCombineMode property set to “Exclude”.

Update:
If everyone is interested I’ve used gdi32 API to calculate the visible area.

    [DllImport("gdi32.dll")]
    static extern IntPtr CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);

    [DllImport("gdi32.dll")]
    static extern int CombineRgn(IntPtr hrgnDest, IntPtr hrgnSrc1, IntPtr hrgnSrc2, int fnCombineMode);

    [DllImport("gdi32.dll")]
    static extern int GetRegionData(IntPtr hRgn, uint dwCount, IntPtr lpRgnData);

    [DllImport("gdi32.dll")]
    static extern bool SetRectRgn(IntPtr hRgn, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);

    [DllImport("gdi32.dll")]
    static extern bool DeleteObject(IntPtr hObject);

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:16:05+00:00Added an answer on May 31, 2026 at 5:16 am

    I ran into a similar problem last night. The goal for me was to determine whether one control would be drawn in front of another. I searched for about 45 minutes and couldn’t find a solution provided by APIs so I had to traverse the visual tree to find out which one would be drawn on top. So, I guess based on my research my answer is no, you can’t compare relative z order without looking at the visual tree yourself.

    In case you still need, here is the code I used to find the relative z order of controls (InFrontOf method), which also takes into account manually setting the .ZOrder property of controls (works for my code but not thoroughly tested, does not work if ZOrder property of control is set to negative value).

    private bool InFrontOf(FrameworkElement c1, FrameworkElement c2){
        Panel root = FindWindowRoot(c1); // Find the root of the document, assumes that c1 and c2 are part of the same document
        Trace.Assert(root != null, "root of ui element is not a window or a panel that contains children");
    
        int z1 = Math.Max(Panel.GetZIndex(c1), GetDrawOrder(root, c1));
        int z2 = Math.Max(Panel.GetZIndex(c2), GetDrawOrder(root, c2));
    
        return z1 > z2;
    }
    
    
    
    private Panel FindWindowRoot(FrameworkElement child)
    {
        FrameworkElement current = child;
        while(current as Window == null)
        {
            current = (FrameworkElement)VisualTreeHelper.GetParent(current); 
        }
        return ((Window)current).Content as Panel;
    }
    
    private int GetDrawOrder(Panel root, FrameworkElement needle)
    {
        int result = 0;
        FrameworkElement current = root;
        Queue<FrameworkElement> toSearch = new Queue<FrameworkElement>();
        toSearch.Enqueue(current);
        while(needle != current)
        {
            if(current is Panel)
            {
                Panel p = (Panel) current;
                foreach (FrameworkElement frameworkElement in p.Children)
                {
                    toSearch.Enqueue(frameworkElement);
                }
            }
            if (current is ContentControl)
            {
                ContentControl cc = (ContentControl)current;
                if(cc.Content as FrameworkElement != null)
                    toSearch.Enqueue(cc.Content as FrameworkElement);
            }
            current = toSearch.Dequeue();
            result++;
        }
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: Compare 2 dates with JavaScript Compare two dates in JavaScript I am
would be possible to Compare two strings to find Alliteration and Assonance? i use
Is it possible to compare the contents of two files in Dreamweaver?
Is it possible to compare variables of raw datatypes? I'm working with XMLDOM.DomNodes, which
Is it possible to compare two 3D images? If possible what are the possible
Possible Duplicate: How does Python compare string and int? Can any one explain the
Possible Duplicate: How do you compare two version Strings in Java? I've 2 strings
Possible Duplicate: What is the best way to compare two entity framework entities? I
Is it possible to compare two HPROF files? How?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.