Get default TitleBar height programmatically before a titlebar is created? - c#

I am creating a custom title bar for my uwp app. I want to match the height of the system bar.
I might be able to get that height be calling
CoreApplication.GetCurrentView().TitleBar.Height
But that depends on a lot of things. The title bar may not have been sized yet.
I've also seen a suggestion (from winforms) to look at the difference of the y coordinates of the window top and the content view top. But again that seems fishy. For one thing, once I've set ExtendViewIntoTitleBar to true, I don't think the method would work.
Is there reliable way to programmatically get the default height?

I know that this answer might not be useful to the person who initially asked, at this point of time, but I would still like to suggest the answer:
You can register a handler for when the size of title bar changes. (The docs mention size change, but it may only be the caption button offset and not height)
This piece of code works well for me, at least at the moment
//Put the below line in the Page initialization/OnNavigated function
var coreTitleBar = CoreApplication.GetCurrentView().TitleBar;
coreTitleBar.ExtendViewIntoTitleBar = true;
coreTitleBar.LayoutMetricsChanged += CoreTitleBar_LayoutMetricsChanged;
private void CoreTitleBar_LayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)
{
MyAppTitleBar.Height = sender.Height;
}
The above code calls your function (CoreTitleBar_LayoutMetricsChanged) automatically whenever the dimensions of the titleBar change (like change in DPI). Here, MyAppTitleBar is a Grid I made for my custom title bar.
Futher info can be found here

Related

How to make a Windows Form responsive [duplicate]

