I have a AxWindowsMediaPlayer control on my WinForm.
Everything works so far. But I can't resize or move the control.
I initialize the control like this:
mediaPlayer = new AxWMPLib.AxWindowsMediaPlayer();
mediaPlayer.CreateControl();
mediaPlayer.enableContextMenu = false;
((System.ComponentModel.ISupportInitialize)(mediaPlayer)).BeginInit();
mediaPlayer.Name = "wmPlayer";
mediaPlayer.Enabled = true;
mediaPlayer.Dock = System.Windows.Forms.DockStyle.Fill;
mediaPlayer.Size = this.Size;
this.Controls.Add(mediaPlayer);
((System.ComponentModel.ISupportInitialize)(mediaPlayer)).EndInit();
mediaPlayer.uiMode = "none";
mediaPlayer.URL = fileName;
mediaPlayer.settings.setMode("loop", true);
mediaPlayer.Ctlcontrols.play();
But the size ist always the same. How can I set the Size or the Bounds of this Controls?
Thanks for help
It is better do this in designer, rather than code.
In your code, you set the size of the player control as large as the form.
//occupies all the form's available space
mediaPlayer.Dock = System.Windows.Forms.DockStyle.Fill;
//again, the player is the same size as form
mediaPlayer.Size = this.Size;
In order to set the bound of the player control within the form, you can set the its AnchorStyle- anchoring the control to the edges of the form- and set the control's Location and Size properties.
mediaPlayer.Location = new Point(50, 50);
mediaPlayer.Size = new Size(this.ClientSize.Width - 100, this.ClientSize.Height - 100);
mediaPlayer.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
For anyone who wants to zoom in on AxWindowsMediaPlayer
axWindowsMediaPlayer1.stretchToFit = true;
axWindowsMediaPlayer1.Width *= 2;
axWindowsMediaPlayer1.Height *= 2;
Related
I have a task, that can be easily completed with html/css, but I cant figure out how to do it with C# WindowsForms.
I need to create container with dinamic height and with 3 types of BackgroundImage's. To do so I created panel1 and set BackgroundImageLayout of "body.jpg" to tile.
Then I put panel2 and set BackgroundImageLayout of "top.jpg" to none.
So I need to create only one container with "bottom.jpg" but here is the problem.
Image is tall and it must have layout to bottom, but I cant set BackgroundImage of panel3 to Bottom and I cant dock PictureBox to bottom, because in that case it will prevent to set content overlay.
P.S. Sorry if you cant understand some of my explanations - my native language is Russian.
Here is what constuctor created for me:
//
// Charsheet_bg
//
this.Charsheet_bg.AutoSize = true;
this.Charsheet_bg.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("Charsheet_bg.BackgroundImage")));
this.Charsheet_bg.Controls.Add(this.Charsheet_top);
this.Charsheet_bg.Dock = System.Windows.Forms.DockStyle.Left;
this.Charsheet_bg.Location = new System.Drawing.Point(0, 0);
this.Charsheet_bg.Margin = new System.Windows.Forms.Padding(0);
this.Charsheet_bg.MinimumSize = new System.Drawing.Size(285, 0);
this.Charsheet_bg.Name = "Charsheet_bg";
this.Charsheet_bg.Size = new System.Drawing.Size(285, 488);
this.Charsheet_bg.TabIndex = 30;
//
// Charsheet_top
//
this.Charsheet_top.BackColor = System.Drawing.Color.Transparent;
this.Charsheet_top.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("Charsheet_top.BackgroundImage")));
this.Charsheet_top.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.Charsheet_top.Dock = System.Windows.Forms.DockStyle.Fill;
this.Charsheet_top.Location = new System.Drawing.Point(0, 0);
this.Charsheet_top.MinimumSize = new System.Drawing.Size(285, 200);
this.Charsheet_top.Name = "Charsheet_top";
this.Charsheet_top.Size = new System.Drawing.Size(285, 488);
this.Charsheet_top.TabIndex = 31;
And screenshot will be in 5 mins.
Screenshot with tooltips
When I set a label's ContentAlignment property to MiddleRight, I expect it to align to the right within the panel on which I place it. Instead, it stubbornly stays put aligned left. Why is this, and how can I fix it? Here's my code:
private void CreateNewLabel(int YPos, string DisplayStr, ContentAlignment contentAlign,
int FontSize)
{
Label lbl = new Label();
//lbl.Left = DEFAULT_XPOS;
lbl.Font = new Font(lbl.Font.Name, FontSize, lbl.Font.Style);
lbl.Top = YPos;
lbl.Text = DisplayStr;
lbl.TextAlign = contentAlign;
lbl.AutoSize = true;
panelFauxLabel.Controls.Add(lbl);
}
Everything is working EXCEPT horizontal placement. I don't want to set the Left property, because I want certain alignments to take up all the "right side" space they can; calculating the XPos is possible, I'm sure, but also quite complicated, I'm even more sure.
UPDATE
Olivier's answer worked just fine. The code is now:
private void CreateNewLabel(int YPos, string DisplayStr, ContentAlignment contentAlign, int FontSize)
{
Label lbl = new Label();
lbl.Left = DEFAULT_XPOS;
lbl.Font = new Font(lbl.Font.Name, FontSize, lbl.Font.Style);
lbl.Top = YPos;
lbl.Text = DisplayStr;
lbl.TextAlign = contentAlign;
if (contentAlign.Equals(ContentAlignment.MiddleRight))
{
lbl.Anchor = AnchorStyles.Right;
}
else // there is no AnchorStyles.Center or AnchorStyles.Middle
{
lbl.Anchor = AnchorStyles.Left;
}
lbl.AutoSize = false;
lbl.Width = panelFauxLabel.Width;
panelFauxLabel.Controls.Add(lbl);
}
UPDATE 2
I had to add a tweak to the height of the label to prevent large font sizes from being chopped off at the knees, so to speak:
// This factor was just a guess, but it seems to work pretty well
double down = Math.Round(FontSize*1.5);
lbl.Height = Convert.ToInt32(down);
TextAlign aligns the text within the label, not the label within the panel. Consider using the Anchor property in order to align the label to the right edge of the panel.
UPDATE
Here's how this can be done: Make the label the same width than the panel, anchor the label to the top, left and right
lbl.TextAlign = contentAlign;
lbl.AutoSize = false;
lbl.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
lbl.Top = YPos;
lbl.Left = 0;
lbl.Width = panelFauxLabel.ClientSize.Width; // ClientSize takes account of scroll
// bars, borders and padding.
panelFauxLabel.Controls.Add(lbl);
Now the label resizes together with the panel and the text aligns correctly within the label. Set lbl.AutoSize to false in order to make the label size independent of the text length.
I'm creating a GUI in C# using WinForms.
I'm trying to position programaticaly created panels one below the other. As the content of these panel can vary depending on their content, I'm using Panel.AutoSize to let WinForms perform the correct resizing.
The problem is: if I'm using Panel.Height (or Panel.Size.Height) right after populating the Panel, the value returned is always my default value. The resizing do occur, as I can see when launching the app, but I just don't know when.
Here's a simplified version of what I'm doing:
this.SuspendLayout();
int yPos = 0;
foreach (String entry in entries)
{
Panel panel = new Panel();
panel.SuspendLayout();
panel.AutoSize = true;
panel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
panel.BackColor = System.Drawing.SystemColors.Window; // Allows to see that the panel is resized for dispay
panel.Location = new System.Drawing.Point(0, yPos);
panel.Size = new System.Drawing.Size(this.Width, 0);
this.Controls.Add(panel);
Label label = new Label();
label.AutoSize = true;
label.Location = new System.Drawing.Point(0, 0);
label.MaximumSize = new System.Drawing.Size(panel.Width, 0);
label.Text = entry;
panel.Controls.Add(label);
panel.ResumeLayout(false);
panel.PerformLayout();
yPos += panel.Height; // When breaking here, panel.Height is worth 0
yPos += label.Height; // This works perfectly, label.Height was updated according to the text content when breaking at that point
}
this.ResumeLayout(false);
this.PerformLayout();
So the real question is: How can I get the updated Panel.Size after adding controls to it, to get its proper height value?
Note: I know I can use the TextBox height, but I find it inelegant and impractical, as in my actual code there are more controls in the Panel and I need to use that panel height a few lines below.
What I beleive is happening is that the Size of the Panel will be determined when you do PerformLayout on its Parent. You can make it work like you are wanting by moving the panel's parent SuspendLayout / ResumeLayout code into the Loop.
int yPos = 0;
foreach (String entry in entries)
{
this.SuspendLayout();
Panel panel = new Panel();
panel.SuspendLayout();
panel.AutoSize = true;
panel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
panel.BackColor = System.Drawing.SystemColors.Window; // Allows to see that the panel is resized for dispay
panel.Location = new System.Drawing.Point(0, yPos);
panel.Size = new System.Drawing.Size(this.Width, 0);
this.Controls.Add(panel);
Label label = new Label();
label.AutoSize = true;
label.Location = new System.Drawing.Point(0, 0);
label.MaximumSize = new System.Drawing.Size(panel.Width, 0);
label.Text = entry;
panel.Controls.Add(label);
panel.ResumeLayout(true);
this.ResumeLayout(true);
yPos += panel.Height; // When breaking here, panel.Height is worth 0
//yPos += label.Height; // This works perfectly, label.Height was updated according to the text content when breaking at that point
}
this.PerformLayout();
There is a lot of already answered question and examples about how to draw images within combobox. But I haven't found any examples how to draw animations within combobox.
The gif animation I use is (it's transparent):
And the result I want to achieve is somethink like this:
I'm using Windows Forms and .Net 3.5.
All ways of achieving that, I thought about, were:
1. Use Graphics.DrawImage in ComboBox's DrawItem handler. But the image was drawn statically, there was no anmation.
2. Use PictureBox to show animation and then somehow resize it and place over the ComboBox.
For second soultion I used the following code:
pictureBox1 = new PictureBox();
pictureBox1.Image = Resource.myImage;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
//3 is used just for try to fit image into "white" area of ComboBox
pictureBox1.ClientSize = new Size(comboBox1.Size.Height-3, comboBox1.Size.Height-3);
pictureBox1.BackColor = System.Drawing.Color.Transparent;
pictureBox1.Dock = DockStyle.Left;
pictureBox1.Parent = this.comboBox1;
pictureBox1.Enabled = true;
pictureBox1.Visible = true;
But in result I've got this:
It's animated, but picturebox is drawn on ComboBox edges and it looks bad.
So, can anyone give me an advice or some help to achive this?
Thank you.
EDIT:
My final solution that worked:
pictureBox1 = new PictureBox();
pictureBox1.Image = Resource1.myImage;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.ClientSize = new Size(comboBox1.Size.Height - SystemInformation.Border3DSize.Height, comboBox1.Size.Height - (2 * SystemInformation.Border3DSize.Height));
pictureBox1.BackColor = System.Drawing.Color.Transparent;
pictureBox1.Location = new Point(SystemInformation.Border3DSize.Width, SystemInformation.Border3DSize.Height);
pictureBox1.Parent = this.comboBox1;
pictureBox1.Enabled = true;
pictureBox1.Visible = true;
Thank you all! You help me a lot!
Try this:
pictureBox1 = new PictureBox();
pictureBox1.Image = Resource.myImage;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
//2 is used just for try to fit image into "white" area of ComboBox
pictureBox1.ClientSize = new Size(comboBox1.Size.Height - 2, comboBox1.Size.Height - 2);
pictureBox1.BackColor = System.Drawing.Color.Transparent;
pictureBox1.Left = 1;
pictureBox1.Top = 1;
pictureBox1.Parent = this.comboBox1;
pictureBox1.Enabled = true;
pictureBox1.Visible = true;
remove the code that sets the "Dock" property. Setting this causes the layout manager to ignore size/location settings.
Instead, set the Size property and the Location property to specific values.
Something like:
pictureBox3.Size = new Size(comboBox1.Size.Height-3, comboBox1.Size.Height-3);
pictureBox3.Location = new Point(0, 3);
You may have to adjust these to get the extact position you need.
It might be then a tad too small, but it would fit, if you set the size of your combobox to pictureBox1.Size = new Size(comboBox1.ItemHeight, comboBox1.ItemHeight).
Or set the height and width to 2*SystemInformation.3DBorderSize
I have a flowlayout as follows:
I need to center all the controls on the form (In other words, let's say the form's width is 200. btnOpt1 to btnOpt4 should have their Left starting at 100 minus half of the button width, not 0.)
You can do it two ways but with some limitation of each one.
Using Anchor property
Using the layout control with help of Docking and Anchor properties.
Method 1: Anchor Property
Controls are anchored by default to the top left of the form which
means when the form size will be changed, their distance from the top
left side of the form will remain constant. If you change the control
anchor to bottom left, then the control will keep the same distance
from the bottom and left sides of the form when the form if resized.
Turning off the anchor in a direction will keep the control centred in
that direction when resizing.
Example :
public TestForm12()
{
InitializeComponent();
Button btn = new Button();
btn.Width = this.Width - 10;
btn.Height = 20;
btn.Left = (this.ClientSize.Width - btn.Width) / 2;
btn.Top = (this.ClientSize.Height - btn.Height) / 2;
btn.Text = "click me";
this.Controls.Add(btn);
btn.Anchor = AnchorStyles.None;
}
2. Using the layout control
Add TableLayout Control, Set it’s Dock property to Fill.
Add 1 Row with Size Type style Percent 100%
Add 3 Columns Column1(Size Type – Percent(100%)), Column2(Size Type – Absolute(200px)), Column3(Size Type – Percent(100%)).
Now Add Panel Control to Column2 and Set it’s Dock property to Fill
Add Buttons to this control and set their Size as you want and Set Their Anchor Property to AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top
Example - Designer.cs code snippet of the form.
private void InitializeComponent()
{
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.tableLayoutPanel1.SuspendLayout();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnCount = 3;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 200F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Controls.Add(this.panel1, 1, 0);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 1;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(284, 262);
this.tableLayoutPanel1.TabIndex = 0;
//
// panel1
//
this.panel1.Controls.Add(this.button2);
this.panel1.Controls.Add(this.button1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(45, 3);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(194, 256);
this.panel1.TabIndex = 0;
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(3, 9);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(188, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.button2.Location = new System.Drawing.Point(3, 38);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(188, 23);
this.button2.TabIndex = 0;
this.button2.Text = "button1";
this.button2.UseVisualStyleBackColor = true;
//
// TestForm11
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "TestForm11";
this.Text = "TestForm11";
this.tableLayoutPanel1.ResumeLayout(false);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
Hope this help..
I'd go with TableLayoutPanel instead:
Put TableLayoutPanel on your form
Set dock style Fill to panel
Leave only one column inside panel
Create row for every button (and put buttons to table cells)
Set row size type Autosize
Set dock style Fill to every button, except last one
Set dock style Top to last button
BTW in your solution you should iterate over flowLayoutPanel controls instead of form controls. Also consider subtracting horizontal margin and padding from width:
foreach (Control control in flowLayoutPanel.Controls)
{
control.Size = new Size(flowLayoutPanel.Width - control.Margin.Horizontal,
control.Height);
}
But I advise you to use TableLayoutPanel instead.
I solved this by changing the margin values. I am adding my content to a panel though.
C#:
int horizontalMargin = (int)(0.5 * (this.containingPanelOrForm.Width - this.button.Width));
this.btnOptX.Margin = new Padding(horizontalMargin, 0, horizontalMargin, 0);
Or you can use Grid layout instead.
I´m not good in C# but you can also add a panel in flowlayoutpanel with the same width of flowlayoutpanel. Then you can add in the Panel created in running time the button you want and set the dock to left or right. As you wish. Let me show a example in VB.net and C# (used online converts)
VB.net
Dim btn As New Button
btn.Text = "Example"
btn.Name = "Button"
btn.Size = New Size(60,10)
Dim panel As New Panel
panel.Size = New Size(FlowLayoutPanel1.Width, 10) 'size of the flowlayoutpanel + height of button
btn.Dock = DockStyle.Right
FlowLayoutPanel1.Controls.Add(panel)
panel.controls.add(btn)
C#
Button btn = new Button();
btn.Text = "Example";
btn.Name = "Button";
btn.Size = new Size(60, 10);
Panel panel = new Panel();
panel.Size = new Size(FlowLayoutPanel1.Width, 10);
//size of the flowlayoutpanel + height of button
btn.Dock = DockStyle.Right;
FlowLayoutPanel1.Controls.Add(panel);
panel.controls.#add(btn);
Create empty Label with Name = lblEmpty and AutoSize = False. Put this control first in controls list in FlowLayoutPanel1, then add code below.
Example: Assuming three existing labels in FlowLayoutPanel1, the result should be lblEmpty, LabelExisting1, and LabelExisting2, in that order.
Dim MarginLabelEmpty As Integer = ((FlowLayoutPanel1.Width - (LabelExisting1.Width + LabelExisting2.Width)) / 2)
lblEmpty.Width = MarginLabelEmpty
I solved my problem by creating this code.
in your case with Button Controls, create 4 new labels with .Text=""(empty) and put each one at the beginning of each button, naming labels as follows: lblEmpty1, lblEmpty2, lblEmpty3, lblEmpty4.
Then Add the following code:
Dim MarginLeftbtnOptAll As Integer = ((FlowLayoutPanel1.Width - btnOpt1.Width) / 2)
lblEmpty1.AutoSize = False
lblEmpty1.Width = MarginLeftbtnOptAll
lblEmpty2.AutoSize = False
lblEmpty2.Width = MarginLeftbtnOptAll
lblEmpty3.AutoSize = False
lblEmpty3.Width = MarginLeftbtnOptAll
lblEmpty4.AutoSize = False
lblEmpty4.Width = MarginLeftbtnOptAll
This center button, increasing the width of the empty label according to the width of the FlowLayoutPanel1
Throwing the buttons directly to the form or a panel (not FlowLayoutPanel), and setting Anchor = Top (only Top) for all of them, they won't be centered but will always move proportional to the form's (or container's) sides when resizing.
Private Sub FlowLayoutPanel1_SizeChanged(sender As Object, e As EventArgs)` Handles FlowLayoutPanel1.SizeChanged
Dim TotalWidth As Integer = FlowLayoutPanel1.Controls.Count * button.Width
Dim LeftPadding As Integer = (FlowLayoutPanel1.Width - TotalWidth) / 2
Dim GapPadding As Integer = FlowLayoutPanel1.Controls.Count * 5
If TotalWidth <= FlowLayoutPanel1.Width Then
FlowLayoutPanel1.Padding = New Padding(LeftPadding - GapPadding, 0, 0, 0)
End If
End Sub