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

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.

Related

Toast notification does not disappear after clicking outside of it in .net WPF application

I am working on a .NET WPF project using Visual Studio 2022 and I added tray icon functionality to my app. I also show toast notification whenever my app is minimized to windows tray. Everything works fine except when I click outside border of toast notification, it does not disappear. I think it is a focus issue but can not able to overcome that. Any idea how to solve this?
My code:
public NotifyIcon m_notifyIcon;
public MainWindow()
{
InitializeComponent();
m_notifyIcon = new NotifyIcon();
m_notifyIcon.BalloonTipText = "The app has been minimised. Click the tray icon to show.";
m_notifyIcon.BalloonTipTitle = "The App";
m_notifyIcon.Text = "The App";
m_notifyIcon.DoubleClick += new EventHandler(m_notifyIcon_Click);
m_notifyIcon.MouseDown += new MouseEventHandler(Notifier_MouseDown);
Hide();
if (m_notifyIcon != null)
m_notifyIcon.ShowBalloonTip(2000);
CheckTrayIcon();
}
}
private WindowState m_storedWindowState = WindowState.Normal;
void OnStateChanged(object sender, EventArgs args)
{
if (WindowState == WindowState.Minimized)
{
Hide();
if (m_notifyIcon != null)
m_notifyIcon.ShowBalloonTip(2000);
}
else
m_storedWindowState = WindowState;
}
void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs args)
{
CheckTrayIcon();
}
void m_notifyIcon_Click(object sender, EventArgs e)
{
Show();
WindowState = m_storedWindowState;
}
void CheckTrayIcon()
{
ShowTrayIcon(true);
}
void ShowTrayIcon(bool show)
{
if (m_notifyIcon != null)
m_notifyIcon.Visible = show;
}
void Notifier_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenu menu = (ContextMenu)this.FindResource("NotifierContextMenu");
menu.IsOpen = true;
IntPtr handle = ((HwndSource)PresentationSource.FromVisual(menu)).Handle;
ApiHelper.SetForegroundWindow(handle);
}
}

Old form still display winforms c#

I implement a form for handle excel file when click button "Start".
Event click Start button:
private void btnImport_Click(object sender, EventArgs e)
{
showFormSelectLanguage();
if (CheckSheetFile() == true) {
using (WaitingForm frm = new WaitingForm(handleExcel))
{
frm.ShowDialog(this);
}
var dialogMessage = new DialogMessage();
dialogMessage.ShowDialog(this);
} else
{
ShowDialogNotFoundSheet();
}
}
showFormSelectLanguage method display dialog for select language:
private void showFormSelectLanguage()
{
var formSelectLanguage = new FormSelectLanguage();
formSelectLanguage.ShowDialog(this);
}
ShowDialogNotFoundSheet function for check sheet excel exist:
private void ShowDialogNotFoundSheet()
{
var dialogNotFoundSheet = new DialogNotFoundSheet();
dialogNotFoundSheet.setTextContent("Not found sheet");
dialogNotFoundSheet.ShowDialog(this);
}
Event click confirm select language button at Select language form:
private void btnConfirmLanguage_Click(object sender, EventArgs e)
{
//close dialog
this.Close();
}
Event click Close button for close DialogNotFoundSheet form:
private void btnCloseDialogNotFoundSheet_Click(object sender, EventArgs e)
{
this.Close();
}
CheckSheetFile method:
private bool CheckSheetFile()
{
var isCorrectFile = false;
try
{
xlWorkBook = xlApp.Workbooks.Open(txtFilePath.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read);
var xlWorkBook1 = xlWorkBook.Sheets["SheetName"];
isCorrectFile = true;
}
catch (Exception e)
{
return false;
}
return isCorrectFile;
}
Issue:
When I click Close button at DialogNotFoundSheet form. Then FormSelectLanguage from still display. It repeats. How can resolve it?
Expected 2 forms can close
Thanks!
Update:
All References btnImport_Click:
UI:
I don't exactly know what you did with btnImport_Click, but if your purpose is to disable the function of a button at a time and to enable it at another time, actually you don't have to register or unregister the click event, you can simply set button's Enabled propety.
//btnImport.Click += btnImport_Click;
btnImport.Enabled = true;
//btnImport.Click -= btnImport_Click;
btnImport.Enabled = false;
My guess of the reason of this loop is that you have called += btnImport_Click many times, but -= btnImport_Click is never (or less) run.
For instance if you do:
btnImport.Click += btnImport_Click;
btnImport.Click += btnImport_Click;
Each time btnImport is clicked, btnImport_Click will get invoked twice.

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);
}

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;
}
}

Minimize to system tray

i was following the instruction on page
but then, there's no icon attached for the application, so after the form is hidden, i cannot reshow the form, since there's no icon on the system tray,
how do i resolve this ?
here is my code
private void Form1_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
cmd.cetakSukses(ident.judul_App + " Diperkecil ke dalam System Tray");
notifyIcon1.BalloonTipText = ident.judul_App + " Diperkecil ke dalam System Tray";
notifyIcon1.BalloonTipTitle = ident.judul_App;
notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else
{
}
}
update :
i have attached the icon, and the icon still not showing on the system tray
and i figured how to make the form showing, i need to add the following code to notifyicon
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
}
You can set the notify icon at design using the Properties sheet:
Or you can add/change the icon property at runtime using the following code:
notifyIcon1.Icon = new Icon("appicon.ico");
This is how i implemented through code behind for a WPF app.
System.Windows.Forms.NotifyIcon m_NotifyIcon;
public StartWindow()
{
InitializeComponent();
m_NotifyIcon = new System.Windows.Forms.NotifyIcon();
m_NotifyIcon.Icon = new System.Drawing.Icon(IconPath);
m_NotifyIcon.Visible = true;
m_NotifyIcon.BalloonTipTitle = "Tip here";
m_NotifyIcon.Text = "Text here";
m_NotifyIcon.DoubleClick += delegate(object sender, EventArgs args)
{
this.Show();
this.WindowState = WindowState.Normal;
};
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
try
{
if (m_NotifyIcon != null)
m_NotifyIcon.Dispose();
}
catch { }
base.OnClosing(e);
}
protected override void OnStateChanged(EventArgs e)
{
if (WindowState == WindowState.Minimized)
this.Hide();
base.OnStateChanged(e);
}
You need to give you application an icon either by using visual studio or programatically.
You can do it in VS by going to the project properties and selecting the application tab
Or you can set it at runtime if you have icon files attached to your project already.
private NotifyIcon appIcon = new NotifyIcon();
appIcon.Icon = new System.Drawing.Icon("myApp.ico");

Categories

Resources