WebBrowser Click-Event - c#

I want to program a screensaver and since I already have the HTML5 Code I just want to use a WebBrowser Control which renders the content. However I can't figure out how to exit the application on events like "Click", "Keydown", etc. WebBrowser class doesn't seem to know those events. Can anyone tell me how I can make my application exit?
Here's some code:
public Screensaver()
{
InitializeComponent();
this.Load += Screensaver_Load;
StartBrowser();
}
private void StartBrowser()
{
this.webBrowser1.DocumentText = Content;
}
private void Screensaver_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
this.Bounds = Screen.PrimaryScreen.Bounds;
Cursor.Hide();
this.TopMost = true;
this.BackColor = Color.White;
this.webBrowser1.Size = new System.Drawing.Size(this.ClientSize.Width, this.ClientSize.Height);
}
private String Content
{
get
{
return "<html>..[long html code following]..</html>";
}
}

you can use javascript function to close current application in browser for example:
close

Related

Opening child form makes that the UI controls get reset

I am a beginner at windows forms, and I am having some issues with opening child forms.
My application has 2 buttons, one that goes to the main menu, and another that goes to the options.
The problem is that, if I click on a checkbox in the options menu and leave the options tab and come back, the checkbox will not be checked anymore.
This is my code:
private Form CurrentChildForm;
private void OpenChildForm(Form childForm)
{
if(CurrentChildForm != null)
{
CurrentChildForm.Visible = false;
}
CurrentChildForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
PanelForForm.Controls.Add(childForm);
PanelForForm.Tag = childForm;
childForm.BringToFront();
childForm.Show();
}
private void MainMenu_Click(object sender, EventArgs e)
{
OpenChildForm(new MenuForm());
}
private void OptionsMenu_Click(object sender, EventArgs e)
{
OpenChildForm(new OptionsForm());
}
through your Click-Events on the different buttons you are always creating a new instance of your Forms.
A possible solution is to cache the instance of your optionsMenu for example through a private field, because I consider it being SingleInstance.
private Form CurrentChildForm;
private OptionsForm _opForm;
private void OptionsMenu_Click(object sender, EventArgs e)
{
if (_opForm == null)
{
_opForm = new OptionsForm();
}
OpenChildForm(_opForm);
}

AxMSTSCLib display disappear in taskbar after minimize from full-screen mode

