I need to execute a code when shutdown/logoff. When any of these action are made im getting an application error with code 0xe0434352. What im doing wrong?
This is my code:
private void Window_SourceInitialized(object sender, EventArgs e)
{
IntPtr windowHandle = (new WindowInteropHelper(this)).Handle;
HwndSource src = HwndSource.FromHwnd(windowHandle);
src.AddHook(new HwndSourceHook(WndProc));
}
private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == 0x0011)
{
EventLog.WriteEntry("WPF1", "finally!");
//my code
}
return IntPtr.Zero;
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
IntPtr windowHandle = (new WindowInteropHelper(this)).Handle;
HwndSource src = HwndSource.FromHwnd(windowHandle);
src.RemoveHook(new HwndSourceHook(this.WndProc));
}
Exception error: http://pastebin.com/gaX0ekaP
Nevermind, admin priviledges were required.
Related
If user double-clicks window title bar to maximize window, the window will maximize on MouseDown event of the second click, but then the MouseUp event on that same second click will be registered by one of the controls in the window that are now under the cursor and trigger something user didn't want.
How can it be prevented?
This should work just fine:
Solution:
//WNDPROC INTEROP
private const int WM_NCLBUTTONDBLCLK = 0x00A3;
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
//TITLE BAR DOUBLE CLICK
if (msg == WM_NCLBUTTONDBLCLK)
{
Console.WriteLine("titlebar double click " + this.WindowState);
//WINDOW ABOUT TO GET MAXIMIZED
if (msg == WM_NCLBUTTONDBLCLK)
handleNextMouseup = true;
else if (msg == 0x202) // mouseup
{
if (handleNextMouseup)
handled = true;
handleNextMouseup = false;
}
}
return IntPtr.Zero;
}
another possible solution:
//WNDPROC INTEROP
private const int WM_NCLBUTTONDBLCLK = 0x00A3;
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
//TITLE BAR DOUBLE CLICK
if (msg == WM_NCLBUTTONDBLCLK)
{
Console.WriteLine("titlebar double click " + this.WindowState);
//WINDOW ABOUT TO GET MAXIMIZED
if (this.WindowState == WindowState.Normal)
{
fakehittestvisible = true;
}
}
return IntPtr.Zero;
}
private bool fakehittestvisible = false;
//ON PREVIEW MOUSE UP ABORT IF MIXIMIZING HAPPENED
private void this_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
if (fakehittestvisible)
{
e.Handled = true;
fakehittestvisible = false;
return;
}
fakehittestvisible = false;
Console.WriteLine("this mouse up state is " + this.WindowState);
}
I'm showing PDF documents in my application using Acrobat ActiveX.
I want to know is it possible to disable right click on PDF documents?
My previous solution didn't work, so there's another one. You just need to set a mouse hook on the child window that receives mouse notifications.
public class AcrobatReader : AxAcroPDF
{
private const int WH_MOUSE_LL = 14;
private const int WM_RBUTTONDOWN = 0x0204;
private static LowLevelMouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
public AcrobatReader()
{
_hookID = SetWindowsHookEx(WH_MOUSE_LL, _proc, FindWindow(null, "avpageview"), 0);
}
~AcrobatReader()
{
if (_hookID != IntPtr.Zero)
{
UnhookWindowsHookEx(_hookID);
_hookID = IntPtr.Zero;
}
}
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && (IntPtr)WM_RBUTTONDOWN == wParam)
{
return new IntPtr(1);
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
}
Windows Forms have the IMessageFilter interface to capture messages. How is this done in WPF? Specifically, I want to create a clipboard format listener.
In your Window derived class:
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
((HwndSource)PresentationSource.FromVisual(this)).AddHook(myHook)
}
private IntPtr myHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
// process messages here
default:
return IntPtr.Zero;
}
}
How can I handle tilt left or tilt right mouse event in WPF?
alt text http://s3images.coroflot.com/user_files/individual_files/featured/featured_1266_st_dVYgtBSHIQRPzXvrG4WnUW.jpg
I figured out how to implement WndProc in WPF UserControl. UserControl must obtain a window pointer like by AppendWindow method in my example:
private static MyUserControl instanceWithFocus;
public void AppendWindow(Window window) {
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(window).Handle);
source.AddHook(new HwndSourceHook(WndProc));
}
private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {
switch (msg) {
case Win32Messages.WM_MOUSEHWHEEL:
MouseWheelTilt(wParam, lParam);
handled = true;
break;
default:
break;
}
return IntPtr.Zero;
}
private static void MouseWheelTilt(IntPtr wParam, IntPtr lParam) {
Int32 tilt = (Int16)Utils.HIWORD(wParam);
Int32 keys = Utils.LOWORD(wParam);
Int32 x = Utils.LOWORD(lParam);
Int32 y = Utils.HIWORD(lParam);
// call an event on active instance of this object
if (instanceWithFocus != null) {
instanceWithFocus.MouseWheelTilt(tilt, keys, x, y);
}
}
private void MouseWheelTilt(Int32 tilt, Int32 keys, Int32 x, Int32 y) {
scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset + tilt);
}
private void UserControl_MouseEnter(object sender, MouseEventArgs e) {
instanceWithFocus = this;
}
private void UserControl_MouseLeave(object sender, MouseEventArgs e) {
instanceWithFocus = null;
}
abstract class Win32Messages {
public const int WM_MOUSEHWHEEL = 0x020E;
}
abstract class Utils {
internal static Int32 HIWORD(IntPtr ptr) {
Int32 val32 = ptr.ToInt32();
return ((val32 >> 16) & 0xFFFF);
}
internal static Int32 LOWORD(IntPtr ptr) {
Int32 val32 = ptr.ToInt32();
return (val32 & 0xFFFF);
}
}
If I use dragMove the wpf window will not move to a location where the y value is negative. I can however set the windows top value to a negative value. Is there a simple way to enable dragMove to allow the top of the window to be moved above the displays 0 position?
Edit:
It seems that this is the default window's handling of moveWindow. Verified with a call to SendMessage( hwnd, WM_SYSCOMMAND, SC_MOVE, null);
As I found, the window will move to "negative" location, but will then jump back. To prevent this you could do something like:
public partial class Window1: Window {
public Window1() {
InitializeComponent();
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e) {
DragMove();
}
public struct WINDOWPOS {
public IntPtr hwnd;
public IntPtr hwndInsertAfter;
public int x;
public int y;
public int cx;
public int cy;
public UInt32 flags;
};
private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {
switch(msg) {
case 0x46://WM_WINDOWPOSCHANGING
if(Mouse.LeftButton != MouseButtonState.Pressed) {
WINDOWPOS wp = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS));
wp.flags = wp.flags | 2; //SWP_NOMOVE
Marshal.StructureToPtr(wp, lParam, false);
}
break;
}
return IntPtr.Zero;
}
private void Window_Loaded(object sender, RoutedEventArgs e) {
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
source.AddHook(new HwndSourceHook(WndProc));
}
}
Handling WM_WINDOWPOSCHANGING this way will prevent any movement of the window unless left mouse button is pressed. This includes maximizing the window and programmatic change of window position as well, so you'll have to adapt the code if you need another behavior.