How to move window using custom titlebar in MAUI Blazor - drag

I have a problem with move application.
Window movement works fin, but releasing the button does not work, it moves the window all the time after releasing the left mouse button.
Titlebar razor
<div #onpointerdown="() => topbarVM.MouseDown()" #onpointerup="() => topbarVM.MouseUp()" class="flex-row fixed-top p-0 m-0">
Code
public void MouseDown()
{
isDown = true;
DragWindow();
}
public void MouseUp()
{
isDown = false;
}
public void DragWindow()
{
#if WINDOWS
var mauiWindow = App.Current.Windows.First();
var nativeWindow = mauiWindow.Handler.PlatformView;
IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow);
WindowId WindowId = Win32Interop.GetWindowIdFromWindow(windowHandle);
AppWindow appWindow = AppWindow.GetFromWindowId(WindowId);
while(isDown)
{
var cursor = GetCursorPosition();
appWindow.Move(new Windows.Graphics.PointInt32(cursor.X - 40, cursor.Y - 20));
};
#endif
}

Put a breakpoint inside MouseUp. Is it ever reached? If not, then your code is keeping MainThread (UI thread) busy, so MouseUp event cannot get processed.
One way to code this (in any UI framework) is to do ONE call on the MouseMove event -- not a loop:
public void MouseMove() {
if (isDown)
{
var mauiWindow ...
...
var cursor = GetCursorPosition();
appWindow.Move(...
}
}
Reference MouseMove() from an appropriate event in your Blazor code. I don't know Blazor, so I don't show that.
Explanation: after the Move is done, MouseMove returns. This allows internal OS code to process the next event.

Related

When to start the WPF Progress Bar

I want my app to show a running progress bar while doing some components checking. However, due to my lack of knowledge in Desktop app programming and WPF, I cannot find suitable place for it.
I tried to show the incrementing the progress Bar during the Window_Loaded(), ContentRendered() but with no luck.
Instead of showing the progressBar increases, it just show the final state of the progress Bar.
Here is the code
public partial class Loading : Window
{
public Loading()
{
InitializeComponent();
SetProgressBar();
this.Show();
CheckComponents();
}
private void CheckComponents()
{
System.Threading.Thread.Sleep(3000);
CheckProductionDBConnection();
pgrsBar.Value = 30;
System.Threading.Thread.Sleep(3000);
CheckInternalDBConnection();
pgrsBar.Value = 60;
System.Threading.Thread.Sleep(3000);
CheckProductionPlanning();
pgrsBar.Value = 90;
//MainWindow mainWindow = new MainWindow();
//mainWindow.Show();
}
private void SetProgressBar()
{
pgrsBar.Minimum = 0;
pgrsBar.Maximum = 100;
pgrsBar.Value = 0;
}
//more code down here...
Where should I put the CheckComponents() method?
You could put this code in an event handler subscribed to the Activated event. The one catch with this is that the Activated event is fired every time the window receives focus after having lost it. To get around this, the first thing you can do in your event handler is unsubscribe from the Activated event so that your code is executed only the first time the window is activated.
You also need to offload this work to a worker thread if you don't want the delay to block the main thread. If you do that, you'll have to invoke your calls to update the progess bar's value.
Here's some sample code to get you started:
public Loader()
{
InitializeComponent();
SetProgressBar();
this.Activated += OnActivatedFirstTime;
}
private void OnActivatedFirstTime(object sender, EventArgs e)
{
this.Activated -= this.OnActivatedFirstTime;
ThreadPool.QueueUserWorkItem(x =>
{
System.Threading.Thread.Sleep(3000);
CheckProductionDBConnection();
this.Dispatcher.BeginInvoke(new Action(() => pgrsBar.Value = 30));
System.Threading.Thread.Sleep(3000);
CheckInternalDBConnection();
this.Dispatcher.BeginInvoke(new Action(() => pgrsBar.Value = 60));
System.Threading.Thread.Sleep(3000);
CheckProductionPlanning();
this.Dispatcher.BeginInvoke(new Action(() => pgrsBar.Value = 90));
});
}
private void SetProgressBar()
{
pgrsBar.Minimum = 0;
pgrsBar.Maximum = 100;
pgrsBar.Value = 0;
}

How to ignore user clicks in WinForms?

When a user clicks a button, it starts some task. I don't want to block the main application thread, so I run it in a separate thread. Now I need to forbid a user to click the button until my task finishes.
I could set
button.Enabled = false;
, but I'm looking for some way to ignore clicks on it.
I could add some check in click event handler:
if (executingThread != null) return;
, but I will have to do it for each handler which is bad idea.
I know that there is some way to filter user's messages. Could you point me how to do this? And I don't want to filter out all messages, because some other buttons must stay clickable, I need to filter out messages that come to particular controls (buttons,grids and etc).
SOLUTION
internal class MessagesFilter: IMessageFilter
{
private readonly IntPtr ControlHandler;
private const int WM_KEYUP = 0x0101;
public MessagesFilter(IntPtr ControlHandler)
{
this.ControlHandler = ControlHandler;
}
#region IMessageFilter Members
public bool PreFilterMessage(ref Message m)
{
// TODO: Add MessagesFilter.PreFilterMessage implementation
if (m.Msg == WM_KEYUP)
{
if (m.HWnd == ControlHandler)
{
Keys k = ((Keys) ((int) m.WParam));
if (k == Keys.Enter)
return true;
}
}
return false;
}
#endregion
}
As always, the UI should be presented in such a way that user understands what the application is doing and should talk to the user with UI elements.
As Adam Houldsworth suggested I would also prefer keeping the button either disabled or enabled but I would also suggest that the caption of the button should convey the message to the user that the long processing is in progress when the new thread starts..and so the caption of the button should be immediately changed to something like "Processing..Please wait..." (in addition to being disabled or even if you want to keep it enabled), and then if you have kept the button enabled just check the caption of the button (or a isProcessing bool flag) on its click event to return if it says "Processing..Please wait..." or (isProcessing == true).
Lots of the Websites which help users to upload files/images change the Upload button's caption to "Uploading..Please wait..." to inform the user to wait until the upload finishes and additionally some sites also disable the upload button so that the user is not able to click again on Upload button.
You would need to also revert back the caption to normal when the thread finishes long processing.
There may be other advanced ways but the idea is to keep it as simple and basic as possible.
Look at this example on Threading in Windows Forms which shows to disable the button while multi-threading.
+1 for all the suggestions so far. As CSharpVJ suggests - My idea was to additionally inform the user by changing the button's caption making the UI design more intuitive
This can be achieved elegantly with Backgroundworker component in Winforms [No hassles code]. Just copy-paste and HIT F5 (After creating a New Winforms Project with a Button and a Label on it)!
You do not have to check anything related to button here. Everything will be taken care by the appropriate event handlers. its just that you have to do correct stuffs int he resepctive event handlers. Try it !
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
private BackgroundWorker _worker;
public Form3()
{
InitializeComponent();
InitWorker();
}
private void InitWorker()
{
if (_worker != null)
{
_worker.Dispose();
}
_worker = new BackgroundWorker
{
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
_worker.DoWork += DoWork;
_worker.RunWorkerCompleted += RunWorkerCompleted;
_worker.ProgressChanged += ProgressChanged;
}
/// do time consuming work here...
void DoWork(object sender, DoWorkEventArgs e)
{
int highestPercentageReached = 0;
if (_worker.CancellationPending)
{
e.Cancel = true;
}
else
{
double i = 0.0d;
for (i = 0; i <= 199990000; i++)
{
// Report progress as a percentage of the total task.
var percentComplete = (int)(i / 199990000 * 100);
if (percentComplete > highestPercentageReached)
{
highestPercentageReached = percentComplete;
// Report UI abt the progress
_worker.ReportProgress(percentComplete);
_worker.CancelAsync();
}
}
}
}
void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
button1.Enabled = true;
if (e.Cancelled)
{
// Display some message to the user that task has been
// cancelled
label1.Text = "Cancelled the operation";
}
else if (e.Error != null)
{
// Do something with the error
}
button1.Text = "Start again";
}
void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = string.Format("Result {0}: Percent {1}",e.UserState, e.ProgressPercentage);
}
private void OnStartClick(object sender, System.EventArgs e)
{
_worker.RunWorkerAsync();
button1.Text = "Processing started...";
button1.Enabled = false;
}
}
}
As mentioned in other answers, there is probably a better solution than what you are asking for.
To directly answer your question, check out the IMessageFilter interface
Create your filter to have it suppress the mouse messages you don't desire, apply it when necessary using Application.AddMessageFilter().
Something along these lines (this should probably compile...):
public class MouseButtonFilter : IMessageFilter
{
private const int WM_LBUTTONDOWN = 0x0201;
private const int WM_LBUTTONUP = 0x0202;
private const int WM_LBUTTONDBLCLK = 0x0203;
private const int WM_RBUTTONDOWN = 0x0204;
private const int WM_RBUTTONUP = 0x0205;
private const int WM_RBUTTONDBLCLK = 0x0206;
private const int WM_MBUTTONDOWN = 0x0207;
private const int WM_MBUTTONUP = 0x0208;
bool IMessageFilter.PreFilterMessage(ref Message m)
{
switch (m.Msg)
{
case WM_LBUTTONDOWN:
/* case ... (list them all here; i'm being lazy) */
case WM_MBUTTONUP:
return true;
}
return false;
}
}

