Click here to Skip to main content
15,912,977 members
Home / Discussions / C#
   

C#

 
GeneralRe: Sort TreeView items by different logic Pin
Heath Stewart17-May-04 6:17
protectorHeath Stewart17-May-04 6:17 
GeneralRe: Sort TreeView items by different logic Pin
Gian17-May-04 23:16
Gian17-May-04 23:16 
GeneralRe: Sort TreeView items by different logic Pin
Heath Stewart18-May-04 3:37
protectorHeath Stewart18-May-04 3:37 
GeneralRe: Sort TreeView items by different logic Pin
Gian19-May-04 5:25
Gian19-May-04 5:25 
Generalhelp with winuser.h Pin
HappyPaws13-May-04 4:28
HappyPaws13-May-04 4:28 
GeneralRe: help with winuser.h Pin
Judah Gabriel Himango13-May-04 6:36
sponsorJudah Gabriel Himango13-May-04 6:36 
GeneralRe: help with winuser.h Pin
Heath Stewart13-May-04 8:37
protectorHeath Stewart13-May-04 8:37 
GeneralRe: help with winuser.h Pin
Heath Stewart13-May-04 8:37
protectorHeath Stewart13-May-04 8:37 
You have to P/Invoke SetWinEventHook (and later unhook it with UnhookWinEvent) and handle those messages. Here's something I threw together quickly:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class Test
{
  static void Main(string[] args)
  {
    using (AccessibleForm form = new AccessibleForm())
    {
      form.MoveSizeStart += new EventHandler(OnMoveSizeStart);
      form.MoveSizeEnd += new EventHandler(OnMoveSizeEnd);
      Application.Run(form);
    }
  }

  static void OnMoveSizeStart(object sender, EventArgs e)
  {
    Console.WriteLine("Move/Size has started.");
  }

  static void OnMoveSizeEnd(object sender, EventArgs e)
  {
    Console.WriteLine("Move/Size has ended.");
  }

  private class AccessibleForm : Form
  {
    private GCHandle cbHandle;
    private IntPtr hWinEventHook;
    
    public AccessibleForm()
    {
      // Create and pin the callback.
      WinEventProcCallback callback =
        new WinEventProcCallback(WinEventProc);
      cbHandle = GCHandle.Alloc(callback);

      // Register the callback.
      hWinEventHook = SetWinEventHook(
        new IntPtr(EVENT_SYSTEM_MOVESIZESTART),
        new IntPtr(EVENT_SYSTEM_MOVESIZEEND),
        IntPtr.Zero,
        (IntPtr)cbHandle,
        Process.GetCurrentProcess().Id,
        0,
        new IntPtr(1)); // WINEVENT_INCONTEXT ???
    }

    public event EventHandler MoveSizeStart;
    public event EventHandler MoveSizeEnd;

    protected override void Dispose(bool disposing)
    {
      if (hWinEventHook != IntPtr.Zero)
      {
        UnhookWinEvent(hWinEventHook);
        hWinEventHook = IntPtr.Zero;
      }

      if (cbHandle.IsAllocated)
        cbHandle.Free();

      base.Dispose(disposing);
    }

    protected virtual void OnMoveSizeStart(EventArgs e)
    {
      if (MoveSizeStart != null)
        MoveSizeStart(this, e);
    }

    protected virtual void OnMoveSizeEnd(EventArgs e)
    {
      if (MoveSizeEnd != null)
        MoveSizeEnd(this, e);
    }

    private void WinEventProc(
      IntPtr hWinEventHook,
      uint evt,
      IntPtr hwnd,
      int idObject,
      int idChild,
      uint dwEventThread,
      uint dwmsEventTime)
    {
      if (evt == EVENT_SYSTEM_MOVESIZESTART)
        OnMoveSizeStart(EventArgs.Empty);

      else if (evt == EVENT_SYSTEM_MOVESIZEEND)
        OnMoveSizeEnd(EventArgs.Empty);
    }

    private const uint EVENT_SYSTEM_MOVESIZESTART = 0x000a;
    private const uint EVENT_SYSTEM_MOVESIZEEND = 0x000b;

    private delegate void WinEventProcCallback(
      IntPtr hWinEventHook,
      uint evt,
      IntPtr hwnd,
      int idObject,
      int idChild,
      uint dwEventThread,
      uint dwmsEventTime);

    [DllImport("user32.dll")]
    private static extern IntPtr SetWinEventHook(
      [MarshalAs(UnmanagedType.SysUInt)] IntPtr eventMin,
      [MarshalAs(UnmanagedType.SysUInt)] IntPtr eventMax,
      IntPtr hmodWinEventProc,
      IntPtr lpfnWinEventProc,
      [MarshalAs(UnmanagedType.U4)] int idProcess,
      [MarshalAs(UnmanagedType.U4)] int idThread,
      [MarshalAs(UnmanagedType.SysUInt)] IntPtr dwflags);

    [DllImport("user32.dll")]
    private static extern bool UnhookWinEvent(IntPtr hWinEventHook);
  }
}
The problem is that in-context hooks require the callback to be in a native DLL to be mapped into the process. Out-of-context hooks marshal across process boundaries but we're in the same process.

