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

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



其實這個函數在以前也經常用到,但主要是用它來實現透明,根本沒想到它還可以實現不規則窗體。
看了MSDN你應該知道了吧,其實通過設置第二個顏色屬性便可過濾掉窗體中指定的某種顏色,達到
不規則窗體的效果。
設置透明色的代碼如下:再此將白色設置為透明。
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);
}
知道如何處理不規則窗體后剩下的就簡單了,在此我用的一半游戲中的(4*4)行走圖做的。
只需要在繪制的時候通過改變繪制區域坐標來控制顯示的位置達到動畫的效果。
其中m_nX,m_nY分別是橫向幀數 和 豎向的幀數 在此為 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 分別只是當前橫向第幾幀 和 縱向的第幾幀

/**//**
* 實現移動窗口
*/
UINT OnNcHitTest(CPoint point)

{
return HTCAPTION;
}


/**//**
* 更新動畫
*/
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;
}
}
好了,結束,雖然沒啥技術含量,但也可以打發下無聊的時間。
以后有好玩的東西我也會更新在此。