• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-341  評(píng)論-2670  文章-0  trackbacks-0

            由于接下去要用uniscribe(這是一個(gè)可以告訴我們?cè)阡秩疽粋€(gè)超長(zhǎng)unicode字符串的時(shí)候,什么地方可以換行,什么地方要換順序,什么字符要用一種神奇的方法來(lái)渲染之類的庫(kù))做可以插入圖片和其它亂七八糟東西的rich text box,為了更方便做實(shí)驗(yàn),而且也考慮到很多軟件也需要直接繪圖的功能,所以我寫(xiě)了這么兩個(gè)Demo:

            1、Rendering.RawAPI.GDI(http://www.gaclib.net/Demos/Rendering.RawAPI.GDI/Demo.html
            2、Rendering.RawAPI.Direct2D(
            http://www.gaclib.net/Demos/Rendering.RawAPI.Direct2D/Demo.html

            由于這兩個(gè)Demo很像,而且Direct2D的比較復(fù)雜,所以我在這里介紹一下這個(gè)Direct2D的demo。

            在Demo里面可以看到,我們可以使用GuiGDIElement或者GuiDirect2DElement來(lái)進(jìn)行手工的繪圖操作。這兩個(gè)Element的使用有限制。當(dāng)GacUI使用GDI繪圖(SetupWindowsGDIRenderer)的時(shí)候才可以使用GuiGDIElement,對(duì)于Direct2D也是一樣的。在使用它們進(jìn)行繪圖的時(shí)候,坐標(biāo)用的是窗口的坐標(biāo)。但是GacUI會(huì)在繪制的時(shí)候先加入一個(gè)clip,這樣我們?cè)诶L制的時(shí)候就算繪制出了邊界,也不會(huì)有任何不好的影響。而且這個(gè)clip的矩形范圍會(huì)在渲染事件觸發(fā)的時(shí)候給出。在這里我們先來(lái)看一下Direct2D的demo。

            首先,整個(gè)程序的框架是這樣子的:

            #include "..\..\Public\Source\GacUI.h"
            #include <math.h>
            #include <Windows.h>

            int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int CmdShow)
            {
                // SetupWindowsDirect2DRenderer() is required for GuiDirect2DElement
                return SetupWindowsDirect2DRenderer();
            }

            class Direct2DWindow : public GuiWindow
            {
            protected:

                // arguments.rt is ID2D1RenderTarget.
                void element_Rendering(GuiGraphicsComposition* sender, GuiDirect2DElementEventArgs& arguments)
                {
                }

                // The render target is going to be destroyed, any binded resources should be released.
                void element_BeforeRenderTargetChanged(GuiGraphicsComposition* sender, GuiDirect2DElementEventArgs& arguments)
                {
                }

                // The new render target is prepared, any binded resources are allowed to recreate now.
                void element_AfterRenderTargetChanged(GuiGraphicsComposition* sender, GuiDirect2DElementEventArgs& arguments)
                {
                }
            public:
                Direct2DWindow()
                    :GuiWindow(GetCurrentTheme()->CreateWindowStyle())
                {
                    SetText(L"Rendering.RawAPI.Direct2D");
                    SetClientSize(Size(640, 480));
                    GetBoundsComposition()->SetPreferredMinSize(Size(640, 480));
                    MoveToScreenCenter();
                    {
                        GuiDirect2DElement* element=GuiDirect2DElement::Create();
                       
            element->Rendering.AttachMethod(this, &Direct2DWindow::element_Rendering);
                       
            element->BeforeRenderTargetChanged.AttachMethod(this, &Direct2DWindow::element_BeforeRenderTargetChanged);
                        element->AfterRenderTargetChanged.AttachMethod(this, &Direct2DWindow::element_AfterRenderTargetChanged);

                        GuiBoundsComposition* composition=new GuiBoundsComposition;
                        composition->SetAlignmentToParent(Margin(0, 0, 0, 0));
                        composition->SetOwnedElement(element);
                        GetContainerComposition()->AddChild(composition);
                    }
                }
            };

            void GuiMain()
            {
                Direct2DWindow window;
                GetApplication()->Run(&window);
            }

            在構(gòu)造函數(shù)里面,我們創(chuàng)建了一個(gè)GuiDirect2DElement,然后把它放進(jìn)一個(gè)會(huì)自動(dòng)充滿整個(gè)窗口的composition里面。然后我們需要監(jiān)聽(tīng)三個(gè)事件(GDI只有一個(gè),就是Rendering):
            1、Rendering。這個(gè)事件在窗口被繪制的時(shí)候調(diào)用。GacUI才用了一個(gè)低功耗的方法讓程序不斷的繪制自己,所以我們并不需要擔(dān)心“如何刷新窗口”的這個(gè)問(wèn)題。
            2、BeforeRenderTargetChanged。在這個(gè)時(shí)候我們要清理掉我們創(chuàng)建出來(lái)的資源,譬如說(shuō)畫(huà)刷等等。
            3、AfterRenderTargetChanged。在這個(gè)時(shí)候我們要建立一些繪圖資源,譬如說(shuō)畫(huà)刷等等。

            為什么下面兩個(gè)函數(shù)那么蛋疼呢?因?yàn)镈irect2D的類似畫(huà)刷這樣的東西,是必須跟一個(gè)ID2D1RenderTarget綁定在一起的,不同的render target之間的畫(huà)刷不能共享。而且那個(gè)可憐的render target還有可能會(huì)失效,這個(gè)時(shí)候GacUI就要重新創(chuàng)建他們。所以無(wú)論如何,都必須監(jiān)聽(tīng)這三個(gè)對(duì)象,除非我們只用GuiDirect2DElement來(lái)渲染文字(因?yàn)槲淖窒嚓P(guān)的資源是IDWriteFactory控制的,跟render target無(wú)關(guān))。

            在這個(gè)Demo里面,我們要畫(huà)的是一個(gè)會(huì)動(dòng)的鐘。在這個(gè)鐘里面我們要繪制4種線形:邊框、時(shí)針、分針、秒針。因此我們需要4個(gè)不同的ID2D1SolidColorBrush。由于操作COM對(duì)象的時(shí)候總要去記得操作那個(gè)引用計(jì)數(shù),特別的麻煩,而且還容易忘掉。所以我特地為大家提供了一個(gè)叫做ComPtr的東西。所以我們就可以這么聲明、創(chuàng)建和釋放他們:

            ComPtr<ID2D1SolidColorBrush>            borderBrush;
            ComPtr<ID2D1SolidColorBrush>            secondBrush;
            ComPtr<ID2D1SolidColorBrush>            minuteBrush;
            ComPtr<ID2D1SolidColorBrush>            hourBrush;

            // The render target is going to be destroyed, any binded resources should be released.
            void element_BeforeRenderTargetChanged(GuiGraphicsComposition* sender, GuiDirect2DElementEventArgs& arguments)
            {
                borderBrush=0;
                secondBrush=0;
                minuteBrush=0;
                hourBrush=0;
            }

            // The new render target is prepared, any binded resources are allowed to recreate now.
            void element_AfterRenderTargetChanged(GuiGraphicsComposition* sender, GuiDirect2DElementEventArgs& arguments)
            {
                ID2D1SolidColorBrush* brush;
                {
                    brush=0;
                    arguments.rt->CreateSolidColorBrush(D2D1::ColorF(0.0f, 0.0f, 0.0f), D2D1::BrushProperties(), &brush);
                    borderBrush=brush;
                }
                {
                    brush=0;
                    arguments.rt->CreateSolidColorBrush(D2D1::ColorF(0.0f, 0.0f, 1.0f), D2D1::BrushProperties(), &brush);
                    secondBrush=brush;
                }
                {
                    brush=0;
                    arguments.rt->CreateSolidColorBrush(D2D1::ColorF(0.0f, 1.0f, 0.0f), D2D1::BrushProperties(), &brush);
                    minuteBrush=brush;
                }
                {
                    brush=0;
                    arguments.rt->CreateSolidColorBrush(D2D1::ColorF(1.0f, 0.0f, 0.0f), D2D1::BrushProperties(), &brush);
                    hourBrush=brush;
                }
            }

            想必大家都應(yīng)該看清楚了。Before和After事件里面,GacUI都會(huì)提供用來(lái)繪圖的ID2D1RenderTarget,這個(gè)時(shí)候必須正確的創(chuàng)建和釋放資源。只要這些資源都建立了起來(lái),那么剩下的就只有把一個(gè)時(shí)鐘畫(huà)出來(lái)了。畫(huà)一個(gè)時(shí)鐘還是很容易的,只需要那么幾行代碼就行了:

            static const int                        Radius=200;
            static const int                        LongScale=10;
            static const int                        ShortScale=5;
            static const int                        SecondLength=180;
            static const int                        MinuteLength=150;
            static const int                        HourLength=120;

            float GetAngle(float second)
            {
                return (second-15.0f)*3.1416f/30.0f;
            }

            void DrawLine(ID2D1RenderTarget* rt, ComPtr<ID2D1SolidColorBrush> brush, float angle, int width, int startLength, int endLength, int x, int y)
            {
                float s=sin(angle);
                float c=cos(angle);
                float x1=(c*startLength)+(float)(x+Radius);
                float y1=(s*startLength)+(float)(y+Radius);
                float x2=(c*endLength)+(float)(x+Radius);
                float y2=(s*endLength)+(float)(y+Radius);
                rt->DrawLine(D2D1::Point2F(x1, y1), D2D1::Point2F(x2, y2), brush.Obj(), (float)width);
            }

            // arguments.rt is ID2D1RenderTarget.
            void element_Rendering(GuiGraphicsComposition* sender, GuiDirect2DElementEventArgs& arguments)
            {
                int w=arguments.bounds.Width();
                int h=arguments.bounds.Height();
                int x=arguments.bounds.Left()+(w-Radius*2)/2;
                int y=arguments.bounds.Left()+(h-Radius*2)/2;

                arguments.rt->DrawEllipse(D2D1::Ellipse(D2D1::Point2F((float)(x+Radius), (float)(y+Radius)), (float)Radius, (float)Radius), borderBrush.Obj());
                for(int i=0;i<60;i++)
                {
                    int scale=i%5==0?LongScale:ShortScale;
                    float angle=GetAngle((float)i);
                    DrawLine(arguments.rt, borderBrush, angle, 1, Radius-scale, Radius, x, y);
                }

                DateTime dt=DateTime::LocalTime();
                {
                    float angle=GetAngle(dt.hour*5+dt.minute/12.0f+dt.second/720.0f);
                    DrawLine(arguments.rt, hourBrush, angle, 5, 0, HourLength, x, y);
                }
                {
                    float angle=GetAngle(dt.minute+dt.second/60.0f);
                    DrawLine(arguments.rt, minuteBrush, angle, 3, 0, MinuteLength, x, y);
                }
                {
                    float angle=GetAngle((float)dt.second);
                    DrawLine(arguments.rt, secondBrush, angle, 1, 0, SecondLength, x, y);
                }
            }

            然后我們就獲得了下圖:(LiveWrite真是太好了,cppblog的傻逼編輯器每次插入圖片都會(huì)插入到一個(gè)詭異的位置中去)

            DXGUI_58

            這樣我們就完成了一個(gè)時(shí)鐘的制作了,而且也學(xué)會(huì)了如何在GacUI里面直接使用GDI和Direct2D繪圖了。

            posted on 2012-11-05 07:14 陳梓瀚(vczh) 閱讀(4558) 評(píng)論(2)  編輯 收藏 引用 所屬分類: 2DGacUI

            評(píng)論:
            # re: 一個(gè)在GacUI上直接使用GDI或者Direct2D進(jìn)行繪圖操作的小demo 2012-11-05 07:30 | DiryBoy
            先沙發(fā)~~  回復(fù)  更多評(píng)論
              
            # re: 一個(gè)在GacUI上直接使用GDI或者Direct2D進(jìn)行繪圖操作的小demo 2012-12-10 18:38 | 畢達(dá)哥拉斯半圓
            在有個(gè)做小軟件的機(jī)會(huì),嘗試了一下GacUI,暫時(shí)沒(méi)有找到主窗口的消息循環(huán)來(lái)響應(yīng)每個(gè)控件的消息,這些控件之間還要交換數(shù)據(jù)和消息等等。我應(yīng)該咋作?  回復(fù)  更多評(píng)論
              
            欧美黑人激情性久久| 精品无码人妻久久久久久| 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | 一97日本道伊人久久综合影院| 久久久久国产精品| 久久久久成人精品无码中文字幕 | 亚洲综合久久夜AV | 久久精品亚洲精品国产欧美| 久久综合伊人77777| 久久久久久国产a免费观看黄色大片 | 一本色道久久99一综合| 精品蜜臀久久久久99网站| 久久综合九色综合精品| 久久A级毛片免费观看| 久久精品国产亚洲AV无码麻豆 | 久久久精品久久久久特色影视 | 国产伊人久久| 久久精品国产一区二区| 久久久久亚洲精品中文字幕 | 99久久精品免费看国产一区二区三区| 精品视频久久久久| 欧美精品福利视频一区二区三区久久久精品 | 欧美丰满熟妇BBB久久久| 精品久久久久久国产潘金莲| 欧美精品一本久久男人的天堂| 久久久WWW成人免费毛片| 一本色道久久综合| 麻豆AV一区二区三区久久 | 久久精品国产免费| 久久婷婷五月综合国产尤物app | 伊人久久精品影院| 久久精品国产清高在天天线| 88久久精品无码一区二区毛片| 亚洲午夜久久久| 99re这里只有精品热久久| 久久久久久久久波多野高潮| 国产精品一区二区久久精品| 久久久久黑人强伦姧人妻| 久久成人国产精品| 色婷婷噜噜久久国产精品12p| 久久精品国产亚洲沈樵|