I'm developing a windows form that opens a web application page.
I'm obliged to use the COM AxWebBrowser for this goal.
the problem is when a window.Open() action is executed in the web page, the new page is opened in my axWebBrowser and another blank page is shown .I need to force the new window to open in the blank page without affecting the main page.
I tried the code below but it doesn't work fine:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyProject.MyNamespace
{
public partial class PopupWindow : Form
{
#region Fields
private PopupWindow _popupWindow;
#endregion
#region Ctr
public PopupWindow()
{
this.InitializeComponent();
this.axWebBrowser.Silent = true;
this.axWebBrowser.NewWindow3 += new AxSHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(axWebBrowser_NewWindow3);
this.FormClosing += PopupWindow_FormClosing;
this.Shown += new EventHandler(PopupWindow_Shown);
}
private void axWebBrowser_NewWindow3(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow3Event e)
{
this._popupWindow = null;
this._popupWindow = new PopupWindow();
this._popupWindow.WindowState = FormWindowState.Maximized;
this._popupWindow.AXWebBrowser.RegisterAsBrowser = true;
e.ppDisp = this._popupWindow.AXWebBrowser.Application;
this._popupWindow.Visible = true;
}
#endregion
#region Properties
public AxSHDocVw.AxWebBrowser AXWebBrowser
{
get { return this.axWebBrowser; }
}
#endregion
#region Methods
private void PopupWindow_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Hide();
}
private void PopupWindow_Shown(object sender, EventArgs e)
{
this.Size = new Size(this.Width + 2, this.Height + 2);
}
#endregion
}
}
Thank you for your help!
Related
This is my current code: https://hastebin.com/ifejusezat.cs
This is a screenshot of what I see: https://gyazo.com/2ee9b1210649ca8ec61e3fa7e645a286
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BrowserView browserView = new WinFormsBrowserView();
Controls.Add((Control)browserView);
browserView.Browser.LoadURL("http://www.google.com");
}
private void Form1_Load(object sender, EventArgs e)
{
BrowserView browserView = new WinFormsBrowserView(BrowserFactory.Create());
Control browserWindow = (Control)browserView;
browserWindow.Dock = DockStyle.Fill;
Controls.Add(browserWindow);
}
}
Updated answer: This is my complete Form1.cs (taken directly from the teamdev website), and it works (see image) without needing to resize.
using DotNetBrowser;
using DotNetBrowser.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormsSampleCSLightweight
{
public partial class Form1 : Form
{
private readonly BrowserView browserView;
public Form1()
{
InitializeComponent();
browserView = new WinFormsBrowserView(BrowserFactory.Create(BrowserType.LIGHTWEIGHT));
Controls.Add((Control)browserView);
browserView.Browser.LoadURL("http://www.google.com");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (browserView != null)
{
browserView.Dispose();
browserView.Browser.Dispose();
}
}
}
}
Original answer:
You can try the UpdateSize method on your browser view.
browserView.UpdateSize(someWidth, someHeight);
Or you can try to size the document that is viewed.
public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//For the case when the control's Dock property is DockStyle.Fill
this.Width = WebBrowser.Document.Body.ScrollRectangle.Width + 40; //40 is for border
this.Height = WebBrowser.Document.Body.ScrollRectangle.Height + 40; //40 is for border
//For the case when the control is not docked
WebBrowser.Size = WebBrowser.Document.Body.ScrollRectangle.Size;
}
If your are using DotNetBrowser 1.9 use the control properties to set Dock to DockFill
Control browserControl = (Control)browserView;
browserControl.Dock = DockStyle.Fill;
Controls.Add(browserControl);
I created a new form i tried before with a new class but the same problem.
I created a new form and added:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Capture
{
public partial class Hook_Keys : Form
{
globalKeyboardHook gkh = new globalKeyboardHook();
public bool onoff = false;
public Hook_Keys()
{
InitializeComponent();
}
private void Hook_Keys_Load(object sender, EventArgs e)
{
gkh.HookedKeys.Add(Keys.A);
gkh.HookedKeys.Add(Keys.B);
gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
gkh.KeyUp += new KeyEventHandler(gkh_KeyUp);
}
void gkh_KeyUp(object sender, KeyEventArgs e)
{
onoff = true;
e.Handled = true;
}
void gkh_KeyDown(object sender, KeyEventArgs e)
{
onoff = false;
e.Handled = true;
}
}
}
Then in another class at the top i did:
Hook_Keys hookeys = new Hook_Keys();
Then using it:
if (hookeys.onoff == true)
{
font.DrawText(null, String.Format("{0:N0} fps", this.FPS.GetFPS()), 5, 5, SharpDX.Color.Red);
string timeRunning = FramesPerSecond.RunTime.ToString();
font.DrawText(null, String.Format("{0:N0} time running ", timeRunning), 5, 30, SharpDX.Color.Red);
}
The problem is that it's never getting to the events gkh_KeyUp and gkh_KeyDown in the new form.
I also must say that the project i'm trying to use it now is a class library project type.
I tried to use it before on a simple windows forms application without any classes or new forms just windows forms application in form1 and it's working fine.
I took the source code from here:
Global Keys Hooking
There's no code problem, the debug doesn't have any problem but when I test, when I checked the checkbox the opacity doesn't change. Nothing happen. I'm using VisualStudio 2013 Express. Here's the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TP3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
int carac = textBox1.Text.Length;
label2.Text = carac.ToString();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Form Form1;
Form1 = new Form1();
if(checkBox1.Checked == true)
{
Form1.Opacity = 1;
}
}
}
}
The code doesn't seem to do anything right. You are creating a new instance of Form in the checkBox1_CheckedChanged method (why do you create a new form?), you set the Opacity property on the new form, but you do not show the form any way. You need to call Show() / ShowDialog() on the Form1 to show it.
If you wish to change the opacity of the current form you can do it this way:
this.Opacity = 1;
And a call like without this would work as well:
Opacity = 1;
i create a simple message box to get user input and set the result into webbrowser of previous form.
this is my MsgInput source code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
public partial class MsgInput : Form
{
private readonly Main mainForm;
public string input_type;
string script;
public MsgInput()
{
this.mainForm = mainForm;
InitializeComponent();
}
private void MsgInput_Load(object sender, EventArgs e)
{
if (input_type == "echo")
{
richTextBox1.Text = "Echo : void echo ( string $arg1 [, string $... ] )";
}
}
private void btnOk_Click(object sender, EventArgs e)
{
if (input_type == "echo")
{
script = mainForm.webBrowser1.DocumentText;
if (chkNewLine.Checked == true)
{
script += "\n";
}
script += "echo " + txtInput.Text;
mainForm.webBrowser1.DocumentText = script;
this.Close();
}
}
}
and i not add anything in first form just set the webbrowser modifiers to public.
when i debug. null return when i try to submit an text
Main Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace EasyPHP
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
private void echoToolStripMenuItem_Click(object sender, EventArgs e)
{
var msg = new MsgInput();
msg.input_type = "echo";
msg.Show();
}
private void Main_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = "<pre>";
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
}
}
I am assuming you are getting a null reference exception. it will be useful if you post the main form code. From what i understood, you should be passing in the main form reference and that's why you are getting a null reference.
In your code, change the constructor as below (ParentForm is whatever is the class name of your parent form)
public MsgInput(ParentForm mainForm)
{
this.mainForm = mainForm;
InitializeComponent();
}
and in the main form
MsgInput frm = new MsgInput(this);
frm.input_type = "echo";
frm.Show(this);
Else please share the full code and we can quickly help
I am creating a C# WinForms MDI application. I have a main form which contains 4 other forms inside it. I want to be able to move the child forms outside of the parent form (their FormBorderStyle value is set to sizeable toolbar so that separate windows don't appear on the taskbar for each child window). I am able to accomplish this by using the following code for the main form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Prototype
{
public partial class MdiParent : Form
{
private FastEyeControl m_ControlForm;
private FastEyeLogWindow m_LogForm;
private FastEyeConfiguration m_ConfigurationForm;
private ROCOrderWindow m_OrderLogForm;
public MdiParent()
{
InitializeComponent();
m_ControlForm = new FastEyeControl();
m_LogForm = new FastEyeLogWindow();
m_ConfigurationForm = new FastEyeConfiguration();
m_OrderLogForm = new ROCOrderWindow();
}
private void MdiParent_Load(object sender, EventArgs e)
{
m_ControlForm.Show(this);
m_LogForm.Show(this);
m_ConfigurationForm.Show(this);
m_OrderLogForm.Show(this);
}
}
}
However, when I minimize the parent form, all child forms get minimized as well (as expected). Is there anyway to prevent any child forms that are outside of the parent window to be minimized when the parent window is minimized? Basically, I want the user to be able to resize and move the individual child forms outside of the parent form if desired (like undocking a toolbar in Visual Studio.NET and placing it in another monitor somewhere). Thanks for your help!
I think you will have to move away from owned/parented/mdi windows, and will instead have to make them all unowned top-level windows, in "parallel" to each other. And then write your own logic for docking one window within another.
You haven't actually created an MDI application, you don't set the child forms' MdiParent property. Should be clearly visible, you can move the child form outside of the bounds of the main form.
You made them owned windows by using the Show(owner) overload. Which means that they will be always on top of the main window. And get minimized too when you minimize the main window.
Simply call the Show() method instead (no argument).
OK I got it to work:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace Prototype
{
public partial class MdiParent : Form
{
private FastEyeControl m_ControlForm;
private FastEyeLogWindow m_LogForm;
private FastEyeConfiguration m_ConfigurationForm;
private ROCOrderWindow m_OrderLogForm;
private Point m_ControlFormLocation;
private Point m_LogFormLocation;
private Point m_ConfigurationFormLocation;
private Point m_OrderLogFormLocation;
public MdiParent()
{
InitializeComponent();
m_ControlForm = new FastEyeControl();
m_LogForm = new FastEyeLogWindow();
m_ConfigurationForm = new FastEyeConfiguration();
m_OrderLogForm = new ROCOrderWindow();
m_ControlFormLocation = new Point(0, 25);
m_LogFormLocation = new Point(0, 405);
m_ConfigurationFormLocation = new Point(550, 25);
m_OrderLogFormLocation = new Point(0, 630);
}
private void MdiParent_Load(object sender, EventArgs e)
{
DockForm(m_ControlForm, m_ControlFormLocation);
DockForm(m_LogForm, m_LogFormLocation);
DockForm(m_ConfigurationForm, m_ConfigurationFormLocation);
DockForm(m_OrderLogForm, m_OrderLogFormLocation);
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void DockForm(Form form, Point location)
{
form.TopLevel = false;
form.Location = location;
if (! this.Controls.Contains(form))
{
this.Controls.Add(form);
}
form.Visible = true;
}
private void UndockForm(Form form)
{
if (this.Controls.Contains(form))
{
this.Controls.Remove(form);
}
form.TopLevel = true;
form.Visible = true;
}
private void DockOrUndockForm(Form form, Point location)
{
if (this.Controls.Contains(form))
{
UndockForm(form);
}
else
{
DockForm(form, location);
}
}
private void ToggleDockingOrDockForm(Form form, Point location)
{
if (form.Visible)
{
DockOrUndockForm(form, location);
}
else
{
DockForm(form, location);
}
}
private void fastEyeControlToolStripMenuItem_Click(object sender, EventArgs e)
{
ToggleDockingOrDockForm(m_ControlForm, m_ControlFormLocation);
}
private void fastEyeLogToolStripMenuItem_Click(object sender, EventArgs e)
{
ToggleDockingOrDockForm(m_LogForm, m_LogFormLocation);
}
private void fastEyeConfigurationToolStripMenuItem_Click(object sender, EventArgs e)
{
ToggleDockingOrDockForm(m_ConfigurationForm, m_ConfigurationFormLocation);
}
private void rOCOrderLogToolStripMenuItem_Click(object sender, EventArgs e)
{
ToggleDockingOrDockForm(m_OrderLogForm, m_OrderLogFormLocation);
}
}
}
Is this code safe?