Subclassing - Detecting Start Menu Activation

J

Jayson Ragasa

I have found an article about how to subclass in NETCF.
http://blogs.msdn.com/raffael/archive/2008/02/26/subclassing-netcf-applications.aspx

Took some part of the code to test in detetcting Start Menu
activation(shown) and closed. But am not sure what I'm doing wrong
because after hooking Start Menu, it hangs.. I even tested on my
phone, and same thing happens..

here's the code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace DetectWM65StartMenuVisibility
{
public partial class Form1 : Form
{
#region DllImport
public const int GWL_WNDPROC = (-4);
public const int WM_RAFFAEL = 123456789;

public delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg,
IntPtr wParam, IntPtr lParam);

[DllImport("coredll", SetLastError = true)]
public static extern IntPtr GetWindowLong(IntPtr hWnd, int
nIndex);

[DllImport("coredll", SetLastError = true)]
public static extern int SetWindowLong(IntPtr hWnd, int
nIndex, IntPtr newWndProc);

[DllImport("coredll", SetLastError = true)]
public static extern IntPtr CallWindowProc(IntPtr
lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

[DllImport("coredll.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string _ClassName,
string _WindowName);

[DllImport("coredll.dll", SetLastError = true)]
public static extern int SendMessage(IntPtr hWnd, uint Msg,
int wParam, int lParam);

[DllImport("coredll.dll", SetLastError = true)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
#endregion DllImport

private static IntPtr oldWndProc = IntPtr.Zero;
private static WndProcDelegate newWndProc;
private static IntPtr hWndTarget = IntPtr.Zero;
private string strClassName = "MSSTARTMENU";
private string strWindowName = "Start";

uint WM_ACTIVATE = 0x06;
uint WM_KILLFOCUS = 0x08;

public Form1()
{
InitializeComponent();
InitializeControls();
InitializeControlEvents();
}

void InitializeControls()
{
this.Size = new Size(100,100);
this.Location = new Point(0,
(Screen.PrimaryScreen.WorkingArea.Height - this.Height)-
Screen.PrimaryScreen.WorkingArea.Top);
}

void InitializeControlEvents()
{
this.Load += new EventHandler(Form1_Load);
this.Closing += new CancelEventHandler(Form1_Closing);

btnStartHook.Click += new
EventHandler(btnStartHook_Click);
btnUnhook.Click += new EventHandler(btnUnhook_Click);
}

void btnUnhook_Click(object sender, EventArgs e)
{
int success = SetWindowLong(hWndTarget, GWL_WNDPROC,
oldWndProc);

Application.Exit();
}

void btnStartHook_Click(object sender, EventArgs e)
{
hWndTarget = FindWindow(strClassName, strWindowName);
System.Diagnostics.Debug.WriteLine("hwndtarget: " +
hWndTarget.ToString());
if (hWndTarget == IntPtr.Zero) { return; }

newWndProc = new WndProcDelegate(NewWndProc);
oldWndProc = GetWindowLong(hWndTarget, GWL_WNDPROC);
int success = SetWindowLong(hWndTarget, GWL_WNDPROC,
Marshal.GetFunctionPointerForDelegate(newWndProc));
}

void Form1_Closing(object sender, CancelEventArgs e)
{
int success = SetWindowLong(hWndTarget, GWL_WNDPROC,
oldWndProc);
}

void Form1_Load(object sender, EventArgs e)
{
}

//SUBCLASSING PROCEDURE
public IntPtr NewWndProc(IntPtr hWnd, uint msg, IntPtr wParam,
IntPtr lParam)
{
if (msg == WM_ACTIVATE)
{
System.Diagnostics.Debug.WriteLine("StartMenu
Activated");
}
else if (msg == WM_KILLFOCUS)
{
System.Diagnostics.Debug.WriteLine("StartMenu
Deactivated");
}

System.Diagnostics.Debug.WriteLine("msg: " +
msg.ToString());

return CallWindowProc(oldWndProc, hWnd, msg, wParam,
lParam);
}
}
}

can somebody help me out?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top