locked
How to get the browser coordinates? RRS feed

  • Question

  • In my app. when the user clicks a button, it opens a window.  I would like the size of this window to be almost the same size of the browser, just a little bit smaller.

    Does anyone has a code example in C# on how to accomplish this?

     Thanks.

    Friday, September 9, 2011 4:41 PM

Answers

  • Hello!

    I think this is what you want:

    string width = Application.Current.Host.Content.ActualWidth.ToString();
    
    string height = Application.Current.Host.Content.ActualHeight.ToString();
    
    MessageBox.Show(string.Format("{0} x {1}", width, height));

    Hope this helps!

    Best Regards

    Echo6

    Tuesday, September 20, 2011 2:32 AM

All replies

  •  how to make the floatable window control

    Make use of child Window control for flotable window of any size.

    Saturday, September 10, 2011 4:08 PM
  • Hi Csbjunior,

    You may have a look at this sample on resizing Childwindow:

    http://leeontech.wordpress.com/2009/06/22/resizing-childwindow/

    And you can set the default size before it show (e.g. op.Height = 800; Width = 1000; op.Show();).

    In addition, you can refer this MSDN article to learn how get a browser-relative coordinate.

    http://msdn.microsoft.com/en-us/library/bb979880(v=vs.95).aspx

    Best Regards,

    Tuesday, September 13, 2011 3:52 AM
    Moderator
  • Haixia Xie - MSFT

    Thanks so much for your info, but I have no problem setting the size of the float window at runtime. what I need to know is the size  (Height and width) of the browser, so that I can open the window the same size of the  browser, respecting some margins 

    Thanks,

    Cid

     

     

     

    Friday, September 16, 2011 8:26 AM
  • Hi csbjunior,

    The following script simply displays a MessageBox showing client screen resolution info on Page Load...
    So if I'm a bit of an old timer and still running an old 800x600 resolution screen and run the script then I should
    see a nice popup message displaying... Current resolution : 800 x 600...
    Hmmm I guess it's about time to get a new flat screen ... Loool

    Still starting from that basic code, you should be able to retrieve the proper Width and Height information you need to resize your ChildWindow accordingly on PageLoad...
    maybe even substract defined top and left margin values from Width and Height if you wish...

    using System.Windows;
    using System.Windows.Browser;
    using System.Windows.Controls;
     
    namespace SilverlightScreenResolutionDemo
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                Loaded += new RoutedEventHandler(MainPage_Loaded);
            }
            void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                string Width = HtmlPage.Window.Eval("screen.width").ToString();
                string Height = HtmlPage.Window.Eval("screen.height").ToString();
                MessageBox.Show(string.Format("Current resolution : {0} X {1}", Width, Height));
            }
        }
    }

    If this answers your question, please Mark as an answer. Thank you. 

    Sunday, September 18, 2011 1:52 PM
  • Thanks for your reply. We are almost there!  I run the code and it indeed gave the screen resolution of 1920 x 1200. However, since the browser is not always maximized, how is this info going to help me find the size of the browser ?  For example: I would like to open the child window 2/3 of the width  and 2/3 of the height of the dimensions of the browser

    Thanks again.

    Monday, September 19, 2011 5:20 PM
  • Hello!

    I think this is what you want:

    string width = Application.Current.Host.Content.ActualWidth.ToString();
    
    string height = Application.Current.Host.Content.ActualHeight.ToString();
    
    MessageBox.Show(string.Format("{0} x {1}", width, height));

    Hope this helps!

    Best Regards

    Echo6

    Tuesday, September 20, 2011 2:32 AM