I have a MainForms.cs with Ribbon, I want to put a transparent PictureBox on the top right of the ribbon (The PictureBox represent my logo).
This is what I have tried:
I put the PictureBox on the top right of the Ribbon
I set BackColor to Transparent.
I load a PNG image (Containe transparence)
I set the parent of the image to be the ribbon (and like that the PictureBox will be transparent relative to Ribbon)
Code :
InitializeComponent();
pictureBox1.Parent = ribbon1;
Until here all is working great.
My Problem :
When I resize my Form, the PictureBox disappears.
On the OnPaint fonction i reset all setting like that :
protected override void OnPaint(PaintEventArgs pe)
{
this.Activate();
pictureBox1.Visible = true;
pictureBox1.Show();
pictureBox1.BringToFront();
}
But nothing makes the Picturebox appear. Please, can you tell me what i missed.
I downloaded the DLL that you are using and created a small test example. What I noticed is the Parent property of the PictureBox was set to null. By adding the Parent back to the Picturebox in the OnPaint event I was able to get it working if the size of the Form was growing, but would disappear when the Form size was reduced. When I put the same code in the OnResize EventHandler it works like you would expect.
public partial class Form1 : Form
{
PictureBox pictureBox1 = new PictureBox();
public Form1()
{
InitializeComponent();
pictureBox1.Image = Image.FromFile(#"C:\temp\test.jpg");
pictureBox1.Parent = ribbon1;
pictureBox1.Location = new Point(this.Width-pictureBox1.Width,10);
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (pictureBox1.Parent == null)
{
pictureBox1.Parent = ribbon1;
pictureBox1.Visible = true;
pictureBox1.Location = new Point(this.Width - pictureBox1.Width, 10);
}
}
}
Related
I have a System.Windows.Forms.PictureBox inside System.Windows.Forms.Panel. The Panel has:
fixed dimensions
AutoScroll=true
event handler is subscribed to MouseWheel which is used to zoom-in or zoom-out
On zoom-in/zoom-out I change the dims of the PictureBox and if it exceeds the Panel dimenstions the vertical and/or horizontal scrolls are shown since AutoScroll=true.
Now, on Windows 7 (I have Enterprise edition), once any or both scrollers appear and I continue zooming in with the mouse wheel, the subscribed event handler to MouseWheel continue to be called and the image gets bigger.
But, on Windows 10 (I have Home edition), if any of the scrollers appear, the event handler stopps to be called and the scrollers take over. Meaning the image is scrolled up/down or left/right.
The OP has confirmed in the comments that disabling the Win10 mouse setting "Scroll inactive windows when I hover over them" resolves the issue, however I believe that this can also be handled by preventing the MouseWheel event from bubbling up to the containing Panel control. Asking users to change their preferred settings to make your code function is never a desirable situation.
The following code demonstrates preventing this event bubbling. Just create a new Winform project and replace the Form1 code with this. The code creates a TextBox, and a PictureBox contained in a Panel. The purpose of the TextBox is to just to show its loss of focus when you click on the PictureBox. For Win7, click on the PictureBox to activate it and then use the mousewheel to increase/decrease the PictureBox size.
public partial class Form1 : Form
{
PictureBox pictureBox1;
Panel panel1;
public Form1()
{
InitializeComponent();
Size = new Size(500, 500);
Controls.Add(new TextBox() { TabIndex = 0, Location = new Point(350, 5)});
panel1 = new Panel() {Size = new Size(300, 300), Location = new Point(5, 5), BorderStyle = BorderStyle.FixedSingle,Parent = this, AutoScroll = true};
pictureBox1 = new PictureBox() {Size = new Size(200, 200) , Location = new Point(5,5), BorderStyle = BorderStyle.FixedSingle, Parent = panel1};
pictureBox1.Click += pictureBox1_Click;
pictureBox1.MouseWheel += pictureBox1_MouseWheel;
panel1.MouseWheel += panel1_MouseWheel;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
// On Win10 with "Scroll inactive windows when I hover over them" turned on,
// this would not be needed for pictureBox1 to receive MouseWheel events
pictureBox1.Select(); // activate the control
// this makes pictureBox1 the form's ActiveControl
// you could also use:
// this.ActiveControl = pictureBox1;
}
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
Rectangle r = pictureBox1.Bounds;
int sizeStep = Math.Sign(e.Delta) * 10;
r.Inflate(sizeStep, sizeStep);
r.Location = pictureBox1.Location;
pictureBox1.Bounds = r;
// e is an instance of HandledMouseEventArgs
HandledMouseEventArgs hme = (HandledMouseEventArgs)e;
// setting to true prevents the bubbling of the event to the containing control (panel1)
hme.Handled = true;
// comment out the above line to observe panel1_MouseWheel
// being called
}
private void panel1_MouseWheel(object sender, MouseEventArgs e)
{
System.Diagnostics.Debug.Print("bubbled wheel event");
}
}
reference: HandledMouseEventArgs Class
I have custom windows form that has no border.I apply that custom form on child form. And I have custom MDIParent Form that has also no border. So, My problem is when I maximise child form then top border of is appear out side of MDIForm so how can manage or solve this issue using c#.See my snapshot for more detail of my problem I want to remove border with maximise button from top of the custom MDIForm.
I am unsure how you are calling the child Form, but here is an example:
private void Button1_Click(object sender, EventArgs e)
{
var myForm = new MyCustomForm();
myForm.MdiParent = this;
myForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; //this should hide the border even when it is maximized.
myForm.Show();
}
OR, you can capture the 'maximize' event and override it with your own method which once again makes sure there is no border:
private void MaximizeWindow()
{
var rectangle = Screen.FromControl(this).Bounds;
this.FormBorderStyle = FormBorderStyle.None;
Size = new Size(rectangle.Width, rectangle.Height);
Location = new Point(0, 0);
Rectangle workingRectangle = Screen.PrimaryScreen.WorkingArea;
this.Size = new Size(workingRectangle.Width, workingRectangle.Height);
}
and to capture the maximize event:
private void Form1_Resize (object sender, EventArgs e)
{
if (Form1.WindowState == FormWindowState.Maximized)
{
// Do some stuff
}
}
Sources:
-How to show a child form within a mdi container form which its windowstate= maximized?
-Remove the title bar in Windows Forms
I'm wondering if there is any way to disable screen-drawing for a winform and only draw to a bitmap. What I'm actually trying to achieve is to create a "live image" based on a form but without having to actually having the form visible.
I tried DrawToBitmap while the form was minimized, but this was highly unstable, didn't work and finally crashed.
Ok so I ended up solving this in a bit different way. Following code gets you a Live Messenger-like taskbar thumbnail by drawing you hidden UserControl to a Bitmap and using that as the thumbnail.
Holding your mouse over the taskbar icon still gets you some small thing in the upper left corner. Doesn't bother me but please tell me if you know how to get rid of it!
Make sure you have the Windows API Code Pck from Microsoft to run this http://archive.msdn.microsoft.com/WindowsAPICodePack/Release/ProjectReleases.aspx?ReleaseId=4906
namespace AndreasCoroiu.Controls
{
public partial class TaskbarThumbnail : UserControl
{
TaskbarForm taskbarForm;
public TaskbarThumbnail()
{
InitializeComponent();
if (!DesignMode)
{
taskbarForm = new TaskbarForm();
TabbedThumbnail preview = new TabbedThumbnail(taskbarForm.Handle, taskbarForm.Handle);
TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview(preview);
preview.TabbedThumbnailBitmapRequested += (o, e) =>
{
Bitmap bmp = new Bitmap(Width, Height);
DrawToBitmap(bmp, new Rectangle(new Point(0, 0), bmp.Size));
preview.SetImage(bmp);
e.Handled = true;
};
}
}
public void Show()
{
taskbarForm.Show();
}
private class TaskbarForm : Form
{
public TaskbarForm()
: base()
{
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Size = new System.Drawing.Size(0, 0);
}
}
}
}
I am new to C#, and I am experimenting by placing a dot where a user clicks on a form. I am working with a 300 x 200 form with a maximum size of 640 x 480. Using the form's AutoScrollMinSize property, I set this maximum size:
this.AutoScrollMinSize = new Size(640, 480);
I am able to place dots onto the form, but the dots disappear once I have scrolled out of the visible area. Since I created the scroll area manually, I am looking for a way to add a scroll event handler that will call this.Invalidate() to repaint the form whenever scrolling occurs. Any advice is greatly appreciated.
Here is what I have:
public Form1()
{
InitializeComponent();
this.AutoScrollMinSize = new Size(640, 480);
vScrollBar vScrollBar1 = new VScrollBar();
vScrollBar1.Scroll += new ScrollEventHandler(this.vScrollBar1_Scroll);
}
And here is how I'm calling the event handler:
private void vScrollBar1_Scroll(Object sender, ScrollEventArgs e)
{
this.Invalidate();
}
I got this example from the Microsoft documentation, but the drawing is still disappearing when I scroll vertically, so I know the form is not repainting.
You don't need that VScrollBar control (which you never added to the form anyway).
Just override the OnScroll method of the form:
protected override void OnScroll(ScrollEventArgs se) {
base.OnScroll(se);
this.Invalidate();
}
try to put your event in a deligate
and use
http://msdn.microsoft.com/en-us/library/system.windows.forms.splitter.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollbar.aspx
How to set scroll bar in Windows form
and set
vScrollBar.visible = true and if it is in pannel or form set AutoScroll = true
I'm having trouble displaying a PictureBox in C#. I have two forms. In my main form I'm calling the other form, where the PictureBox is located.
This is how I am calling the second form:
frmODeck oDeck = new frmODeck();
oDeck.Show();
Now, this is my second form, where the PictureBox is located from main form
namespace Shuffle_Cards
{
public partial class frmODeck : Form
{
private PictureBox picBox;
private Image image;
public frmODeck()
{
InitializeComponent();
}
private void frmODeck_Load(object sender, EventArgs e)
{
image = Image.FromFile("C:\\C2.jpg");
picBox = new PictureBox();
picBox.Location = new Point(75, 20);
picBox.Image = image;
picBox.Show();
}
public void getCards()
{
}
}
}
What am I doing wrong, or what am I missing?
Thanks
The picture-box control needs to be added to the control-collection of the top-level control it should belong to - in the case, the form itself. Relevant: Control.Controls.
Replace:
picBox.Show();
with:
Controls.Add(picBox);
Befor you do a picBox.Show(); , you need to add it to the Controls of the window you are loading, with the code #Ani provided:
Controls.Add(picBox);
That should do it!