How to show and hide the Popup?

void Start()
{
System.Windows.Controls.Primitives.Popup p = new System.Windows.Controls.Primitives.Popup();
p.HorizontalOffset = this.ActualWidth / 2;
p.Width = 100;
p.Height = 100;
p.VerticalOffset = this.ActualHeight / 2;
DockPanel dock = new DockPanel();
dock.Children.Add(new Button() { Content = "Обновлено" });
p.Child = dock;
p.IsOpen = true;
Thread t = new Thread(StopPopup);
t.Start(p);}
function:
private void StopPopup(object obj)
{
try
{
System.Windows.Controls.Primitives.Popup p = (System.Windows.Controls.Primitives.Popup)obj;
this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
{
dataGrid1.DataContext = DataSetCreator.AllItems();
Thread.Sleep(1500);
p.IsOpen = false;
}));
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
but why this code is triggered once = (
}
It looks like you're trying to show a control from a non-UI thread and not connecting it to the Application UI in any way (as far as I can see here). WPF UI elements need to created and manipulated on the UI thread and need to be associated with some Window based control in order to be rendered.
In addition to what John said, I would suggest looking at a Modeless dialog box. When you call Show() on the dialog box, the method returns immediately. This allows the application to continue instead waiting for a response from the dialog. You can also attach to button's click event so you know when the button is clicked.

