Click here to Skip to main content
15,891,951 members
Articles / Mobile Apps / Windows Mobile

Radio Power

Rate me:
Please Sign up or sign in to vote.
4.92/5 (27 votes)
19 Aug 2010CPOL11 min read 142.5K   10.9K   49  
An in-depth view in to monitoring and controlling the power of your Windows Mobile device's wireless communications systems.
// MainDlg.cpp : implementation of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"

#include "MainDlg.h"

#include <boost/bind.hpp>

LRESULT CMainDlg::OnInitDialog( UINT /*uMsg*/, 
                                WPARAM /*wParam*/, 
                                LPARAM /*lParam*/, 
                                BOOL& /*bHandled*/ )
{
    // center the dialog on the screen
    CenterWindow();

    DoDataExchange();

    // set icons
    HICON hIcon = ( HICON )::LoadImage( _Module.GetResourceInstance(), 
                                        MAKEINTRESOURCE( IDR_MAINFRAME ), 
                                        IMAGE_ICON, 
                                        ::GetSystemMetrics( SM_CXICON ), 
                                        ::GetSystemMetrics( SM_CYICON ), 
                                        LR_DEFAULTCOLOR );
    SetIcon( hIcon, TRUE );

    HICON hIconSmall = ( HICON )::LoadImage( _Module.GetResourceInstance(), 
                                             MAKEINTRESOURCE( IDR_MAINFRAME ), 
                                             IMAGE_ICON, 
                                             ::GetSystemMetrics( SM_CXSMICON ), 
                                             ::GetSystemMetrics( SM_CYSMICON ), 
                                             LR_DEFAULTCOLOR );
    SetIcon( hIconSmall, FALSE );

    // update the dialog controls to reflect the current state of the WiFi radio
    UpdateControlState( ndis_power_.IsRadioEnabled() );

    // start listening for changes in the WiFi radio state
    ndis_monitor_.Start( boost::bind( &CMainDlg::OnRadioStateChanged, this, _1 ) );

    return TRUE;
}

LRESULT CMainDlg::OnEnable( WORD /*wNotifyCode*/, 
                            WORD /*wID*/, 
                            HWND /*hWndCtl*/, 
                            BOOL& /*bHandled*/ )
{
    if( ndis_power_.IsRadioEnabled() )
        ndis_power_.Disable();
    else
        ndis_power_.Enable();
    return 0;
}

LRESULT CMainDlg::OnCancel( WORD /*wNotifyCode*/, 
                            WORD wID, 
                            HWND /*hWndCtl*/, 
                            BOOL& /*bHandled*/ )
{
    EndDialog( wID );
    return 0;
}

LRESULT CMainDlg::OnDestroy( UINT /*uMsg*/, 
                             WPARAM /*wParam*/, 
                             LPARAM /*lParam*/, 
                             BOOL& /*bHandled*/ )
{
    ndis_monitor_.Stop();
    return 0;
}

LRESULT CMainDlg::OnRadioStateChanged( UINT /*uMsg*/, 
                                       WPARAM wParam, 
                                       LPARAM /*lParam*/, 
                                       BOOL& /*bHandled*/ )
{
    bool enabled = static_cast< bool >( wParam );
    UpdateControlState( enabled );
    return 0;
}

void CMainDlg::OnRadioStateChanged( bool enabled )
{
    /// we must return to the UI thread to change the state of the UI.
    PostMessage( UWM_RADIO_STATE_CHANGED, enabled );
}

void CMainDlg::UpdateControlState( bool enabled )
{
    status_.SetWindowText( enabled ? L"WiFi is enabled." : L"WiFi is disabled." );
    enable_.SetWindowText( enabled ? L"Disable" : L"Enable" );
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) An engineering firm in Cedar Rapids, Iowa
United States United States
I'm also on the MSDN forums
http://social.msdn.microsoft.com/profile/paulh79

Comments and Discussions