Equivalent of Swing's pack in C# - c#

I am developing a quick app in C#. One thing that I want to do is the equivalent of Swing's (java) pack(). This is a call that allows me to say to a form (Frame in Java), resize yourself to the size of the sum of the components within yourself.
I have searched and searched (the components change dynamically so I cannot simply hardcode the form to the right size) but I cannot find the equivalent method in C#.
Does anyone know what it is called?

You don't even need write code in order to 'pack' the form. In the forms designer, set the form's AutoSize property to true, and set the AutoSizeMode property to GrowAndShrink, repeat this for any controls on the form which may also change size.
And voila!
At runtime (only), the form will dynamically resize itself to accommodate all the controls. If you want it to keep a little distance from the controls so that the controls won't 'stick' to the edges, you can set the Padding property (I'd recommend at least a value of 12,12,12,12).

Also in xaml:
<Window SizeToContent="WidthAndHeight" />

Another solution, available only if the image is smaller than the screen:
Form frm = new Form();
PictureBox pbx = new PictureBox();
pbx.Image = Image.FromFile( _imagePath.Text );
pbx.Dock = DockStyle.Fill;
frm.Controls.Add( pbx );
frm.Size = pbx.Image.Size;
frm.Show();

Related

Setting the height of two controls to be the same

How can I make two controls share the same height. I have set the Size to be the same for both controls but when I run it, I am seeing a difference.
this.txtUserName.Size = new System.Drawing.Size(382, 45);
this.btnLogin.Size = new System.Drawing.Size(75, 45);
Actually both have the same Height as you set them. However in order to accomodate various BorderStyles (or FlatStyles as it is called in the case of Buttons) the full size is not always visible.
So, the Button will look to be one pixel smaller on each side than the Height is has with these (current) appearances:
button1.FlatStyle = FlatStyle.Standard
button1.FlatStyle = FlatStyle.System
The full Size will be visible with
button1.FlatStyle = FlatStyle.Popup
button1.FlatStyle = FlatStyle.Flat
If you are sure about your choice of FlatStyle you may want to adapt their Height (and Location!) accordingly..
To further complicate thing the TextBox's visible Height will change if you set its BorderStyle from FixedSingle or Fixed3D to None: It will shrink by 7 (!) pixels..
Note that as far as I remember, all those details of the borderstyles depend on the Windows version & Visual Styles on the target machine.. I am running W8.1 here
Open the Form.Designer.cs and go to InitializeComponent() method for the form, you'll see initialization code for all components on the form.
If you are setting these values prior to InitializeComponent() call from the constructor, it'll be lost. Otherwise you may need to check Margin and Padding of the components.
http://msdn.microsoft.com/en-us/library/vstudio/ms229627(v=vs.100).aspx

Resizing a directx control automatically with the window form size?

This app I am making is perhaps going to be deployed on machines with differing screen sizes. Whilst I am setting the form to be maximised by default, the size of the directx control (which I want maximised also) needs to be done manually at present in code which isn't ideal. I wondered if there is an easy way to have it resize automatically with the size of the form window.
The winform is mazimised with:
this.WindowState = FormWindowState.Maximized;
And the directx control window with:
this.ClientSize = new System.Drawing.Size(1200, 1200);
I thought something like this would work, but it didn't (those properties aren't even available):
this.ClientSize = new System.Drawing.Size(Form.Height, Form.Width);
Sorry I am still very new!
On the Form's resize event, you have to call directx device's reset method putting new presentationparameters in. Also, you should use XNA over Managed Directx..

How to dock a windows form in C#?

