同往常一樣,先放一章效果圖,這鴨子是會(huì)飛的哦。

效果類似常見的QQ寵物、360精靈之類的。
不說廢話了,還是說重點(diǎn)。
==================================================
要實(shí)現(xiàn)這種效果,我的第一想法便是通過SetWindowRGN設(shè)置不規(guī)則窗體,然后更新。
顯然,這樣是最容易想到的,但是也不由的有點(diǎn)擔(dān)心效率,最終我也通過這種方式,實(shí)現(xiàn)了一個(gè),
正如擔(dān)心的一樣,每秒6幀的速度偶爾會(huì)出現(xiàn)閃爍(當(dāng)然我的本本可能不是很給力- -。)。
這種方式就是通過載入的位圖,獲取每個(gè)像素點(diǎn),并生成相應(yīng)的HRGN,每幀動(dòng)畫一個(gè)。
然后通過計(jì)時(shí)器切換,由于效果不是很好,在此就不貼代碼了。
既然不能通過SetWindowRGN,那還能怎么做呢?其實(shí)windows自身已經(jīng)給了我們一個(gè)
處理這種位圖窗口的API,而且還可以實(shí)現(xiàn)透明,一說到透明很多人都應(yīng)該知道是那個(gè)API了。
Syntax

BOOL SetLayeredWindowAttributes( HWND hwnd,
COLORREF crKey,
BYTE bAlpha,
DWORD dwFlags
);
Parameters

hwnd
[in] Handle to the layered window. A layered window is created by specifying WS_EX_LAYERED when creating the window with the CreateWindowEx function or by setting WS_EX_LAYERED via SetWindowLong after the window has been created.
crKey
[in] COLORREF structure that specifies the transparency color key to be used when composing the layered window. All pixels painted by the window in this color will be transparent. To generate a COLORREF, use the RGB macro.
bAlpha
[in] Alpha value used to describe the opacity of the layered window. Similar to the SourceConstantAlpha member of the BLENDFUNCTION structure. When bAlpha is 0, the window is completely transparent. When bAlpha is 255, the window is opaque.
dwFlags
[in] Specifies an action to take. This parameter can be one or more of the following values.
LWA_COLORKEY
Use crKey as the transparency color.
LWA_ALPHA
Use bAlpha to determine the opacity of the layered window.
Return Value

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.



其實(shí)這個(gè)函數(shù)在以前也經(jīng)常用到,但主要是用它來實(shí)現(xiàn)透明,根本沒想到它還可以實(shí)現(xiàn)不規(guī)則窗體。
看了MSDN你應(yīng)該知道了吧,其實(shí)通過設(shè)置第二個(gè)顏色屬性便可過濾掉窗體中指定的某種顏色,達(dá)到
不規(guī)則窗體的效果。
設(shè)置透明色的代碼如下:再此將白色設(shè)置為透明。
HINSTANCE hInstance=(HINSTANCE)LoadLibrary("user32.dll");
if (hInstance)

{
typedef BOOL(WINAPI *pFun)(HWND,COLORREF,BYTE,DWORD);
pFun fun = NULL;
fun=(pFun)GetProcAddress(hInstance, "SetLayeredWindowAttributes");
SetWindowLong(GWL_EXSTYLE, GetWindowLong(GWL_EXSTYLE) | 0x80000);
if (fun)

{
fun(hWnd, RGB(255, 255, 255), 0, 0x1);
}
FreeLibrary(hInstance);
}
知道如何處理不規(guī)則窗體后剩下的就簡(jiǎn)單了,在此我用的一半游戲中的(4*4)行走圖做的。
只需要在繪制的時(shí)候通過改變繪制區(qū)域坐標(biāo)來控制顯示的位置達(dá)到動(dòng)畫的效果。
其中m_nX,m_nY分別是橫向幀數(shù) 和 豎向的幀數(shù) 在此為 4 * 4

void OnPaint(CDCHandle /**//*dc*/)

{
CPaintDC dc(*this);
CDC bmpDC;
CBitmapHandle bmpOld;
CRect rtClient;

GetClientRect(&rtClient);
bmpDC.CreateCompatibleDC(dc);
bmpOld = bmpDC.SelectBitmap(m_bmpBox);

int nSubX = m_nWidth / m_nX;
int nSubY = m_nHeight / m_nY;

dc.BitBlt(0, 0, nSubX, nSubY, bmpDC, m_nCurX * nSubX,
m_nCurY * nSubY, SRCCOPY);

bmpDC.SelectBitmap(bmpOld);
}
剩下就是一些輔助的功能了:m_nCurX m_nCurY 分別只是當(dāng)前橫向第幾幀 和 縱向的第幾幀

/**//**
* 實(shí)現(xiàn)移動(dòng)窗口
*/
UINT OnNcHitTest(CPoint point)

{
return HTCAPTION;
}


/**//**
* 更新動(dòng)畫
*/
void OnTimer(UINT_PTR nIDEvent)

{
if (nIDEvent == ANIMA_TIMER)

{
m_nCurX = (++m_nCurX) % m_nX;
RedrawWindow();
}
}


/**//**
* 改變方向
*/
void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)

{
switch (nChar)

{
case VK_LEFT: m_nCurY = 1; break;
case VK_RIGHT: m_nCurY = 2; break;
case VK_UP: m_nCurY = 3; break;
case VK_DOWN: m_nCurY = 0; break;
case VK_ESCAPE:OnClose();break;
}
}
好了,結(jié)束,雖然沒啥技術(shù)含量,但也可以打發(fā)下無(wú)聊的時(shí)間。
以后有好玩的東西我也會(huì)更新在此。