I am using AxMSTSCLib to develop a Windows App for creating RDP connections.
The steps listed below caused my remote desktop display disappear:
Start an RDP connection with full-screen mode
Click "restore down" button from connection bar
Click "maximize" button again to re-enter full-screen mode
Click "minimize" button
Then my app disappears, I can't see it in taskbar (but still be listed / running in taskmanager)
If I skip steps 2 & 3, it won't disappear when I click "minimize" button from connection bar, it's really weird.
I post some part of my code here, hope anyone can help me to find out the problem.
public partial class RdpShowForm : Form
{
public AxMSTSCLib.AxMsRdpClient9NotSafeForScripting oRemote;
public RdpShowForm()
{
InitializeComponent();
oRemote = new AxMSTSCLib.AxMsRdpClient9NotSafeForScripting();
((System.ComponentModel.ISupportInitialize)(this.oRemote)).BeginInit();
oRemote.Dock = System.Windows.Forms.DockStyle.Fill;
oRemote.Enabled = true;
oRemote.Name = "WindowsVM";
this.Controls.Add(oRemote); // Controls contains 'panel1' and 'oRemote'
((System.ComponentModel.ISupportInitialize)(this.oRemote)).EndInit();
oRemote.CreateControl();
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
}
private void RdpShowForm_Load(object sender, EventArgs e)
{
NewRdpConnectionInstance();
}
private void NewRdpConnectionInstance()
{
oRemote.Server = 'xxx';
...
oRemote.FullScreen = true;
oRemote.AdvancedSettings7.DisplayConnectionBar = true;
oRemote.AdvancedSettings7.ConnectionBarShowMinimizeButton = true;
oRemote.AdvancedSettings7.ConnectionBarShowRestoreButton = true;
oRemote.OnConnected += rdpClient_OnConnected;
oRemote.OnLeaveFullScreenMode += rdpClient_OnLeaveFullScreenMode;
oRemote.Connect();
oRemote.Show();
oRemote.Focus();
}
private void rdpClient_OnConnected(object sender, EventArgs e)
{
this.panel1.Visible = false;
this.Visible = false;
}
private void rdpClient_OnLeaveFullScreenMode(object sender, EventArgs e)
{
this.Visible = true;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SYSCOMMAND)
{
if (m.WParam.ToInt32() == MAXIMIZE)
{
this.Visible = false;
oRemote.FullScreen = true;
oRemote.Show();
}
}
base.WndProc(ref m);
}
}
I've tried some methods, but none of them worked.
oRemote.Bounds = Screen.PrimaryScreen.WorkingArea;
oRemote.IsAccessible = true;
this.ShowInTaskbar = true;
or
this.Visible = ture; // do not hide the main form
or
if (m.WParam.ToInt32() == MAXIMIZE)
{
oRemote.FullScreen = true;
oRemote.Show();
oRemote.Update();
oRemote.Refresh();
}
So far, the only solution that came into my mind is to disable the restore button, like this:
oRemote.AdvancedSettings7.ConnectionBarShowRestoreButton = false;
This way isn't fix the problem, just avoid the issue to be triggered.
I would appreciate any help.
Since AxMSTSCLib has a restore down issue, I turn to use a form to handle its fullscreen mode. In this way, I can get extensive control over full-screen mode behavior, write code to tell the container what to do when the restore down button is called.
// enable container-handled full-screen mode
oRemote.AdvancedSettings7.ContainerHandledFullScreen = 1;
Then, customize OnRequestGoFullScreen, OnRequestLeaveFullScreen, OnRequestContainerMinimize.
oRemote.OnRequestGoFullScreen += corey_OnRequestGoFullScreen;
oRemote.OnRequestLeaveFullScreen += corey_OnRequestLeaveFullScreen;
oRemote.OnRequestContainerMinimize += corey_OnRequestContainerMinimize;
private void corey_OnRequestGoFullScreen(object sender, EventArgs e)
{
// set the form to fullscreen
}
private void corey_OnRequestLeaveFullScreen(object sender, EventArgs e)
{
// set the form back to non-fullscreen mode
}
private void corey_OnRequestContainerMinimize(object sender, EventArgs e)
{
// minimize the form
}
The method I posted here can be a workaround to this issue.

Keep Popup open and active as long as a TextBox has keyboard focus

I have a Popup that I want to always be open and its content active when a TextBox has keyboard focus. I have attempted this with this code
public partial class MyPopup : Popup
{
public MyPopup
{
InitializeComponent();
EventManager.RegisterClassHandler(
typeof(UIElement),
Keyboard.PreviewGotKeyboardFocusEvent,
(KeyboardFocusChangedEventHandler)OnPreviewGotKeyboardFocus);
}
private void OnPreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (sender is TextBox)
this.IsOpen = true;
else
this.IsOpen = false;
}
}
were I create the Popup in the constructor of App.
The problem with this code is that if the Popup is already open when ShowDialog is used the Popup is no longer active, even though it is still visually on top.
How do I get around this or get the required behavior in another way.
Found one solution where I check if the Window is opening by checking if it has loaded. If so I close the popup and reopen it again after the new Window has had its content rendered.
Not sure it's something I trust enough to use to use so a better solution would be welcome.
private void OnGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
e.Handled = true;
if (sender is TextBox)
{
var _parentWindow = Window.GetWindow((UIElement)sender);
if (!_parentWindow.IsLoaded)
{
this.IsOpen = false;
_parentWindow.ContentRendered += (o, i) => this.IsOpen = true;
}
else
{
this.IsOpen = true;
}
}
else
{
this.IsOpen = false;
}
}