I just would like to know if it is possible to dock a windows form on top of the user screen? I have been trying to do this by manually setting the position of my form to the coordinates I want. But using this method, however, allows the user to change the position of the form just by dragging it. I want to make the form docked to the upper portion of the screen since this window form will server as a menu for the project I am making.
Thanks a lot. :)
I would consider using the Control.Dock property along with one of the DockStyle enumeration values.
You might need to play with the Layout too, so that you may layout your form's controls differently depending on the DockStyle selected.
You will need, in my point of view, to consider the Control.Location property so that you get to know which DockStyle value to dock your form with.
EDIT #1
Your Windows Form has a Dock property as it inherits from Control.
Let's consider the following :
Each time your form comes closer to your right-side of the screen, for example, or of the MDI container, you want to dock right, right ? (Little word play here... =P) So, you have to subscribe to the Control.LocationChanged event.
private void myForm_LocationChanged(object sender, EventArgs e) {
if (this.Location.X > 900) then
this.Dock = DockStyle.Right;
else if (this.Location.X < 150) then
this.Dock = DockStyle.Left;
else if (this.Location.Y > 600) then
this.Dock = DockStyle.Bottom;
else if (this.Location.Y < 150) then
this.Dock = DockStyle.Top;
else
this.Dock = DockStyle.None;
}
Indeed, instead of constant values, you should use the current desktop resolution and calculate a ratio from it where you want your docking to occur.
***Disclaimer:****This code is provided as-is and has not been tested. This algorithm is hopefully enough to guide you through the docking process as you need it. Further assistance may be brought upon request.* =)
It seems the Form.DesktopLocation property is the righter tool for the job as for your main window, meaning your MDI container, for instance. As for the other windows, I would go along with something that looks like the code sample provided.
Does this help?
EDIT #2
If you want to prevent Form's overlapping, perhaps the Control.BringToFront() method could do it before or after your call to the Control.Show() method, depending on what works best for you.
So after some tweaks I finally was able to get this code working.
this.DesktopLocation = new Point((Screen.PrimaryScreen.Bounds.Width / 2 - 420), 0);
I placed that line below the InitializeComponent() and it docks my form to the center of the screen with whatever resolution values.
By setting the FormBorderStyle of your form to None, you take the drag handle away from the user so they cannot move it via the mouse.
Then you just need to place it where you want.
If you really want to take away the users options you can also set the ShowInTaskbar property to false

How do I make a form transparent while keeping the controls fully visible?

I would like to have a form in which the controls on the form are fully visible but the form itself is invisible. If I change the form's Opacity, this makes both the form and the controls on it semi-transparent, so this doesn't work.
I can't do this by setting the form's TransparencyKey, since I have a PictureBox on the form. If the image in the PictureBox happens to contain pixels that match the TransparencyKey, they appear as openings in the form, which I don't want.
TransparencyKey is the only way to get this. Pick the right color. Color.Fuchsia has a long tradition of being the color of choice, going back to the early days of Win32 development. Assault your eye with it to see its merits.
With the caveat that I've never used it, just ran across it once, thought "neat!" and moved on...
Look into System.Drawing.Drawing2D.GraphicsPath and setting the form's Region property. I added two buttons to the basic Windows forms application:
public Form1()
{
InitializeComponent();
Rectangle r1 = new Rectangle(button1.Location, button1.Size);
Rectangle r2 = new Rectangle(button2.Location, button2.Size);
GraphicsPath gp = new GraphicsPath();
gp.AddRectangle(r1);
gp.AddRectangle(r2);
this.Region = new Region(gp);
}
I've approximated the shape of the button with a rectangle; with this code, you can see the form background color at the buttons' corners. You'll need to work out the enclosing path for each of your controls and add them to the path individually. You'll need to take into account any offset introduced by the form title bar or border style.
Update: I did some investigation and have a couple of possible approaches for the problem:
Using the GraphicsPath method, set pictureBox.Visible to False if there is no image loaded.
When you load an image into the picture box, analyze the image to get a list of all the colors in it, then randomly generate one that isn't. Set the form's BackColor and TransparencyKey properties to match this new color, Hans Passant's answer.

Zoom for a windows form in C#

Is there an easy way to set the zoom level for a windows form in C#? In VBA there was a zoom property of the form.
I had the same problem and I solved it this way in c#. Code goes on Form load
float scaleX = ((float)Screen.PrimaryScreen.WorkingArea.Width / 1024);
float scaleY = ((float)Screen.PrimaryScreen.WorkingArea.Height / 768);
SizeF aSf = new SizeF(scaleX, scaleY);
this.Scale(aSf);
This "more or less" scales form and all children. Loops forever in 800x600 (?)
You have to set the following Form properties:
AutoscaleMode = Font
AutoSize = False
You can get some kind of zoom by assigning different Font to the Form, all the controls will be zoomed accordingly if AutoScaleMode set to Font. Also settings AutoSize to False will keep form size intact, the controls will grow to the center of the form. You need to set up all Anchors correctly and test the look, since its just "kind of zoom".
So basically here is sample constructor:
public Form1()
{
InitializeComponent();
AutoSize = false;
AutoScaleMode = AutoScaleMode.Font;
Font = new Font("Trebuchet MS",
10.0f,
FontStyle.Regular,
GraphicsUnit.Point,
((byte)(204))
);
}
After form has been shown assigning new Font will mess up all the controls and this trick will not work.
There is no way (that I know of) to do what you ask with typical WinForms.
If you're doing custom painting/drawing, you can zoom that by using a zoom transform, but so far as I know there is no "Zoom" property for the form in the entire world of .NET and native Windows/C++ APIs combined.
You could probably rig something yourself such that you scale controls by a constant factor. And you can probably find 3rd-party controls/surfaces which support this. And who knows what is possible with WPF. But in a typical WinForms world, no.

Categories