I have read several stack overflow questions without finding a good working solution to my problem. How can I resize my controls whenever the form is resized? I would like them to get larger or smaller when the form becomes larger or smaller.
In visual basic this was quite easy to do with the form.Zoom property (which did't really require resizing controls of course, but solved what I needed). Unfortunately this is not available in C# winforms.
Here is some other things I have tried without luck:
private void formMain_Resize(object sender, EventArgs e)
{/*
double scale;
this.scaleWidth = (float)this.Width / (float)this.origWidth;
this.scaleHeight = (float)this.Height / (float)this.origHeight;
if (this.scaleHeight > this.scaleWidth)
{
scale = this.scaleHeight;
}
else
{
scale = this.scaleWidth;
}
foreach (Control control in this.Controls)
{
control.Height = (int)(control.Height * this.scaleHeight);
control.Width = (int)(control.Width * this.scaleWidth);
this.Refresh();
// control.Font = new Font("Verdana", control.Font.SizeInPoints * heightRatio * widthRatio);
}
///////This scaling didnt work for me either
//this.Scale(new SizeF(this.scaleWidth, this.scaleHeight));
//this.Refresh();
*/
}
If I overlooked an actualy working sample of code on another stack overflow question I would love to see it, but the ones I found were similar to those above which are not working.
Perhaps I was misusing it and someone could post sample code to show for those of us who keep asking this question how to go about solving the problem.
Also, I have tried using some of the anchor/docking tools thinking they would automatically allow it but it didn't.
The best option is to use a TableLayoutPanel. Put TableLayoutPanel on the form, set the Dock property to Fill, create required rows and columns and put the controls inside the cells. Of course you need to set Dock/Anchor on the controls inside the cells, so they respond to changes to the cell size. In some situations you may need to put a Panel into a cell and drop the controls inside it, because every cell can only contain a single control. You may also need to set RowSpan/ColumnSpan on the controls.
By using a TableLayoutPanel, you have complete control over how your cotrols should be arranged. You can set absolute or percentage size for rows and columns.
Use Anchor of the control. There's an option on anchoring the top, bottom, left and right. And you're good to go.
I found an alternative solution that is working well for me, appreciate any negative or positive comments on the solution.
Using several Split Containers and Split Containers inside of Split Containers in different regions I am able to section off the primary pieces of the layout, and within there utilizing Docking and Anchoring I am able to accomplish exactly what I wanted to do - it works beautifully.
I would point out I am aware that some folks online mention split containers use lots of resources.
If your controls are in a group box, be sure to set the group boxes properties to resize. Controls inside the box are controlled by the box. The box size (unless it is inside another box) is controlled by the form.
What you are trying to do in your code is to change the sizes of the controls which isn't so good approach. Generally, the size of the Buttons and TextBoxes shouldn't be changed when you re-size your form, but they often need to move (change location). Some controls do need to change size according to the re-sized form and but in most cases only one dimension. The central controls that are used for working area (if you are developing the tool for drawing for instance) should change sizes of both dimensions. All this you can accomplish by properly setting Dock and/or Anchor properties of the controls.
textBox1.Dock = DockStyle.Bottom;
textBox1.Anchor = AnchorStyles.Bottom & AnchorStyles.Left;
All these are also easily set in the Properties panel when using designer.
But if that isn't enough for you, in rare cases, you will most definitely want to only change the location of the control:
textBox1.Location = new Point(newX, newY);

Horizontal Scroll Bar in Oxyplot WPF

I'm trying to implement a utility for showing throughput over time in a system, and am using Oxyplot to visualise the data.
Currently, zoom and pan are working as expected, but I would like some visual indication to the user which clearly shows whether the graph can be zoomed or panned.
After ditching the idea of using a scroll bar (being neither able to accurately get the position of the visible section of the graph, nor correctly position the thumb of the scroll bar releative to the chart), I have settled on using icons to show whether there is any data on the chart which is hidden to the left or rightmost side.
I would like these icons to work as buttons which allow the user to page left and right on the graph, however as with all things OxyPlot related, the implementation is far more complex than it first seems.
I'm using the WPF implementation, which uses a ViewModel representing the overall data set, with each series item represented by its own model.
This effectively renders almost every tutorial useless as the WPF implementation is significantly different to the basic OxyPlot package.
Currently, the code behind in the view handles the click on the page left/right buttons. I cannot put this in my ViewModel as it must interract directly with the PlotControl object.
private void btnPageRight_Click(object sender, RoutedEventArgs e) {
CategoryAxis axis = (CategoryAxis)PlotControl.ActualModel.Axes[0];
double xAxisMin = axis.ActualMinimum;
double xAxisMax = axis.ActualMaximum;
double visibleSpan = xAxisMax - xAxisMin;
double newMinOffset = xAxisMax + visibleSpan;
PlotControl.Axes[0].Minimum = newMinOffset;
PlotControl.Axes[0].Maximum = newMinOffset + visibleSpan;
PlotControl.ActualModel.InvalidatePlot(true);
}
As it stands, the above code throws no errors, but it does not work either.
If anybody can advise a possible way to make OxyPlot scroll to a given position using just code behind, I would be grateful.
As a last resort, I have pondered trying to simulate a mouse drag event to make this finicky beast behave.
I find the need to work around the problem in that way quite offensive, but desparation leads to odd solutions...
In case anybody else runs into this issue, the following snippet will scroll the graph in pages based on the number of columns visible on the graph at the time.
The snippet takes the number of visible columns as the viewport, and will move the visible area by the viewport size.
Although this applies to the WPF implementation, the only way I could find to make this work was to run this method from the code behind in the View containing the OxyPlot chart.
This should work correctly regardless of the zoom amount at the time.
The CategoryAxis reference must be obtained from the ActualModel as the WPF.Axis does not provide the ActualMinumum and ActualMaximum needed to calculate the viewable area.
The visibleSpan in this case represents the number of columns, with panStep denoting the amount to pan by in pixels.
private void ScrollInPages() {
//To zoom on the X axis.
CategoryAxis axis = (CategoryAxis)PlotControl.ActualModel.Axes[0];
double visibleSpan = axis.ActualMaximum - axis.ActualMinimum;
double panStep = 0;
//Scrolling the chart - comment out as appropriate
//Scroll right one page
panStep = axis.Transform(0 - (axis.Offset + visibleSpan));
//Scroll left one page
panStep = axis.Transform(axis.Offset + visibleSpan);
axis.Pan(panStep);
PlotControl.InvalidateFlag++;
}

c# - create Horiz. scrollbar on chart when data is plotted off-screen

I have a line chart, which, after enough data points have been plotted to it, the data will exceed what is visible on screen (so that the chart is only showing the most recent data). When this occurs, I would like a scroll bar to be filled for the X axis, allowing the user to use the scroll bar to view such previous data.
How do I go about doing this? I don't want the user to be able to drag or zoom on the chart itself, just to solely use the scroll bar to navigate along the chart.
I've looked at this article: https://msdn.microsoft.com/en-us/library/dd456730.aspx but it doesn't help & the scrollbars do not appear.
Without seeing the relevant parts of your code it is hard to pin down your problems.
Here is one strange statement:
after enough data points have been plotted to it, the data will exceed
what is visible on screen (so that the chart is only showing the most recent data).
Now this can only happen after you have set the AxisX.Maximum because by default the chart control will squeeze the area more and more while you add points.
But when you have set a maximum of what can be shown, no scrollbar can work or even been shown. Sounds logical, right?
So either don't set it in the first place or clear it when the number of points exceeds what you want to show. To clear it use NaN :
chart1.ChartAreas[0].AxisX.Maximum = Double.NaN;
Or, of course, set it to the last point you want to be shown!
After looking at what you mustn't do let's see what you need to do to show the scrollbar:
First you enable it:
chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
Next you tell it to show only the scrolling handle and not the zoom-reset button:
chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
See MSDN on ScrollBarButtonStyles for the various things the scrollbar can show/do!
And to make sure the user can't zoom set this:
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = false;
And finally set the current range to show:
chart1.ChartAreas[0].AxisX.ScaleView.Size = 111; // show 111 DataPoints
Now the scrollbar should show.
It is a good idea to study the AxisScaleView class as it has a couple of helpful properties..
Depending on the data type of your X-Values you may also need to set the ScaleView.MinSizeType to whatever suits your data:
chart1.ChartAreas[0].AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Number;

Determine Control's 'AutoSize'-size at runtime

I want to layout controls during runtime (dynamically created). For the purpose of this question, let's restrict to a Button control. I want to set the control's properties (such as Text) and then determine the minimum size for the control for it to display properly; the size that setting AutoSize = true would give. In C# example code, with GetAutoSizeSize being this minimum size:
Button button = new Button();
this.Controls.Add(button);
button.Text = "Example";
button.Size = GetAutoSizeSize(button);
button.Location = /* Some calculation based on button.Size */
Possible solution: AutoSize
One can set button.AutoSize = true and button.AutoSizeMode = AutoSizeNode.GrowAndShrink. After that, the button.Size can be fetched, after which AutoSize can be turned off and the control size can be changed.
Potential issues:
It looks odd and I can't help but feel that this could easily break, but maybe I am wrong?
Possible solution: GetPreferredSize
button.GetPreferredSize can be used to get a size that the control wants to be.
Problems with this:
Its usage is internal and/or meant for flow layout.
GetPreferredSize takes a suggested size as a parameter, so one needs to guess at what would be appropriate.
The size returned is wrong, in that it returns the 'comfy' size of a control, which can be much larger than the minimum size that AutoSize gives.
EDIT: From the comments and some trial-and-error, I was able to conclude that the problems I originally listed with the AutoSize-method were due to needing both the control to be added to the control collection first and AutoSizeMode set to GrowAndShrink.
I would like to know if there is a function (and/or more 'robust' way) of determining the AutoSize-size: a function like GetPreferredSize that returns the size without actually having to toggle AutoSize.
This works when you are drawing on a control.
String sMyString = "this is my string";
Font fntFont = new Font("Arial", 8);
SizeF sfMySize = new SizeF(5,5);
sfMySize = System.Graphics.MeasureString(sMyString, fntFont, sfMySize);
This will give you the dimensions of the bounding box around the control text. You would have to work out the appropriate buffer around the text to set the button size.

Resize Controls with Form Resize

I have read several stack overflow questions without finding a good working solution to my problem. How can I resize my controls whenever the form is resized? I would like them to get larger or smaller when the form becomes larger or smaller.
In visual basic this was quite easy to do with the form.Zoom property (which did't really require resizing controls of course, but solved what I needed). Unfortunately this is not available in C# winforms.
Here is some other things I have tried without luck:
private void formMain_Resize(object sender, EventArgs e)
{/*
double scale;
this.scaleWidth = (float)this.Width / (float)this.origWidth;
this.scaleHeight = (float)this.Height / (float)this.origHeight;
if (this.scaleHeight > this.scaleWidth)
{
scale = this.scaleHeight;
}
else
{
scale = this.scaleWidth;
}
foreach (Control control in this.Controls)
{
control.Height = (int)(control.Height * this.scaleHeight);
control.Width = (int)(control.Width * this.scaleWidth);
this.Refresh();
// control.Font = new Font("Verdana", control.Font.SizeInPoints * heightRatio * widthRatio);
}
///////This scaling didnt work for me either
//this.Scale(new SizeF(this.scaleWidth, this.scaleHeight));
//this.Refresh();
*/
}
If I overlooked an actualy working sample of code on another stack overflow question I would love to see it, but the ones I found were similar to those above which are not working.
Perhaps I was misusing it and someone could post sample code to show for those of us who keep asking this question how to go about solving the problem.
Also, I have tried using some of the anchor/docking tools thinking they would automatically allow it but it didn't.
The best option is to use a TableLayoutPanel. Put TableLayoutPanel on the form, set the Dock property to Fill, create required rows and columns and put the controls inside the cells. Of course you need to set Dock/Anchor on the controls inside the cells, so they respond to changes to the cell size. In some situations you may need to put a Panel into a cell and drop the controls inside it, because every cell can only contain a single control. You may also need to set RowSpan/ColumnSpan on the controls.
By using a TableLayoutPanel, you have complete control over how your cotrols should be arranged. You can set absolute or percentage size for rows and columns.
Use Anchor of the control. There's an option on anchoring the top, bottom, left and right. And you're good to go.
I found an alternative solution that is working well for me, appreciate any negative or positive comments on the solution.
Using several Split Containers and Split Containers inside of Split Containers in different regions I am able to section off the primary pieces of the layout, and within there utilizing Docking and Anchoring I am able to accomplish exactly what I wanted to do - it works beautifully.
I would point out I am aware that some folks online mention split containers use lots of resources.
If your controls are in a group box, be sure to set the group boxes properties to resize. Controls inside the box are controlled by the box. The box size (unless it is inside another box) is controlled by the form.
What you are trying to do in your code is to change the sizes of the controls which isn't so good approach. Generally, the size of the Buttons and TextBoxes shouldn't be changed when you re-size your form, but they often need to move (change location). Some controls do need to change size according to the re-sized form and but in most cases only one dimension. The central controls that are used for working area (if you are developing the tool for drawing for instance) should change sizes of both dimensions. All this you can accomplish by properly setting Dock and/or Anchor properties of the controls.
textBox1.Dock = DockStyle.Bottom;
textBox1.Anchor = AnchorStyles.Bottom & AnchorStyles.Left;
All these are also easily set in the Properties panel when using designer.
But if that isn't enough for you, in rare cases, you will most definitely want to only change the location of the control:
textBox1.Location = new Point(newX, newY);

Categories

Resources