Click here to Skip to main content
11,922,788 members (55,662 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked

Mobile Development: Disable Windows Mobile 6.5 Start and Close Button

, 18 Nov 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Windows Mobile 6.5.3. Class to subclass menu toolbar to disable Start and Close button clicks.

Here is one other way to write a kios mode .NET application using a technique called SubClassing. The idea was born by a comment of redwolf2222 on this blog about how to Hide Start and Close buttons on Windows Mobile 6.5 devices. Redwolf2222 also provided a code snippet. Unfortunately it was incomplete and so I wrote my own class.

Disable clicks on Start and Close button

The demo project shows one dialog with two check boxes and you can easily test the function. If “StartButton Disabled” or “Close Button disabled” is checked, you cannot ‘click’ the corresponding button any more:

You still ‘click’ the buttons but the subclassed window will not ‘execute’ your click. The buttons are part of the toolbar32 window which is a child of the menu_worker window. So first, we have to follow the window tree.

Find the right window

/// <span class="code-SummaryComment"><summary>


Now, as we have the window handle, the subclassing can be started:

private void SubclassHWnd(IntPtr hWnd)
    // hWnd is the window you want to subclass..., create a new
    // delegate for the new wndproc
    newWndProc = new Win32WndProc(MyWndProc);
    // subclass
    oldWndProc = SetWindowLong(hWnd, GWL_WNDPROC, newWndProc);

The installation of the ‘hook’ is very simple. Just use SetWindowLong with the new window procedure. The old, original window procedure is saved for later use. We need it for example to call it for clicks outside the buttons and for all messages we don't care about.

The ‘hook’ or better the redirection will remain active until you install the old window procedure. So your device’s start and close button will not ‘work’ as long as the hook is in place.

The New Window Procedure

// this is the new wndproc, just show a messagebox on left button down:
private IntPtr MyWndProc(IntPtr hWnd, int msg, int wParam, int lParam)
    //is this a message for us?
    if (((msg == (int)WM_LBUTTONDOWN) || (msg == (int)WM_LBUTTONUP)) && 
	(this._mIsStartButtonDisabled || this._mIsCloseButtonDisabled) )
        int x = ((int)lParam) & 0xFFFF;
        int y = ((int)lParam) >> 16;
        bool isVGA;
        bool isQVGA;
        using (System.Windows.Forms.Control detector = 
			new System.Windows.Forms.Control())
            using (System.Drawing.Graphics gr = detector.CreateGraphics())
                isVGA = gr.DpiY == 192;
                isQVGA = gr.DpiY == 96;
        RECT rect;
        GetWindowRect(hWnd, out rect); //get the rectangle of the menu_bar
        int width = Math.Max(rect.Left, rect.Right) - 
				Math.Min(rect.Left, rect.Right);
        int height = Math.Max(rect.Bottom, rect.Top) - 
				Math.Min(rect.Bottom, rect.Top);
        //width values are assumed
        int buttonWidth = (isQVGA | isVGA) ? 92 : 46;
        int buttonHeight = height; //(isQVGA | isVGA) ? 72 : 36;
        System.Drawing.Rectangle rectStartButton = 
	new System.Drawing.Rectangle(0, 0, buttonWidth, buttonHeight);
        System.Drawing.Rectangle rectCloseButton = 
	new System.Drawing.Rectangle(width - buttonWidth, 0, 
				buttonWidth, buttonHeight);
        //check if enabled and click is inside the start or close button rectangle
        if(this._mIsStartButtonDisabled && rectStartButton.Contains(x, y))
            return IntPtr.Zero;
        if (this._mIsCloseButtonDisabled && rectCloseButton.Contains(x, y))
            return IntPtr.Zero;
        //if both are false, we have to provide the click to windows
        return CallWindowProc(oldWndProc, hWnd, msg, wParam, lParam);
        return CallWindowProc(oldWndProc, hWnd, msg, wParam, lParam);

Subclassing the window means that we redirect the window message procedure of the found window to our own, custom window procedure. This new procedure checks for WM_LBUTTONDOWN and WM_LBUTTONUP messages. Then the click position is checked and discarded if within the rectangle area of the Start and/or Close button. If the position is outside the calculated rectangles, the original window procedure is called.

The Demo Code

public partial class StartButtonControl : Form
    StartButtonWM65.hwndutils _hwndutils = new StartButtonWM65.hwndutils();
    private bool _bInitializing = true;
    public StartButtonControl()
        this.chkDisableStartButton.Checked = this._hwndutils.StartButtonDisabled;
        _bInitializing = false;
    private void chkDisableStartButton_CheckStateChanged(object sender, EventArgs e)
        if (_bInitializing)
        this._hwndutils.StartButtonDisabled = chkDisableStartButton.Checked;
    private void mnuExit_Click(object sender, EventArgs e)
    private void StartButtonControl_Closing(object sender, CancelEventArgs e)
    private void chkCloseButton_CheckStateChanged(object sender, EventArgs e)
        if (_bInitializing)
        this._hwndutils.CloseButtonDisabled = chkCloseButton.Checked;

As you see, the usage of the class hwndutils is very simple. Don't forget to Dispose the hwndutils object before you exit your app.


Visual Studio 2008 solution with demo project targeting Windows Mobile 6 SDK:
DOWNLOAD: StartButtonHookWM65 VS2008 source code and demo project - (Hits: 1, size: 18.53 KB)

Thanks to redwolf2222 for the great idea.

<!-- Social Bookmarks BEGIN --> <!-- Social Bookmarks END -->


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionDisabling the Taskbar Pin
kcmmi25-Mar-14 8:07
memberkcmmi25-Mar-14 8:07 
AnswerRe: Disabling the Taskbar Pin
hjgode25-Mar-14 9:26
memberhjgode25-Mar-14 9:26 
GeneralRe: Disabling the Taskbar Pin
kcmmi25-Mar-14 9:36
memberkcmmi25-Mar-14 9:36 
GeneralRe: Disabling the Taskbar Pin
hjgode25-Mar-14 19:28
memberhjgode25-Mar-14 19:28 
GeneralRe: Disabling the Taskbar Pin
kcmmi26-Mar-14 4:42
memberkcmmi26-Mar-14 4:42 
GeneralRe: Disabling the Taskbar Pin
kcmmi26-Mar-14 6:58
memberkcmmi26-Mar-14 6:58 
GeneralRe: Disabling the Taskbar Pin
hjgode26-Mar-14 9:41
memberhjgode26-Mar-14 9:41 
GeneralRe: Disabling the Taskbar Pin
kcmmi28-Mar-14 13:04
memberkcmmi28-Mar-14 13:04 
GeneralRe: Disabling the Taskbar Pin
hjgode31-Mar-14 23:53
memberhjgode31-Mar-14 23:53 
GeneralRe: Disabling the Taskbar Pin
kcmmi1-Apr-14 4:21
memberkcmmi1-Apr-14 4:21 
GeneralRe: Disabling the Taskbar Pin
kcmmi25-Mar-14 12:50
memberkcmmi25-Mar-14 12:50 
Generalnice but... Pin
moshhc9-Dec-10 5:17
membermoshhc9-Dec-10 5:17 
GeneralRe: nice but... Pin
hjgode9-Dec-10 7:46
memberhjgode9-Dec-10 7:46 

I am sorry, the subclassing code should work as long as no other app is subclassing the same window and does not forward messages to orignal WndProc.

I have done subclassing frequently using a DLL. Due to design changes in memory layout and special compact framework it may happen that the C# subclassing is not working as a native dll would do.

Nevertheless this code here is meant as an extension to be used in kiosk mode applications and such application would NOT allow switching to another uncontrolled app. A kiosk mode app would call AllKeys(True) so the green phone button will NOT launch the phone app. For the purpose of writing a kiosk mode app the code is fine. Smile | :)

I am sorry about all the nasty things one has to do to get a kiosk mode app (more or less). Frown | :(

with regards


PS: I will think about writing a native DLL to provide system-wide subclassing.

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151125.1 | Last Updated 18 Nov 2010
Article Copyright 2010 by hjgode
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid