CefSharp WinForms - Flickering when page loading - c#

I am using CefSharp.WinForms version 84.4.10.
My application is a wip game launcher for the games I make.
Screenshot of the launcher.
There is a Main form that loads and unloads child forms when buttons are pressed. The child forms contain a panel stretched to fill the whole child form. The CefSharp is initialized on the panel.
Here's the code that does that:
public CefSharp.WinForms.ChromiumWebBrowser browser;
private void Changelog_Load(object sender, EventArgs e)
{
browser = new CefSharp.WinForms.ChromiumWebBrowser("https://aerial-knightstudios.com/store/akslauncher/")
{
Dock = DockStyle.Fill,
Size = new Size(600, 600),
Location = new Point(200, 200),
};
this.panelChangelogPage.Controls.Add(browser);
}
When the child form is enabled, a black box appears that fills half the form then it flickers white then the webpage loads.
So far I have tried the following:
I have turned on double buffering by adding DoubleBuffered = true; inside public FormMain() and this made the flickering less intense, but it still flickers a lot.
I also changed [STAThread] in program.cs to [MTAThread]. This also helped, but very slightly.
Any solutions are welcome.

Related

C# Winforms anchoring doesn't work when started maximized

I have windows form with some form with buttons and other components. Buttons are anchored to Bottom and Right. When the application is started as maximized, the buttons don't appear in the corner of the form as they should but remain as if they were anchored to Top and Left. If I unmaximize this form, they remain as far away from the bottom right as in maximized. However, if the form is started normal, not maximized, then everything is ok and the buttons are in the corner. I have the same buttons in other forms and there everything works good, only this one is broken. How can I fix this?
Designer:
Form started maximized:
Form unmaximized:
Now I change the WindowState from Maximized to Normal:
And when I maximize:
And this happens only in this form, in other forms, this solution works fine, it doesn't matter if maximized or normal. What is broken in this particular form?
Designer.cs code for this form is the following (the whole file has over 600 lines, so I selected the important ones:
this.buttonAneks = new System.Windows.Forms.Button();
(...)
// buttonAneks
//
this.buttonAneks.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonAneks.BackColor = System.Drawing.SystemColors.Window;
this.buttonAneks.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.buttonAneks.Location = new System.Drawing.Point(715, 585);
this.buttonAneks.Name = "buttonAneks";
this.buttonAneks.Size = new System.Drawing.Size(75, 23);
this.buttonAneks.TabIndex = 103;
this.buttonAneks.Text = "Aneksy";
this.buttonAneks.UseVisualStyleBackColor = false;
this.buttonAneks.Click += new System.EventHandler(this.buttonAneks_Click);
(...)
this.Controls.Add(this.buttonAneks);
this.Controls.SetChildIndex(this.buttonAneks, 0);
(...)
private System.Windows.Forms.Button buttonAneks;
Turns out that the problem was the Adobe PDF Reader control axAcroPDF, which was present in this particular form, and for unknown reasons, it caused such behavior. Removing it solved the problem.

Moving a control at the bottom left of the screen at runtime

I made a Windows Forms application with C# and the Form size is 1200x800. There's a Picture Box called RivalCube which moves at runtime at the bottom left using the command RivalCube.Location = new Point(0, 677);. It works fine, but if you make the Application full screen and you start it it places the Picture Box somewhere in the middle of the screen because being Maximized makes it bigger so 0,677 is no longer the bottom left of the screen. So how do I make sure it stays at the bottom even if the application resizes?
I'v tried anchoring it but it still does not work.
Thanks alot !
On the resize event of the form add your code for the picture box's movement
Like this:
private void Form1_Resize(object sender, EventArgs e)
{
RivalCube.Location = new Point(0, 677);
}

How can I add transparency to a c# form while keeping controls visible?

UPDATE: I took a break from messing with the transparency stuff for a few days. I started messing with it again tonight. I got a new result using Hans Passant's solution:
http://img3.imageshack.us/img3/4265/icontransp.jpg
Passant's solution does solve the issue of the transparent background gradient. However, I'm still running into the problem with the transparent colors in my icon blending with the form's BackColor. You can see the fuchsia around various parts of the icon in the above image.
ORIGINAL CONTENT:
I've been going at this for several hours now, and I haven't had much luck. I've messed with Control.Region, Form.TransparencyKey, Form.Opacity, and a couple other random things with some funky effects.
Lately I've been trying to customize my desktop and decided to mess with Application Docks. After seeing what the Mac dock and a few third-party Windows implementations had to offer, I decided I wanted to build my own.
Eventually I want to move on to using the Win32 API. For now I just want to get something working using as much C# and .Net framework capabilities as possible.
There are a few things I want to be able to do in this application:
Display a form/menu with a gradient background.
Allow the form/menu to have transparency while keeping icons opaque.
Display icons that contain transparent backgrounds.
The Menu and Icons should be able to receive mouse-related events (hover, leave, click, dragover, dragdrop, and a few others).
This is the effect I'm shooting for:
http://img207.imageshack.us/img207/5716/desired.jpg
This image shows the visual effects I'm trying to achieve. This was a skin I made for a program called Rainmeter. The image shows Notepad++ behind the skin with a few of the skin's files open in the editor. The menu is transparent, but the icons remain opaque.
My Approach:
Using a Form to act as the menu seemed like a logical first choice to me. I have a basic understanding of events. I'm not quite sure how to create my own click events, so a form would make working with events a tad easier. I considered a few options for the icons. I decided I'd use PictureBoxes for the icons, since they can hold images and receive events.
Once I finished the code for all the structural logic of my menu, I started playing around with it to try to get the visual effect I wanted. Form.Opacity affected the transparency of everything on the form. Since I want the icons to be fully opaque, I left this property alone. I tried setting the BackColor to Color.Transparent, but that gives an error. I played around with a few combinations...
http://img204.imageshack.us/img204/757/effectsi.jpg
I drew the gradient with a Drawing2D.LinearGradientBrush into a Bitmap. This Bitmap was then placed as the Form.BackgroundImage or as a PictureBox.Image. If used, the PictureBox was sized to cover the entire Form and sent to the back.
I noticed that some of the Form.BackgroundColor would be mixed in with the outlines of my icons. The icons have transparency along the edges for a smoother appearance. Since the icons are picking up the Form's BackgroundColor, this makes me think that the PictureBoxes are creating new images when the icons are loaded into the form. The semi-transparent portions of the image are then merged with the Form's BackgroundColor when they should merge with whatever colors are behind the form.
http://img838.imageshack.us/img838/8299/whitedesktop.jpg
In this image you can see the Fuchsia existing in the icons even though the Form's Fuchsia color is now completely transparent. I forgot to point out that the same green to yellow gradient with an Alpha value of 150 was used in every case. In the images where the gradient doesn't look green, it's because the transparent colors are blending with the Fuchsia background.
I'm not really sure what to do from here. I feel like I could get what I want if I could somehow make the Form alone completely transparent. I was also thinking I may have better luck just drawing the icons instead of using PictureBoxes. The problem then would be setting up the icons to receive mouse events. (I've never made my own events, and I think it would involved some Win32 API calls.)
Is there something else I can do with the PictureBoxes to get the effect I want? Whichever the case, I'm open to any ideas or suggestions for the overall effect I'm trying to achieve.
This is pretty easy to do in Winforms. What you need is a sandwich of two forms. The bottom one should provide the transparent gradient background, the top one should draw the icons and handle mouse clicks. Some sample code:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.TransparencyKey = this.BackColor = Color.Fuchsia;
this.Opacity = 0.3;
var overlay = new Form();
overlay.FormBorderStyle = FormBorderStyle.None;
overlay.TransparencyKey = overlay.BackColor = Color.Fuchsia;
overlay.StartPosition = FormStartPosition.Manual;
overlay.Location = this.Location;
overlay.MouseDown += HandleIconClick;
this.Resize += delegate { overlay.Size = this.Size; };
this.LocationChanged += delegate { overlay.Location = this.Location; };
overlay.Paint += PaintIcons;
this.Paint += PaintBackground;
this.Load += delegate { overlay.Show(this); };
}
private void PaintBackground(object sender, PaintEventArgs e) {
var rc = new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);
using (var br = new LinearGradientBrush(rc, Color.Gainsboro, Color.Yellow, 0f)) {
e.Graphics.FillRectangle(br, rc);
}
}
private void PaintIcons(object sender, PaintEventArgs e) {
e.Graphics.DrawIcon(Properties.Resources.ExampleIcon1, 50, 30);
// etc...
}
void HandleIconClick(object sender, MouseEventArgs e) {
// TODO
}
}
Which looks like this with the somewhat random colors and icon I selected:
OK, I got a bit lost in all that, but from the description in the original paragraph, I would make sure the background rectangle is NOT the visual parent of the pictureboxes. Make them overlapping siblings, with the pictureboxes in front using Panel.Zindex.
Then you can just change the opacity of the rectangle, without affecting the icons. Also make sure the icon source image files have a transparent background.
Should work I think.

How can I add a splash screen to my C# application? [duplicate]

How can we show startup picture when my application starts as every software hows like Photoshop ,vs word etc?? I planed to paste it on form and then show it but there is top blue bar coming that has controls etc any idea/
If you're looking for the simplest way, you can use the .NET Framework's excellent built-in support for splash screens. You'll have to put aside any irrational fears that you might have of including something with the name "Visual Basic" in a C# application, but this way will save you from having to roll your own custom solution and worry about things like multi-threading, invoking, and all that. It all compiles down to the same IL in the end anyway. Here's how it works:
Add a reference to Microsoft.VisualBasic to your project.
Add a new form (named something like SplashForm) to serve as your splash screen.
To make it look more like a proper splash screen, set the form's FormBorderStyle property to "None" and its StartPosition property to "CenterScreen". You can add any controls or images to this form that you want to be displayed on the splash screen to this form.
Add the following code to your Project.cs file:
using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;
namespace WindowsFormsApplication1
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new SplashScreenApp().Run(args);
}
}
public class SplashScreenApp : WindowsFormsApplicationBase
{
protected override void OnCreateSplashScreen()
{
this.SplashScreen = new SplashForm();
this.SplashScreen.ShowInTaskbar = false;
this.SplashScreen.Cursor = Cursors.AppStarting;
}
protected override void OnCreateMainForm()
{
//Perform any tasks you want before your application starts
//FOR TESTING PURPOSES ONLY (remove once you've added your code)
System.Threading.Thread.Sleep(2000);
//Set the main form to a new instance of your form
//(this will automatically close the splash screen)
this.MainForm = new Form1();
}
}
}
If you want to do something fancy like create a transparent splash screen in the style of Adobe Photoshop, you can add an alpha-channel PNG image to your project's Resources file, and then add the following code to your splash screen form, replacing splashImage with the path to your embedded image resource:
protected override void OnPaintBackground(PaintEventArgs pevent)
{
Graphics g = pevent.Graphics;
g.DrawImage(splashImage, new Rectangle(0, 0, this.Width, this.Height));
}
protected override void OnPaint(PaintEventArgs e)
{
//Do nothing here
}
For this to work, make sure that you have double buffering turned off, or else you'll get a black background to your form. There's really no reason to double buffer a splash screen anyway.
You can remove all those blue bars etc, via this.FormBorderStyle = FormBorderStyle.None in your Form_Load().
So if I were you, I'd create a Form of specific Size, then set the following in Form_Load()
or directly in the code generated by the designer:
this.FormBorderStyle = FormBorderStyle.None;
this.StartPosition = FormStartPostition.CenterScreen;
Now you have a splash screen like many other apps - all you have to still do is write the code for all the making it visible or not stuff etc :)
WPF 3.5 and above actually has built in splash screen support, for faster rendering of the splash screen and much (much) less faffing with code. See this page for details.
Are you writing a WinForms or a WPF application? You will have to set different properties depending on which kind you write. If you are writing a WPF application, you can add the WindowStyle="None" and ResizeMode="NoResize" attributes to the Window top-level element in XAML. The first one will remove the title bar with the minimize, resize, and close buttons whereas the second one will remove the border around the form.
Now, you have a borderless form that you can modify to create a splash screen if you want or to simply add your startup image. If you don't want your default form to appear, you need to add Background="Transparent" and AllowsTransparency="True" attributes as well. The first one will set the background color to transparent whereas the second one allows your program to look transparent. Now, you can add any image in any shape, and the user will see only that image at program startup.
P.S. Make sure to load another form once the startup screen has been shown for a set amount of time or once the methods that are supposed to "load" stuff return control.
Pretty simple stuff!!

Load image in PictureBox before make control visible

I have a form with black background, with 9 picture boxes.
When the program starts, I want to show 9 images using these picture boxes.
However, the picture boxes take time to load the picture. It is quite ugly that the picture boxes show up first while waiting.
Is there a way I can move from blank black screen to straightaway 9 images, without the visible loading in between?
Thanks.
How are you loading your Form (I guess you are talking about Windows.Forms here)?
You can just create a new Window class and load your pictures and then after all is done call the Show method.
MyForm form = new MyForm ();
form.DoLoadImages ();
form.Show ();
Or you can just set the WaitOnLoad property of the PictureBox to true.
private void startButton_Click(object sender, EventArgs e)
{
// Ensure WaitOnLoad is false.
pictureBox1.WaitOnLoad = false;
// Load the image asynchronously.
pictureBox1.LoadAsync(#"http://localhost/print.gif");
}
Courtesy of MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.waitonload(v=VS.100).aspx1

Categories