I have a WPF window that spawns a custom usercontrol
owner = Window.GetWindow(placementTarget);
form = new Form();
form.Opacity = owner.Opacity;
form.ShowInTaskbar = false;
form.FormBorderStyle = FormBorderStyle.None;
System.Drawing.Size size = new System.Drawing.Size(Convert.ToInt32(placementTarget.ActualWidth), Convert.ToInt32(placementTarget.ActualHeight));
form.ClientSize = size;
webBrowser.Dock = DockStyle.Fill;
form.Controls.Add(webBrowser);
Cursor.Hide();
Point location = placementTarget.TransformToAncestor(owner).Transform(new Point(0, 0));
form.Left = Convert.ToInt32(location.X + owner.Left);
form.Top = Convert.ToInt32(location.Y + owner.Top);
DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromProperty(UIElement.OpacityProperty, typeof(Window));
descriptor.AddValueChanged(owner, delegate { form.Opacity = owner.Opacity; });
This displays the form, and I can see it if I move the left property of the form to a negative value so that my main window is not covering it.
For some reason though, I cannot seem to get this form to show up ontop of the window. Any ideas?
Cheers.
Related
I try to open a form just below a DataGridView header cell. I have this (and it is not working):
private void button1_Click(object sender, EventArgs e)
{
Form aForm = new Form();
aForm.Text = #"Test";
aForm.Top = this.Top + dataGridView1.Top - dataGridView1.GetCellDisplayRectangle(0, 0, false).Height;
aForm.Left = this.Left + dataGridView1.GetCellDisplayRectangle(0, 0, false).Left;
aForm.Width = 25;
aForm.Height = 100;
aForm.ShowDialog();
}
I don't see how to get the right top and left based on the DataGridView cell.
Should you consider to use a Form, you have to calculate its Location using Screen coordinates:
Form form = new Form();
form.StartPosition = FormStartPosition.Manual;
form.FormBorderStyle = FormBorderStyle.FixedSingle;
form.Size = new Size(dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].Width, 100);
Point c = dataGridView1.PointToScreen(dataGridView1.GetCellDisplayRectangle(
dataGridView1.CurrentCell.ColumnIndex,
dataGridView1.CurrentCell.RowIndex, false).Location);
form.Location = new Point(c.X, c.Y);
form.BringToFront();
form.Show(this);
If you find youself in trouble using a Form, you might consider using a Panel instead:
Point c = dataGridView1.PointToScreen(dataGridView1.GetCellDisplayRectangle(
dataGridView1.CurrentCell.ColumnIndex,
dataGridView1.CurrentCell.RowIndex, false).Location);
Point r = this.PointToClient(c);
panel1.Location = new Point(r.X, r.Y);
panel1.BringToFront();
Take also a look at How do I to get the current cell position x and y in a DataGridView?
and Position new form directly under Datagridview selected row of parent
Sometime ago I asked this question. I get an answer and it worked fine at time. But now, I'm trying to do the same but isn't working. I have a Form and a FlowLayoutPanel setted in the same way as the answer but it isn't working. Both Form has FLowLayoutPanel has set AutoSize to true and FlowDirection set to TopDown but the form is growing vertically without pushing down the progressBar control and label itself. Here's what's like my form after click on button a couple of times(the button's code is the same as in the accepted question in the link I have linked):
What am I missing?
Try this and see if it works!
public Form1()
{
InitializeComponent();
Size = new Size(400, 150);
AutoSize = true;
AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
FlowLayoutPanel panel = new FlowLayoutPanel();
panel.Size = new Size(200, 150);
panel.MaximumSize = new System.Drawing.Size(panel.Width, int.MaxValue);
panel.FlowDirection = FlowDirection.TopDown;
panel.AutoSize = true;
panel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
Controls.Add(panel);
Label label = new Label();
label.Text = "Starting text!\n";
label.Padding = new System.Windows.Forms.Padding(0, 0, 0, 50);
label.AutoSize = true;
panel.Controls.Add(label);
ProgressBar progressBar = new ProgressBar();
progressBar.Location = new Point(0, 125);
progressBar.Size = new Size(190, 25);
panel.Controls.Add(progressBar);
Button button = new Button();
button.Location = new Point(275, 50);
button.Text = "Click me!";
button.Click += (object sender, EventArgs e) => { label.Text += "some more text, "; };
Controls.Add(button);
}
Ok, I've tested the solution suggested in the earlier post you made and it's working fine for me...
Test these things:
Make sure both the Label and the ProgressBar are located inside the FlowLayoutPanel
If you mean that it's growing horizontally <---->, then set the MaximumSize-Width of the FlowLayoutPanel to how wide it can be before switching to a new row (and from there growing vertically instead!)
Otherwise please provide more information so that I can help you from there.
I have a winform that starts out with a sizable border around it. On the form is a button which, when pressed, changes the form to border style none.
The problem is that then the inner part of the form moves up and to the left slightly. I want to make it that, no matter what border is used, the "inner" part of the form will always stay in the same spot (note: but I do still want the form to be moved around when it has a movable border selected)
Thanks.
The Borderless form moves up and slightly left because thats the location that the form currently has,you need to count for the border.To achieve the result you are after you need to reassign the location property and to do that you need to account for the client size and the whole size(with border),the code i think is simple and it will be self-explanatory i believe:
private void button1_Click(object sender, EventArgs e)
{
var X = (this.Size.Width - this.ClientRectangle.Width) / 2;
var Y = (this.Size.Height - this.ClientRectangle.Height) - X;
Point p = new Point(Location.X + X, Location.Y + Y);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Location = p;
}
Another option is to set the padding on the parent that contains the panel with the border. E.g.
public class Form10 : Form {
Button btn = new Button { Text = "Button", Location = new Point(100, 100) };
Panel border = new Panel { Dock = DockStyle.Fill, BorderStyle = BorderStyle.Fixed3D };
public Form10() {
Controls.Add(border);
border.Controls.Add(btn);
btn.Click += delegate {
if (border.BorderStyle == BorderStyle.Fixed3D) {
border.BorderStyle = BorderStyle.None;
border.Parent.Padding = new Padding(2);
}
else {
border.BorderStyle = BorderStyle.Fixed3D;
border.Parent.Padding = new Padding(0);
}
};
}
}
I use WebBrowser in some DLL to make screenshots.
The main problem that it is not maximized sometimes and I guess it takes settigns of the Internet Explorer.
So my question is how to maximize WebBrowser control via C#?
Thank you!
Rectangle r = new Rectangle();
r.X = cropToRectangle.X;
r.Y = cropToRectangle.Y;
r.Width = cropToRectangle.Width;
r.Height = cropToRectangle.Height;
Point p = new Point();
p.X = scrollTo.X;
p.Y = scrollTo.Y;
var sb = Math.Max(SystemInformation.VerticalScrollBarWidth, SystemInformation.HorizontalScrollBarHeight);
var size = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
using (var form =
new FocuslessForm
{
Width = size.Width + sb,
Height = size.Height + sb,
Padding = new Padding(0),
Margin = new Padding(0),
FormBorderStyle = FormBorderStyle.None,
Opacity = 0,
TabStop = false,
ShowInTaskbar = false
})
{
var webBrowser1 = new WebBrowser
{
Padding = new Padding(0),
Margin = new Padding(0),
Dock = DockStyle.Fill,
Url = url,
TabStop = false
};
form.Controls.Add(webBrowser1);
var finished = false;
webBrowser1.DocumentCompleted += delegate
{
webBrowser1.Document.Window.ScrollTo(p);
finished = true;
};
form.Show();
while (!finished)
{
Application.DoEvents();
}
image = CaptureBrowserScreenshot(webBrowser1, r);
form.Close();
}
Well, the WebBrowser is a control that is embedded into your own program's window; it doesn't launch IE as a separate process (though it does hook into IE for the renderer and other critical code). So, the control's location and size is dependent on where you embed it.
I see you are fill-docking the control to the form. This is a good first step. Now, you must make sure the WebBrowser control is being added to the Controls hierarchy of the Form (so it'll show up), and then you must maximize that Form. The way to do this is to set the WindowState property of the Form to WindowState.Maximized.
I implemented Form resize event, when form is maximize, i set web browser size to form size.
It worked well in my app, I share for whom concern.
private void Form1_Resize(object sender, EventArgs e)
{
webBrowser1.Size = this.Size;
}
I'm developing a WinForms application and can't figure out how to resolve an issue.
I need to show an image in a Form. Because the image can be arbitrarily large, I need scrollbars on the picturebox containing the image so the user can see it entirely.
Googling around I found out the best way to achieve this is to add the PictureBox as a Child Control of a Panel, and making the Panel autosizable and autoscrollable.
I did that programatically since using the designer I was unable to insert the picturebox as a child control of the panel.
The problem I'm now facing is that I can't seem to be able to center and scroll the picturebox at the same time.
If I put the anchor of the picturebox to top,left,bottom,right, the scrollbars are not shown and the image displayed is strange, if I put back the anchor to only top-left, the image is not centered.
Is there any way to do both at the same time?
Here's the code for my Panel and Picturebox:
this.panelCapturedImage = new System.Windows.Forms.Panel();
this.panelCapturedImage.SuspendLayout();
this.panelCapturedImage.AutoScroll = true;
this.panelCapturedImage.AutoSize = true;
this.panelCapturedImage.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.panelCapturedImage.Controls.Add(this.pictureBoxCapturedImage);
this.panelCapturedImage.Location = new System.Drawing.Point(0, 49);
this.panelCapturedImage.Name = "panelCapturedImage";
this.panelCapturedImage.Size = new System.Drawing.Size(3, 3);
this.panelCapturedImage.TabIndex = 4;
this.pictureBoxCapturedImage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBoxCapturedImage.Location = new System.Drawing.Point(0, 0);
this.pictureBoxCapturedImage.Name = "pictureBoxCapturedImage";
this.pictureBoxCapturedImage.Size = new System.Drawing.Size(0, 0);
this.pictureBoxCapturedImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.pictureBoxCapturedImage.TabIndex = 0;
this.pictureBoxCapturedImage.TabStop = false;
this.panelCapturedImage.Controls.Add(this.pictureBoxCapturedImage);
And here's where I set the image:
public Image CapturedImage
{
set
{
pictureBoxCapturedImage.Image = value;
pictureBoxCapturedImage.Size = value.Size;
}
}
For the PictureBox, set SizeMode = AutoSize, Anchor it Top, Left, and set its Location to 0, 0.
Set Panel.AutSize to False and Panel.AutoScroll to True.
When you set the PictureBox.Image property, it will auto-size to the size of the image. You can then use that size to set the panel's AutoScrollPosition property:
public Image CapturedImage
{
set
{
pictureBoxCapturedImage.Image = value;
panelCapturedImage.AutoScrollPosition =
new Point {
X = (pictureBoxCapturedImage.Width - panelCapturedImage.Width) / 2,
Y = (pictureBoxCapturedImage.Height - panelCapturedImage.Height) / 2
};
}
}
If the image is smaller then then panel's size, it will remain in the upper left corner. If you want it centered within the panel, you'll have to add logic to set its Location appropriately.
Based on earlier answers I was able to create this full example:
private void testShowPictureBox()
{
/* format form */
Form frmShowPic = new Form();
frmShowPic.Width = 234;
frmShowPic.Height = 332;
frmShowPic.MinimizeBox = false;
frmShowPic.MaximizeBox = false;
frmShowPic.ShowIcon = false;
frmShowPic.StartPosition = FormStartPosition.CenterScreen;
frmShowPic.Text = "Show Picture";
/* add panel */
Panel panPic = new Panel();
panPic.AutoSize = false;
panPic.AutoScroll = true;
panPic.Dock = DockStyle.Fill;
/* add picture box */
PictureBox pbPic = new PictureBox();
pbPic.SizeMode = PictureBoxSizeMode.AutoSize;
pbPic.Location = new Point(0, 0);
panPic.Controls.Add(pbPic);
frmShowPic.Controls.Add(panPic);
/* define image */
pbPic.ImageLocation = #"c:\temp\pic.png";
frmShowPic.ShowDialog();
}
The picturebox has to be set to autosize. anchored at the center (or a border).
You could manage all of this in the designer, don't undertand your problem with that.
The panel has to be set to autoscroll to true.