Archive

Archive for the ‘Windows 7’ Category

Windows 7 Ribbon – Part 2 – How handle ribbon control events?

July 2nd, 2009 Sarath Comments

In this installement, let’s see how to handle the events of the ribbon control. I strongly reconmmend you to read the previous post on the basics of ribbon the way it’s being created. This is a continuation of the previous post.

To handle the events, the IUICommandHandler interface is implemented by the application and defines the Command handler methods for framework events.  The following function has to be implemented in the derived class.

Execute
Executes or previews the Commands bound to the Command hand`ler.

UpdateProperty
Sets a property value for a bound Command, for example, setting a Command to enabled or disabled depending on the state of a View.

For each Command in a View(Application.Views in the XML file), the Ribbon framework requires a corresponding Command handler in the host application. A new handler or an existing handler must be bound to the Command through the IUIApplication::OnCreateUICommand notification method.  This method is executed when the UI component is created. It’s possible create new command handler by querying IID_PPV_ARGS intefac. Any number of Commands can be bound to a Command handler.

The Command handler serves two purposes. First, it can update the values of properties for any command to which it is bound, such as setting a command to enabled or disabled. Second, it can execute or preview any commands to which it is bound.

In the previous instalment we’ve seen CRibbonImplementer class. So here we will be modifying the class. We’ll be creating the the handler

Step 1 – Include the generated .h file contains control IDs to the implementation .h/.cpp file of CRibbonImplementer

b1

Step 2 & 3 – Derive ribbon implementer class from IUICommandHandler and add the interface to COM Map

b2

Step 4 – Modify OnCreateUICommand function and add UI Handler on creating the control.

b3

Step 5 -  Add Execute handler to get notification when the button is clicked. This is like the normal message loop of a Win32 message loop system.b4

The final Step (6 ) – It’s necessary to implement IUICommandHandler::UpdateProperty as the base class doesn’t provide any implementations. We can leave this interface as unimplemented.

b5

The full Source code is given below. There’s no change in the other part of source code.

[sourcecode language='cpp']
#include “stdafx.h”
#include
#include
#include
#include
// Step 1: Include menu ribbon resource.h
#include “MenuRibbonRes.h”

class CRibbonImplementer:
public CComObjectRootEx,
public IUIApplication,
// Step 2: derive fromm IUICommandHandler
public IUICommandHandler
{
public:
BEGIN_COM_MAP(CRibbonImplementer)
COM_INTERFACE_ENTRY(IUIApplication)
// Step 3: IUICommandHandler add in teh COM Map
COM_INTERFACE_ENTRY(IUICommandHandler)
END_COM_MAP()

STDMETHOD(OnCreateUICommand)(UINT32 nCmdID, __in UI_COMMANDTYPE typeID, __deref_out IUICommandHandler** ppCommandHandler)
{
// Step 4: IUICommandHandler
// if my button is being created, the handler is created and attached
if (nCmdID == cmdMyButton)
{
return QueryInterface(IID_PPV_ARGS(ppCommandHandler));
}
return E_NOTIMPL;
}

/* Step 5: Implement execute function.
This function will be called on clicking
the controls attached to command handler */
STDMETHODIMP Execute(UINT nCmdID,
UI_EXECUTIONVERB verb,
__in_opt const PROPERTYKEY* key,
__in_opt const PROPVARIANT* ppropvarValue,
__in_opt IUISimplePropertySet* pCommandExecutionProperties)
{
HRESULT hr = S_OK;
switch (verb)
{
case UI_EXECUTIONVERB_EXECUTE:
if (nCmdID == cmdMyButton)
{
MessageBox(NULL, _T( “Clicked on My Button!” ),
_T(”My Button Execute”), MB_OK);
}
break;
}

return hr;

}

// unimplemented methods
// Step 6: Implement Update Property interface as well
STDMETHODIMP UpdateProperty(UINT nCmdID,
__in REFPROPERTYKEY key,
__in_opt const PROPVARIANT* ppropvarCurrentValue,
__out PROPVARIANT* ppropvarNewValue)
{
return E_NOTIMPL;
}

STDMETHOD(OnViewChanged)(UINT32 nViewID, __in UI_VIEWTYPE typeID, __in IUnknown* pView, UI_VIEWVERB verb, INT32 uReasonCode)
{
return E_NOTIMPL;
}

STDMETHOD(OnDestroyUICommand)(UINT32 commandId,
__in UI_COMMANDTYPE typeID,
__in_opt IUICommandHandler* pCommandHandler)
{
return E_NOTIMPL;
}

STDMETHODIMP UpdateProperty(UINT nCmdID,
__in REFPROPERTYKEY key,
__in_opt const PROPVARIANT* ppropvarCurrentValue,
__out PROPVARIANT* ppropvarNewValue)
{
return E_NOTIMPL;
}
};
[/sourcecode]

Windows 7 Ribbon – Part 1 – How to Integrate a Simple Ribbon to your MFC Application?

June 30th, 2009 Sarath Comments

The Office 2007 changed the the conventional menu to a new vibrant, beautiful, and useful(??) ribbons. After that many third party libraries (both commercial and non commercial) vendors provided components to integrate ribbons to our application. Finally Microsoft heed the MFC guys crying a loud to get support on ribbons. Microsoft included Ribbons and other office style controls with MFC Feature Pack for Visual Studio 2008.

The following figure depicts the anatomy of a typical Ribbon.

image

In the new version of Windows, Windows 7 , it supports ribbons natively and Microsoft allows us to create it using Ribbon Frame Work.  Few Accessories application like MS Paint, Wordpad etc. got UI lift with Windows Ribbons.

Developer can create ribbon using Ribbon Markup Language (similar to XML format). For the Windows Ribbon framework (Ribbon) to consume the Ribbon markup file, the markup file must be compiled into a binary format resource file. A dedicated Ribbon markup compiler, the UI Command Compiler (UICC), is included with the Microsoft Windows Software Development Kit (SDK) (7.0 or later) for this purpose. In addition to compiling the binary version of the Ribbon markup, the UICC generates an ID definition header file that exposes all markup elements to the Ribbon host application and a resource file that is used to link the binary markup to the host application at build time.

The workflow for the Ribbon is as follows

Ribbon-Flow

OK let’s create a simple ribbon step by step. You should have Windows 7 SDK installed and Visual Studio 2005 or above is required to compile this application. I’m taking a MFC Application do this instead of a Win32 application. I’m just creating a new MFC Dialog Based Application.

We can categorize the ribbon creation in to two categories.

  • XML Markup, used to define the Ribbon structure and organization of controls
  • C++ COM interfaces, used to initialize and handle events

Step 1: Create the XML file with your ribbon. Add this XML for the solution. The following XML is a simple ribbon contains the application a Tab,Group and a button inside it.

1

[sourcecode language='xml']









My Button
My Button











How to make Windows 7 taskbar like Windows Vista

March 31st, 2009 Sarath Comments

As we’ve seen, Windows vista has a whole revamped taskbar. Windows Vista, still follows the old kind of taskbar which we’ve been seeing since the initial version of Windows. Because of this reason, lot of people are not comfortable using the new taskbar. Or some guys are not ready to accept the changes. So for them, here’s tip to change Windows 7 taskbar similar to Windows Vista. But functionally it’s not really similar to windows Vista, it’s ‘almost a replica’ of it.

image

What you’re seeing above is an original Windows 7 taskbar. Windows provides ways to modify it’s appearance.

Right Click on taskbar->Properties. From the appearing dialog, chose “User Small Icons” (it’s again according to your convenience). The also opt for “Combine when toolbar is full” option from Toolbar buttons combo. See the image below.

TBPPT

The resultant taskbar may appear as followsimage

Thanks Tim Sneath for his great tips

 

Categories: Tips, Windows 7, Windows Vista Tags: ,

How to Create a drop down button in Windows Vista, 7?

February 19th, 2009 Sarath Comments

MenuIn this post, I’m introducing the new button style which is introduced in Windows Vista and above. It’s no more painful to create a Drop down button with Windows Vista, earlier it was accomplished by placing two controls side by side and handle it’s message separately.

Windows Vista introduced new button style BS_SPLITBUTTON and you can set this new style by specifying as parameter of CreateWindow API if you’re dynamically creating button control or by modifying the style using ModifyStyle API. To use this style Windows Vista or higher required .


Using with CreateWindow/CButton::Create API

[sourcecode language='cpp']
HWND hWndButtonMain = CreateWindow(_T(”Button”), _T(”Split Button”),
WS_CHILD | WS_VISIBLE | BS_SPLITBUTTON,
10, 10, 120, 50,
m_hWnd, (HMENU)IDC_BTN_BUTTON1, AfxGetApp()->m_hInstance, NULL);
[/sourcecode]

Or

[sourcecode language='cpp']
CButton* pButton = new CButton;
pButton->Create( _T( “Split Button”), WS_CHILD | WS_VISIBLE | BS_SPLITBUTTON,CRect(10,10,120,50),this,IDC_BUTTON1);
[/sourcecode]

Or modifying Existing control

[sourcecode language='cpp']
m_SplitButton.ModifyStyle(0, BS_SPLITBUTTON );
[/sourcecode]

Handling Dropdown Event

Add message handler for BCN_DROPDOWN in your message handler or in message map of your MFC window class

[sourcecode language='cpp']
ON_NOTIFY(BCN_DROPDOWN, IDC_BUTTON1, &CControlSampleDlg::OnBnDropDownButton1)
[/sourcecode]

In the message Handler, Display the popup menu and the menu item can be handled using ON_COMMAND message with associated menu ID.

[sourcecode language='cpp']
void CControlSampleDlg::OnBnDropDownButton1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMBCDROPDOWN pDropDown = reinterpret_cast(pNMHDR);

Button_SetDropDownState( m_SplitButton, TRUE );

RECT rc;

// Get the bounding rectangle of the client area
::GetWindowRect(m_SplitButton, &rc);

HMENU hmenu = LoadMenu(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDR_MENU1));
HMENU hmenuTrackPopup;

hmenuTrackPopup = GetSubMenu(hmenu, 0);
TrackPopupMenuEx(hmenuTrackPopup, TPM_RIGHTBUTTON, rc.left, rc.bottom, m_hWnd, NULL);
DestroyMenu(hmenu);

Button_SetDropDownState( m_SplitButton, FALSE );
*pResult = 0;
}
[/sourcecode]

Windows 7 Taskbar

February 18th, 2009 Sarath Comments

The surprising new feature for me in Windows 7 was it’s taskbar. It’s excellent and very user friendly (still have some pains of shifting from old windows taskbar).

On Windows 7 Feature Focus, Paul Thurrot reveals about the revamped Window 7 taskbar. An excellent article. Read it.

Technorati Tags: ,

Virtual CD/DVD for Windows 7 (beta)

January 20th, 2009 Sarath Comments

After installing Windows 7 in my laptop, I had to use some of my DVD image files. As usual I tried installing Magic Disc which is my favorite Virtual DVD Drive. The installation had shown up some problems and finally somehow I was able to launch Magic Disc but I failed to map the image file and instead I tried Virtual Clone Drive. Now things are working fine but Virtual Clone Drive is not having much options and easiness of use like Magic Disc. Bye Magic Disc at least till you fix the problems with Win7.

Update[3/15/2009 - 10:06 AM IST] : My all-time-favorite Magic Disc is now supporting Windows 7. You can download it from their website.

Categories: Windows 7, Windows Vista Tags: