1

i'm currently trying to get wndproc handling in wpf but without any success..

i need to get the event of window created, window activated and window destroid events.

here's what i tried so far

 int uMsgNotify;

    public MainWindow()
    {
        InitializeComponent();

        WinApi.SetTaskmanWindow(new WindowInteropHelper(this).Handle);
        WinApi.RegisterShellHookWindow(new WindowInteropHelper(this).Handle);

        uMsgNotify = WinApi.RegisterWindowMessage("SHELLHOOK");

        MainForm.ShowInTaskbar = false;
        MainForm.ShowActivated = true;   
    }

    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)
    {
        IntPtr handle;
        if (msg == uMsgNotify)
        {
            switch (wParam.ToInt32())
            {
                case WinApi.HSHELL_WINDOWCREATED:
                    handle = lParam;
                    string windowName = GetWindowName(handle);
                    IntPtr hWnd = WinApi.FindWindow(null, windowName);
                    add_icon(windowName, handle);// add new task's icon in taskbar
                    break;
                case WinApi.HSHELL_WINDOWACTIVATED:
                    handle = lParam;
                    break;
                case WinApi.HSHELL_WINDOWDESTROYED:
                    handle = lParam;
                    del_icon(handle); //remove icon from taskbar
                    break;
            }
        }
        return IntPtr.Zero;
    }

    private static string GetWindowName(IntPtr hWnd)
    {
        // Allocate correct string length first
        int length = WinApi.GetWindowTextLength(hWnd);
        StringBuilder sb = new StringBuilder(length + 1);
        WinApi.GetWindowText(hWnd, sb, sb.Capacity);
        return sb.ToString();
    }

code doesn't give any kind of runtime error whatsoever.... but it wont work.. to make more sense i'm developing an alternate shell for windows for my gaming cafe... where it needs to have a kind of taskbar..

any help?

Suraj Bhawal
  • 403
  • 1
  • 6
  • 19
  • 2
    try putting the code after `InitializeComponent()` in some `Loaded` event handler. I doubt that you cannot get some `Handle` right after `InitializeComponent()`. – King King Nov 10 '14 at 18:03
  • How do you know it's not working? Are you listening in with another program and not receiving the message? – jdnew18 Nov 10 '14 at 18:03
  • @JasonNew ... it's supposed to add an icon to the taskbar(see add_icon in the code above) when any program is executed and is avaialble in the taskbar.. and should be removed after exiting that program – Suraj Bhawal Nov 10 '14 at 18:07
  • you should try that. it works in winforms because in winforms you can get the `Handle` right after `InitializeComponent()`, but as what I've experienced in WPF, it is not like so. If it still does not work, try checking if the hook is set-up successfully and printing the message sent to the window, maybe some messages are not sent to the Hook proc in WPF (while they are sent to WndProc in winforms). – King King Nov 10 '14 at 18:23
  • I take it the "WinApi" class is not written by Microsoft? If not, the problem may be in there. Also, I don't see any code for posting/sending out a Windows message, so I'm guessing that's taken care of in the WinApi class? If not, then not sending out a Windows message means that there is no Windows message to hear in the first place. – jdnew18 Nov 10 '14 at 18:25
  • @JasonNew I don't think that class has some problem. It is just a simple wrapper static class declaring many static imported win32 functions (called pinvoke in .NET), here is the documentation for the native api function [RegisterShellHookWindow](http://msdn.microsoft.com/en-us/library/windows/desktop/ms644989(v=vs.85).aspx) – King King Nov 10 '14 at 18:29

1 Answers1

-1

I don't know if you still need it but : If you use ShowInTaskbar = false, it won't be able to catch any message with WndProc.

SeyoS
  • 661
  • 5
  • 22