Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 13:57, 6 July 2008.

自定义控件: 聚焦(四)

From Forum Nokia Wiki


ID CS000868 Creation date March 28, 2008
Platform S60 3rd Edition, FP1 Tested on devices Nokia N95
Category Symbian C++ Subcategory UI


Keywords (APIs, classes, methods, functions): CCoeControl, TKeyEvent

概述

这个代码片段演示如何添加摁键事件处理到自定义的容器控件以及如何改变所选部件的焦点。

本例扩展已存代码片段自定义控件: 容器控件(三)。查看本文的“参见部分”以了解自定义控件系列的其它代码片段。

CMyContainerControl - 头文件

CMyContainerControl部件的头文件添加三个方法以便接收摁键事件和改变焦点。

private:
    TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType);
public:
    void MoveFocusUp();
    void MoveFocusDown();

CMultiViewsView1 - 源代码

CMyContainerControl必须加到控件栈中以便接收摁键事件AppUi()->AddToStackL().

// Create Compound control
iContainerControl = CMyContainerControl::NewL(ClientRect());
 
// Create control to the compound container
CMyControl* control1 = CMyControl::NewLC(ClientRect(),iContainerControl);
iContainerControl->AddControlL(control1,1);
CleanupStack::Pop(); //control 1
 
// Add the control to the control stack
AppUi()->AddToStackL(iContainerControl);

CMyContainerControl - 源代码

OfferKeyEventL()捕捉击键事件EKeyUpArrowEKeyDownArrow以便改变所选部件的焦点。 MoveFocusUp()MoveFocusDown()CCoeContainer::SetFocus()改变活动部件焦点到另一个部件。

TKeyResponse CMyContainerControl::OfferKeyEventL
(const TKeyEvent& aKeyEvent,TEventCode /*aType*/)
    {
    // Move focus
    switch ( aKeyEvent.iCode )
        {
        case EKeyUpArrow:
            {
            MoveFocusUp();
            DrawNow(); // Draw components again
            return EKeyWasConsumed;
            }
        case EKeyDownArrow:
            {
            MoveFocusDown();
            DrawNow(); // Draw components again         
            return EKeyWasConsumed;
            }
        default:
            {
            return EKeyWasNotConsumed;
            }
        }
    }
 
void CMyContainerControl::MoveFocusUp()
    {
    CCoeControlArray::TCursor cursor = Components().Begin();
    CCoeControl* ctrl = NULL;
    CCoeControl* prevCtrl = NULL;
    while ((ctrl = cursor.Control<CCoeControl>()) != NULL)
        {
        if (ctrl->IsFocused())
            {
            if (prevCtrl)
                {
                // Set focus to previous control
                ctrl->SetFocus(EFalse);
                prevCtrl->SetFocus(ETrue);
                break;
                }
            else
                {
                break; // First control is already focused
                }
            }
        prevCtrl = ctrl;
        cursor.Next();
        }
    }
 
void CMyContainerControl::MoveFocusDown()
    {
    CCoeControlArray::TCursor cursor = Components().Begin();
    CCoeControl* ctrl = NULL;
    CCoeControl* nextCtrl = NULL;
    while ((ctrl = cursor.Control<CCoeControl>()) != NULL)
        {
        if (ctrl->IsFocused())
            {
            cursor.Next();
            nextCtrl = cursor.Control<CCoeControl>();
            if (nextCtrl)
                {
                // Set focus to next control
                ctrl->SetFocus(EFalse);
                nextCtrl->SetFocus(ETrue);
                break;
                }
            else
                {
                break; // Last control is already focused
                }
            }
        cursor.Next();
        }
    }

绘制焦点

在自定义控件CMyControl::Draw()方法中绘制焦点矩形,Draw方法调用检查部件焦点的CMyControl::DrawFocusFrame()方法。 若部件有焦点,就用CWindowGc::DrawRoundRect()来绘制。

后置条件

CMyContainerControl接受击键事件并显示活动部件焦点。

参见

自定义控件系列:

 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtopicQUqfnTopicZseriesE5f60Q
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX