這一夜我們只說CreateCompatibleDC

需要在Windows Mobile和Symbian OS上搭一個抽象層來對GDI有個基本的封裝.定義了一個Graphics的類.原意是讓他每次畫圖的時候在memory DC上畫,最後update的時候可以整體把Memory DC bitblt到目標HDC上去.

下面就是一個簡要的initGraphis的函數.粗粗的看好像平平無奇.

就是創建了memory dc, 設置了bitmap object. 有了這個東西之後,後面的一切畫圖的操作,我們都已m_pMemory爲DC來話,在update函數的時候,我們就吧m_pMemory渲染到m_pDevice上面去.

下面是一個畫矩形的函數和Update函數.

一起都挺和諧的,也以爲肯定可以工作了.但是問題來了.在外部調用了g.drawRect(rc, RGB_GREEN);的時候.矩形是看到了.但是並沒有看到綠色的矩形.只是一個黑色的矩形!!!!不像是我們的RGB_GREEN宏定義的有問題,因爲實際上調用的就是系統的RGB宏來做的.那到底是什麼問題呢?那個SolidBrush無效? 但是無效如何能畫呢? 現在的問題只是顏色不對啊!?

終於在MSDN上找到的了原因.和自己想的果然很有差距:

A memory DC exists only in memory. When the memory DC is created, its display surface is exactly one monochrome pixel wide and one monochrome pixel high. Before an application can use a memory DC for drawing operations, it must select a bitmap of the correct width and height into the DC. To select a bitmap into a DC, use the CreateCompatibleBitmap function, specifying the height, width, and color organization required.

然後在CreateCompatibleBitmap裏面說到了:

Note: When a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected into it. If this memory device context is used in CreateCompatibleBitmap, the bitmap that is created is a monochrome bitmap. To create a color bitmap, use the hDC that was used to create the memory device context, as shown in the following code:


看到了這些才知道自己太弱了.原來CreateCompatibleDC的時候創建的memory DC只是黑白的.那怎麼讓他和系統的DC一致呢?只要創建一個和系統DC compatible的bitmap,然後設給這個Memory DC就行了.原來我的那段代碼之所以不工作.我創建的bitmap的時候用了

m_pMemBmp = CreateCompatibleBitmap(m_pMemory, rc.dx, rc.dy);這樣就變成了創建了一個黑白的bitmap.當然畫上去的一些帶顏色的東西都變成黑白的啦!




 

發佈了70 篇原創文章 · 獲贊 9 · 訪問量 47萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章