Bring already opened winform application to front without API?

Hi,
Say that we got a WinForm application(app1) running in the background, now another application(app2)(the topmost active application) trigger a startProcess with the app1.
Now I need app1 to use the existing instance and bring it to topmost application(not only within the app1 application).
I have found this : http://sanity-free.org/143/csharp_dotnet_single_instance_application.html
Is it true that its not possible to do this without API? I have looked att bringToFront, Activate and Focus but all these does seem to only effect within a application and not between applications?
I don't know what you mean "without API" or why that matters.
However the simplest way is via WindowsFormsApplicationBase. It gives you all you need, with just a few lines of code.
You need to add a reference to the Microsoft.VisualBasic assembly - but it can be used through C#.
Make this class:
public class SingleInstanceApplication : WindowsFormsApplicationBase
{
private SingleInstanceApplication()
{
IsSingleInstance = true;
}
public static void Run(Form form)
{
var app = new SingleInstanceApplication
{
MainForm = form
};
app.StartupNextInstance += (s, e) => e.BringToForeground = true;
app.Run(Environment.GetCommandLineArgs());
}
}
And in your Program.cs, change the run line to use it:
//Application.Run(new Form1());
SingleInstanceApplication.Run(new Form1());
You really need some sort of communications between 2 apps. In article link to you posted communications is through WinApi messages. Also you can do that through sockets or through files and FileWatchers.
UPD1:
Code to simulate minimize with timer simulation message from another app and maximize on that message:
public partial class Form1 : Form
{
private Timer _timer = null;
public Form1()
{
InitializeComponent();
this.Load += OnFormLoad;
}
private void OnFormLoad(object sender, EventArgs e)
{
Button btn = new Button();
btn.Text = "Hide and top most on timer";
btn.Width = 200;
btn.Click += OnButtonClick;
this.Controls.Add(btn);
}
private void OnButtonClick(object sender, EventArgs e)
{
//minimize app to task bar
WindowState = FormWindowState.Minimized;
//timer to simulate message from another app
_timer = new Timer();
//time after wich form will be maximize
_timer.Interval = 2000;
_timer.Tick += new EventHandler(OnTimerTick);
_timer.Start();
}
private void OnTimerTick(object sender, EventArgs e)
{
_timer.Stop();
//message from another app came - we should
WindowState = FormWindowState.Normal;
TopMost = true;
}
}

How to find master form and attach child

I use MDI in MainForm.cs
public Le_MainForm()
{
InitializeComponent();
this.IsMdiContainer = true;
this.Name = "MainUSER";
}
private void barButtonItem_ListeOrdres_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Close_AllForm();
Liste_Ordres f_Liste = new Liste_Ordres();
f_Liste.MdiParent = this;
f_Liste.Show();
}
private void barButtonItem_CreatOrdreAller_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Close_AllForm();
Program.AllerRetour = "Ordre Aller";
Fiche_Ordre f_Fiche = new Fiche_Ordre();
f_Fiche.MdiParent = this;
f_Fiche.Show();
}
the question now, when I am in other form Ex.:Liste_Ordres.cs or Fiche_Ordre.cs how can i redirect from Fiche_Ordre.cs into Liste_Ordres.cs and vice versa without loosing MDI ?
When I'm in Fiche_Ordres.cs to go to Liste_Ordres.cs I use:
private void simpleButton_Annluer_Click_1(object sender, EventArgs e)
{
Liste_Ordres f_Liste = new Liste_Ordres();
f_Liste.Show();
this.Close();
}
But as you can see I lose the MDI, that mean when I click the menu on MainForm, the Liste_Ordres form will disappear.
as you can see in this Video that when i redirect From Liste to fiche, and then maximize the window i lose the menu that mean i lose Mdi.
You just need to set the MdiParent again:
f_Liste.MdiParent = this.MdiParent;

Categories

Resources