GacUI Demo:使用Label控件模仿超链接

GacUI Demo:使用Label控件模仿超链接
    今天我给 GacUI添加了一个新Demo。我发现写Demo也是一个测试的过程,可以用来检验类库提供的API是否够完整。前面这两个Demo都促使我往类库里面加入了新的函数。这次的Demo是用Label控件来模仿超链接。下载最新代码之后,可以在“Libraries\GacUI\GacUIDemo\GacUIDemo.sln”下面找到最新的Demo代码。

    为了模仿超链接,我们要做两件事情。第一件事情就是鼠标悬浮在Label上的时候需要显示出手的光标图,第二件事情就是在鼠标进入Label的时候显示下划线,离开的时候去掉下划线。因此我们需要监听三个事件,分别是MouseEnter,MouseLeave和LeftButtonDown。下面是Demo的图:

    上图:鼠标在Label外。下图:鼠标在Label内。单击Label的时候会打开浏览器。



    代码如下:

#include  " ..\..\Public\Source\GacUIIncludes.h "
#include 
< Windows.h >

int  CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,  int  CmdShow)
{
    
return  SetupWindowsDirect2DRenderer();
}

class  HyperlinkWindow :  public  GuiWindow
{
private :
    GuiLabel
*                 labelHyperlink;

    
void  labelHyperlink_OnMouseEnter(GuiGraphicsComposition *  sender, GuiEventArgs &  arguments)
    {
        FontProperties font
= labelHyperlink -> GetFont();
        font.underline
= true ;
        labelHyperlink
-> SetFont(font);
    }

    
void  labelHyperlink_OnMouseLeave(GuiGraphicsComposition *  sender, GuiEventArgs &  arguments)
    {
        FontProperties font
= labelHyperlink -> GetFont();
        font.underline
= false ;
        labelHyperlink
-> SetFont(font);
    }

    
void  labelHyperlink_OnLeftButtonDown(GuiGraphicsComposition *  sender, GuiMouseEventArgs &  arguments)
    {
        ShellExecute(NULL, L
" OPEN " , L " http://www.cppblog.com/vczh " , NULL, NULL, SW_SHOWNORMAL);
    }
public :
    HyperlinkWindow()
        :GuiWindow(GetCurrentTheme()
-> CreateWindowStyle())
    {
        SetText(L
" Controls.Label.Hyperlink " );
        SetClientSize(Size(
300 200 ));
        MoveToScreenCenter();

        labelHyperlink
= g::NewLabel();
        labelHyperlink
-> SetText(L " http://www.cppblog.com/vczh " );
        labelHyperlink
-> SetTextColor(Color( 0 0 255 ));
        {
            FontProperties font;
            font.fontFamily
= L " Segoe UI " ;
            font.size
= 18 ;
            font.antialias
= true ;
            labelHyperlink
-> SetFont(font);
        }
        {
            INativeCursor
*  hand = GetCurrentController() -> ResourceService() -> GetSystemCursor(INativeCursor::Hand);
            labelHyperlink
-> GetBoundsComposition() -> SetAssociatedCursor(hand);
        }
        labelHyperlink
-> GetEventReceiver() -> mouseEnter.AttachMethod( this & HyperlinkWindow::labelHyperlink_OnMouseEnter);
        labelHyperlink
-> GetEventReceiver() -> mouseLeave.AttachMethod( this & HyperlinkWindow::labelHyperlink_OnMouseLeave);
        labelHyperlink
-> GetEventReceiver() -> leftButtonDown.AttachMethod( this & HyperlinkWindow::labelHyperlink_OnLeftButtonDown);
        AddChild(labelHyperlink);
    }

    
~ HyperlinkWindow()
    {
    }
};

void  GuiMain()
{
    GuiWindow
*  window = new  HyperlinkWindow();
    GetApplication()
-> Run(window);
    delete window;
}

    这里展示的主要是监听事件的方法。在使用control->GetEventReceiver()->event的时候,可以使用Attach、AttachMethod、AttachFunction和AttachLambda。AttachLambda传入一个functor,在C++11里面可以直接使用lambda表达式。在这里使用AttachMethod给一个事件绑定类成员函数。C++运行类成员函数的时候,不仅需要参数,还需要一个this对象,所以AttachMethod有两个参数,使用方法在Demo里面已经展现出来了。

    在这里还引入了GetCurrentController函数。GetCurrentController返回的INativeController对象抽象了所有需要的操作系统的功能,其中获得一个光标的对象就封装在了ResourceService里面。INativeController还包含了很多其他的Service,这个留在以后的Demo展示。

你可能感兴趣的:(GacUI Demo:使用Label控件模仿超链接)