Button Click Event Getting Lost

I have a Menu and Submenu structure in Silverlight, and I want the submenu to disappear when the parent menu item loses focus - standard Menu behavior. I've noticed that the submenu's click events are lost when a submenu item is clicked, because the parent menu item loses focus and the submenu disappears.
It's easier to explain with code:
ParentMenuBtn.Click += delegate
{
SubMenu.Visibility = (SubMenu.Visibility == Visibility.Visible) ? SubMenu.Collapsed : SubMenu.Visible;
};
ParentMenuBtn.LostFocus += delegate
{
SubMenu.Visibility = Visibility.Collapsed;
};
SubMenuBtn.Click += delegate
{
throw new Exception("This will never be thrown.");
};
In my example, when SubMenuBtn is clicked, the first event that triggers is ParentMenuBtn.LostFocus(), which hides the container of SubMenuBtn. Once the container's visibility collapses, the Click event is never triggered.
I'd rather avoid having to hide the sub-menu each time, but I'm a little surprised that the Click event is never triggered as a result...
I can't put any checks inside the LostFocus() event to see if my SubMenuBtn has focus, because it does not gain focus until after the LostFocus() event is called. In other words, SubMenuBtn.IsFocused = false when LostFocus() is triggered.
Anyone have any thoughts about this?
I've found out the solution - albeit, it's not as simple, or elegant as I would have liked. The solution is to use a secondary thread that pauses only for a moment before executing.
ie.
public partial class Navigation : UserControl
{
public Navigation()
{
ParentMenuBtn.Click += delegate
{
SubMenu.Visibility = (SubMenu.Visibility == Visibility.Visible) ? Visibility.Collapsed : Visibility.Visible;
};
ParentMenuBtn.LostFocus += delegate(object sender, RoutedEventArgs e)
{
HideSubMenu(SubMenu);
};
SubMenuBtn.Click += delegate
{
//Sub Menu Button actions...
};
private void HideSubMenu(UIElement subMenu)
{
//Get the Main Page
App app = (App)Application.Current;
MainPage mainPage = (MainPage)app.RootVisual;
Thread thread = new Thread(Navigation.HideSubMenu);
thread.Start(new ThreadState(mainPage, subMenu));
}
private static void HideSubMenu(object threadStateObj)
{
ThreadState threadState = (ThreadState)threadStateObj;
//Execute after 5 milliseconds...
System.Threading.Thread.Sleep(5);
threadState.MainPage.Dispatcher.BeginInvoke(delegate() {
threadState.TargetElement.Visibility = Visibility.Collapsed;
});
}
I just use a simple object called ThreadState to handle all the state objects I want to preserve:
public class ThreadState
{
public MainPage MainPage = null;
public UIElement TargetElement = null;
public ThreadState(MainPage mainPage, UIElement targetElement)
{
this.MainPage = mainPage;
this.TargetElement = targetElement;
}
}

Using IVMRWindowlessControl to display video in a Winforms Control and allow for full screen toggle

I've recently switched from using the IVideoWindow interface to IVMRWindowlessControl in my custom Winforms control to display video.
The reason for this was to allow zoom capabilities on the video within the control.
However in switching over, I've found that the FullScreen mode from IVideoWindow is not available and I am currently trying to replicate this using the SetVideoWindow() method.
I'm finding that I size the video in my control to be at the same resolution as the screen however I can't get the control to position itself to the top/left of the screen and become the top most window.
Any ideas on how to achieve this since the IVideoWindow::put_FullScreenMode just did it all for you?
Resolved the FullScreen problem by hosting the video control in a fresh form which I resized to the size of the current screen, then handled the 'Escape' key press in the form, to toggle back to the normal size video. Here's an extract of the code:-
Members
private Rectangle fullScreenRectangle;
private bool fullScreen;
private Form fullScreenForm;
private Control fullScreenParent;
Toggle FullScreen code
/// <summary>
/// Toggle Full Screen Mode
/// </summary>
public bool FullScreen
{
get
{
return this.fullScreen;
}
set
{
this.fullScreen = value;
if (this.fullScreen)
{
// If switch to full screen, save the current size of the control
this.fullScreenRectangle = new Rectangle(this.Location, this.Size);
// Get the current screen resolution and set that to be the control's size
Rectangle screenRect = Screen.GetBounds(this);
// Create a new form on which to host the control whilst we go to full screen mode.
this.fullScreenForm = new Form();
this.fullScreenForm.Location = PointToScreen(new Point(0, 0));
this.fullScreenForm.Size = new Size(screenRect.Width, screenRect.Height);
this.fullScreenForm.BackColor = Color.Black;
this.fullScreenForm.ShowInTaskbar = false;
this.fullScreenForm.ShowIcon = false;
this.fullScreenForm.FormBorderStyle = FormBorderStyle.None;
this.fullScreenForm.KeyPreview = true;
this.fullScreenForm.PreviewKeyDown += new PreviewKeyDownEventHandler(fullScreenForm_PreviewKeyDown);
this.fullScreenParent = this.Parent;
this.fullScreenForm.Controls.Add(this);
this.fullScreenForm.Show();
this.windowlessControl.SetVideoPosition(null, screenRect);
}
else
{
// Revert to the original control size
this.Location = PointToScreen(new Point(this.fullScreenRectangle.Left, this.fullScreenRectangle.Top));
this.Size = new Size(this.fullScreenRectangle.Width, this.fullScreenRectangle.Height);
this.windowlessControl.SetVideoPosition(null, this.fullScreenRectangle);
if (this.fullScreenForm != null)
{
this.fullScreenForm.Controls.Remove(this);
if (this.fullScreenParent != null)
this.Parent = this.fullScreenParent;
this.fullScreenForm.PreviewKeyDown -= new PreviewKeyDownEventHandler(fullScreenForm_PreviewKeyDown);
this.fullScreenForm.Close();
}
}
}
}
void fullScreenForm_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
var viewer = this.Controls[0] as ViewerControl;
if (viewer != null)
viewer.FullScreen = false;
}
}

Resources