If all you're trying to do is get notification when a user moves a window, handle the LocationChanged event and use a timer (like the Windows Forms Timer component you'll find in the toolbox). The timer is initially stopped. In your LocationChanged event handler, start the timer. With each call to the handler (there will be many), increment the Timer.Interval by 500 ms or so. When the timer's Tick event handler is called, stop the timer and perform the operations you want. Also reset the Interval back down to its default, like 500 (.5 seconds).

Timers are used like this for many different things, including click vs. double-click. There's other ways to do this as well, but this is just one idea.

Also, relying on accessibility (in which those notification messages relate) is probably not a good idea since it's not very portable.

 

Microsoft MVP, Visual C#
My Articles
GeneralRe: help with winuser.h Pin
HappyPaws14-May-04 1:37
HappyPaws14-May-04 1:37 
Generalregd DataGrids Pin
karteek13-May-04 4:06
karteek13-May-04 4:06 
Generalregd dialogs Pin
karteek13-May-04 3:41
karteek13-May-04 3:41 
GeneralRe: regd dialogs Pin
Heath Stewart13-May-04 3:47
protectorHeath Stewart13-May-04 3:47 
GeneralRe: regd dialogs Pin
karteek13-May-04 4:07
karteek13-May-04 4:07 
GeneralRe: regd dialogs Pin
Heath Stewart13-May-04 4:51
protectorHeath Stewart13-May-04 4:51 
GeneralCancel more than a click Pin
sreejith ss nair13-May-04 2:55
sreejith ss nair13-May-04 2:55 
GeneralRe: Cancel more than a click Pin
Heath Stewart13-May-04 3:19
protectorHeath Stewart13-May-04 3:19 
GeneralDES Crypto help Pin
yyzzababa13-May-04 2:43
yyzzababa13-May-04 2:43 
GeneralRe: DES Crypto help Pin
gek_at13-May-04 2:50
gek_at13-May-04 2:50 
GeneralRe: DES Crypto help Pin
Heath Stewart13-May-04 3:23
protectorHeath Stewart13-May-04 3:23 
GeneralRe: DES Crypto help Pin
gek_at13-May-04 3:50
gek_at13-May-04 3:50 
GeneralRe: DES Crypto help Pin
Heath Stewart13-May-04 3:56
protectorHeath Stewart13-May-04 3:56 
GeneralRe: DES Crypto help Pin
gek_at13-May-04 4:53
gek_at13-May-04 4:53 
GeneralRe: DES Crypto help Pin
Heath Stewart13-May-04 3:26
protectorHeath Stewart13-May-04 3:26 
GeneralInter Process Communication Pin
gek_at13-May-04 2:31
gek_at13-May-04 2:31 
GeneralRe: Inter Process Communication Pin
Heath Stewart13-May-04 3:45
protectorHeath Stewart13-May-04 3:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.