• <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  評論-2670  文章-0  trackbacks-0

            由于接下去要用uniscribe(這是一個可以告訴我們在渲染一個超長unicode字符串的時候,什么地方可以換行,什么地方要換順序,什么字符要用一種神奇的方法來渲染之類的庫)做可以插入圖片和其它亂七八糟東西的rich text box,為了更方便做實驗,而且也考慮到很多軟件也需要直接繪圖的功能,所以我寫了這么兩個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

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

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

            首先,整個程序的框架是這樣子的:

            #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);
            }

            在構造函數里面,我們創建了一個GuiDirect2DElement,然后把它放進一個會自動充滿整個窗口的composition里面。然后我們需要監聽三個事件(GDI只有一個,就是Rendering):
            1、Rendering。這個事件在窗口被繪制的時候調用。GacUI才用了一個低功耗的方法讓程序不斷的繪制自己,所以我們并不需要擔心“如何刷新窗口”的這個問題。
            2、BeforeRenderTargetChanged。在這個時候我們要清理掉我們創建出來的資源,譬如說畫刷等等。
            3、AfterRenderTargetChanged。在這個時候我們要建立一些繪圖資源,譬如說畫刷等等。

            為什么下面兩個函數那么蛋疼呢?因為Direct2D的類似畫刷這樣的東西,是必須跟一個ID2D1RenderTarget綁定在一起的,不同的render target之間的畫刷不能共享。而且那個可憐的render target還有可能會失效,這個時候GacUI就要重新創建他們。所以無論如何,都必須監聽這三個對象,除非我們只用GuiDirect2DElement來渲染文字(因為文字相關的資源是IDWriteFactory控制的,跟render target無關)。

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

            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;
                }
            }

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

            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的傻逼編輯器每次插入圖片都會插入到一個詭異的位置中去)

            DXGUI_58

            這樣我們就完成了一個時鐘的制作了,而且也學會了如何在GacUI里面直接使用GDI和Direct2D繪圖了。

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

            評論:
            # re: 一個在GacUI上直接使用GDI或者Direct2D進行繪圖操作的小demo 2012-11-05 07:30 | DiryBoy
            先沙發~~  回復  更多評論
              
            # re: 一個在GacUI上直接使用GDI或者Direct2D進行繪圖操作的小demo 2012-12-10 18:38 | 畢達哥拉斯半圓
            在有個做小軟件的機會,嘗試了一下GacUI,暫時沒有找到主窗口的消息循環來響應每個控件的消息,這些控件之間還要交換數據和消息等等。我應該咋作?  回復  更多評論
              
            久久久久久久久久久| 午夜精品久久久久成人| 久久国产精品-久久精品| 国产精品视频久久久| 久久精品国产只有精品66| 老男人久久青草av高清| 7777久久亚洲中文字幕| 欧美色综合久久久久久| 精品国产乱码久久久久久郑州公司| 99精品久久精品一区二区| 性做久久久久久免费观看| 久久精品人成免费| 内射无码专区久久亚洲| 国产精品久久久久影院嫩草| 亚洲国产精品无码久久青草| 欧美久久综合性欧美| 精品久久亚洲中文无码| 国产精品99久久久久久董美香| 久久人妻无码中文字幕| 久久久久免费视频| 91精品国产91热久久久久福利| 无码人妻精品一区二区三区久久久 | 99久久综合国产精品免费| 伊人久久精品无码av一区| 无码人妻精品一区二区三区久久久| 久久国产一片免费观看| 国产精品一区二区久久精品| 国产色综合久久无码有码| 四虎国产精品成人免费久久| 久久成人精品| 国产精品久久久天天影视香蕉| 97久久超碰国产精品2021| 久久亚洲私人国产精品| 综合久久精品色| 免费无码国产欧美久久18| 国产69精品久久久久观看软件| 青青热久久国产久精品 | 久久www免费人成看片| 国产精品综合久久第一页| 国产 亚洲 欧美 另类 久久| 99久久精品免费|