WPF minimized window setting to restore - wpf

I'm getting some very odd behaviour with a WPF window that I am able to recreate with a basic application. When I minimize my window from a maximized state, then restore my window it does not return maximized. I've implemented code to try and counter this, but the window still returns to a WindowState.Normal.
An important thing to note about this is the this only happen when I am running the application stand-alone. If I run in debug mode in visual studio everything works as expected. Also, this behaviour occurs on multiple boxes, not just my own.
Steps to reproduce with code below:
double-click WpfApplication1.exe
ensure application is maximized
click the minimize button in the top right
click the application in the taskbar
Expected Result: application window returns to a maximized state
Actual Result: application window is in restored state, with WindowState = WindowState.Normal
Here is the XAML code for the demo window.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
WindowState="Maximized"
Activated="MainWindow_OnActivated"
StateChanged="MainWindow_OnStateChanged">
<Grid>
</Grid>
</Window>
and the code-behind:
public partial class MainWindow : Window
{
private WindowState _lastWindowState;
private bool _activating;
public MainWindow()
{
InitializeComponent();
_lastWindowState = WindowState.Normal;
}
private void MainWindow_OnActivated(object sender, EventArgs e)
{
if (WindowState == WindowState.Minimized)
{
WindowState = _lastWindowState;
}
}
private void MainWindow_OnStateChanged(object sender, EventArgs e)
{
if (WindowState != WindowState.Minimized)
{
_lastWindowState = WindowState;
}
}
}
In my real application I placed logging code before and after the WindowState set in the OnActivated function and in the StateChanged function. When activating from minimized I get the following:
OnActivated 1 - _lastWindowState: Maximized
OnStateChanged - _lastWindowState: Maximized
OnActivated 2 - _lastWindowState: Maximized
OnStateChanged - _lastWindowState: Normal
Finally, in case this helps, this is the Environment.StackTrace in the last StateChanged call that happens:
at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at MapViewer.MainWindow.MainWindow_OnStateChanged(Object sender, EventArgs e) in c:\APP\MainWindow.xaml.cs:line 994
at System.Windows.Window.OnStateChanged(EventArgs e)
at System.Windows.Window.WmSizeChanged(IntPtr wParam)
at System.Windows.Window.WindowFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.PublicHooksFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.HwndSubclass.DefWndProcWrapper(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.HwndSubclass.DefWndProcWrapper(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at MapViewer.App.Main() in c:\Projects\APP\obj\x86\Debug\App.g.cs:line 0

Related

Touch, Framework 4.7, multiple windows & Windows Creator Update

Since Windows 10 creator update, we have a touch compliant WPF App that crashes:
Crash occurs when we are moving from one window to another on a touch action.
Here is the revelant part of the stack :
Source : PresentationCore
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Input.StylusWisp.WispStylusDevice.get_TabletDevice()
at System.Windows.Input.StylusTouchDeviceBase..ctor(StylusDeviceBase stylusDevice)
at System.Windows.Input.StylusWisp.WispLogic.PromoteMainToMouse(StagingAreaInputItem stagingItem)
at System.Windows.Input.StylusWisp.WispLogic.PromoteStoredItemsToMouse(WispStylusTouchDevice touchDevice)
at System.Windows.Input.StylusWisp.WispStylusTouchDevice.OnManipulationEnded(Boolean cancel)
at System.Windows.Input.ManipulationDevice.OnManipulationCancel()
at System.Windows.Input.InputManager.RaiseProcessInputEventHandlers(ProcessInputEventHandler postProcessInput, ProcessInputEventArgs processInputEventArgs)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.ManipulationLogic.ReportFrame(ICollection`1 manipulators)
at System.Windows.Input.ManipulationDevice.ReportFrame()
at System.Windows.Input.ManipulationDevice.RemoveManipulator(IManipulator manipulator)
at System.Windows.Input.Manipulation.TryRemoveManipulator(UIElement element, IManipulator manipulator)
at System.Windows.Input.TouchDevice.PromoteMainToManipulation(UIElement manipulatableElement, TouchEventArgs touchEventArgs)
at System.Windows.Input.TouchDevice.PostProcessInput(Object sender, ProcessInputEventArgs e)
at System.Windows.Input.InputManager.RaiseProcessInputEventHandlers(ProcessInputEventHandler postProcessInput, ProcessInputEventArgs processInputEventArgs)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.TouchDevice.Capture(IInputElement element, CaptureMode captureMode)
at System.Windows.Input.TouchDevice.PromoteMainToManipulation(UIElement manipulatableElement, TouchEventArgs touchEventArgs)
at System.Windows.Input.TouchDevice.PostProcessInput(Object sender, ProcessInputEventArgs e)
at System.Windows.Input.InputManager.RaiseProcessInputEventHandlers(ProcessInputEventHandler postProcessInput, ProcessInputEventArgs processInputEventArgs)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.TouchDevice.RaiseTouchUp()
at System.Windows.Input.TouchDevice.ReportUp()
at System.Windows.Input.StylusWisp.WispLogic.PromoteMainUpToTouch(WispStylusDevice stylusDevice, StagingAreaInputItem stagingItem)
at System.Windows.Input.StylusWisp.WispLogic.PromoteMainToTouch(ProcessInputEventArgs e, StylusEventArgs stylusEventArgs)
at System.Windows.Input.StylusWisp.WispLogic.PostProcessInput(Object sender, ProcessInputEventArgs e)
at System.Windows.Input.InputManager.RaiseProcessInputEventHandlers(ProcessInputEventHandler postProcessInput, ProcessInputEventArgs processInputEventArgs)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.StylusWisp.WispLogic.InputManagerProcessInput(Object oInput)
at System.Windows.Input.StylusWisp.WispLogic.PreProcessInput(Object sender, PreProcessInputEventArgs e)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Window.ShowHelper(Object booleanBox)
at System.Windows.Window.ShowDialog()
...
Let me be precise, the product is perfectly working on windows 7/8/8.1/10(non creator) and we do not use any Wacom or stylus device except our finger :)
If you have any idea, let me know and i will test it.

The specified Visual is not a descendant of this Visual Exception

I am getting The "specified Visual is not a descendant of this Visual" exception.
Can you please help on what scenarios we will get this exception as stacktrace is not helping me to identify the issue?
Stack-trace:
at System.Windows.Media.Visual.TrySimpleTransformToAncestor(Visual
ancestor, Boolean inverse, GeneralTransform& generalTransform, Matrix&
simpleTransform) at
System.Windows.Media.Visual.InternalTransformToAncestor(Visual
ancestor, Boolean inverse) at
System.Windows.Window.HandleWmNcHitTestMsg(IntPtr lParam, IntPtr&
refInt) at System.Windows.Window.WindowFilterMessage(IntPtr hwnd,
Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at
System.Windows.Interop.HwndSource.PublicHooksFilterMessage(IntPtr
hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at
MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam,
IntPtr lParam, Boolean& handled) at
MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate
callback, Object args, Int32 numArgs) at
MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object
source, Delegate method, Object args, Int32 numArgs, Delegate
catchHandler)

MS Access 2013: Attempted to read or write protected memory. This is often an indication that other memory is corrupt

We are running windows 8 64 bit and MS Office 2013 64 bit on a virtual machine. I got this error when I am trying to query the access database with parameters in the query string. The stack trace is:
at System.Data.Common.UnsafeNativeMethods.ICommandText.Execute(IntPtr pUnkOuter, Guid& riid, tagDBPARAMS pDBParams, IntPtr& pcRowsAffected, Object& ppRowset)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
at _64AccessTest.MainWindow.Button_Click_1(Object sender, RoutedEventArgs e) in c:\Users\xiaosu\Documents\Visual Studio 2012\Projects\64AccessTest\64AccessTest\MainWindow.xaml.cs:line 110
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run()
at _64AccessTest.App.Main() in c:\Users\xiaosu\Documents\Visual Studio 2012\Projects\64AccessTest\64AccessTest\obj\Debug\App.g.cs:line 0
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
I have got the following code:
var connectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\temp\templates\64Access.accdb;Persist Security Info=False;";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
const String query = "PARAMETERS #Id LONG; select * from Subject where Id = #Id";
OleDbCommand _command = new OleDbCommand(query, connection);
_command.Parameters.Add("#Id", OleDbType.Integer);
_command.Parameters["#Id"].Value = 1;
using (OleDbDataReader reader = _command.ExecuteReader()){
......
}
}
The exception occurs at this line: using (OleDbDataReader reader = _command.ExecuteReader()
We are running .NET 4 with Visual Studio 2012 and this is a WPF application.
The wired thing is if I do not pass parameters to the sql query, it works fine. Once the query has some parameters, the error occurs.
In Visual Studio, the platform is set to AnyCPU which is what we want. We do not want to change this to x86.
Thanks for the help.

System.InvalidOperationException: Dispatcher processing has been suspended, but messages are still being processed

I'm converting a MFC app in x64. This app has a few WPF controls/dialogs. The problem I have is, for a few users, displaying a WPF control/dialog does not work. I get this exception:
System.InvalidOperationException: Dispatcher processing has been suspended, but messages are still being processed.
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Threading.WaitHandle.WaitOneNative(SafeHandle waitableSafeHandle, UInt32 millisecondsTimeout, Boolean hasThreadAffinity, Boolean exitContext)
at System.Threading.WaitHandle.InternalWaitOne(SafeHandle waitableSafeHandle, Int64 millisecondsTimeout, Boolean hasThreadAffinity, Boolean exitContext)
at System.Windows.Input.PenThreadWorker.WorkerCreateContext(IntPtr hwnd, IPimcTablet pimcTablet)
at System.Windows.Input.TabletDevice.CreateContext(IntPtr hwnd, PenContexts contexts)
at System.Windows.Input.TabletDeviceCollection.CreateContexts(IntPtr hwnd, PenContexts contexts)
at System.Windows.Input.PenContexts.Enable()
at System.Windows.Input.StylusLogic.RegisterHwndForInput(InputManager inputManager, PresentationSource inputSource)
at System.Windows.Interop.HwndStylusInputProvider..ctor(HwndSource source)
at System.Windows.Interop.HwndSource.Initialize(HwndSourceParameters parameters)
at System.Windows.Forms.Integration.ElementHost.OnHandleCreated(EventArgs e)
at System.Windows.Forms.Control.WmCreate(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Integration.ElementHost.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
The app than hangs for a few seconds and crashes.
Has anybody ever had similar problems with MFC/WPF in 64bit ?

How can a Windows Forms ListView be programmatically scrolled left/right?

How can a Windows Forms ListView be programmatically scrolled left/right?
Maybe a SendMessage can be sent to the control's horizontal scrollbar?
Try This
[DllImport("user32")]
static extern IntPtr SendMessage(IntPtr Handle, Int32 msg, IntPtr wParam,
IntPtr lParam);
protected void ScrollH(int pixelsToScroll)
{
const Int32 LVM_FIRST = 0x1000;
const Int32 LVM_SCROLL = LVM_FIRST + 20;
SendMessage(lvwList.Handle, LVM_SCROLL, (IntPtr) pixelsToScroll,
IntPtr.Zero);
}

Resources