C語言百行代碼繪制圣誕水晶球
序
我愛你,不是因?yàn)槟闶且粋€(gè)怎樣的人,而是因?yàn)槲蚁矚g與你在一起時(shí)的感覺。
嗨!這里是狐貍~~
今天就是圣誕節(jié)了,再過一個(gè)星期就是2022年了,最近總是感覺傷感,有些事情就是比想象中來的快一些,希望大家都可以把握2021年最后的時(shí)間,不留遺憾吧,后天圣誕節(jié),今天再教大家一個(gè)圣誕項(xiàng)目吧,圣誕水晶球,今天這個(gè)呢代碼不多,但難度會(huì)有點(diǎn),因?yàn)檫@個(gè)涉及桌面,就是可以在桌面實(shí)現(xiàn),希望大家可以認(rèn)真看,認(rèn)真學(xué)吧。
同樣,先給大家看效果吧

效果還是很不錯(cuò)的,再加上一個(gè)音樂,女朋友看完就馬上同意你了,嘿嘿,這里就不再是簡(jiǎn)單的窗口了,難度有一些,但也不大,畢竟代碼也不多,好了,認(rèn)真學(xué),認(rèn)真的嘗試哦!
項(xiàng)目代碼
這是我第一次只用一個(gè)代碼段,因?yàn)榇a確實(shí)不多,我覺得分段就沒必要了,而且其中必要的地方,我也加了詳細(xì)的注釋,應(yīng)該還好,難就難在之前沒有用Windows庫(kù),這次用了,效果看起來就會(huì)舒服很多,怎么樣,學(xué)會(huì)了趕快拿給心中的那個(gè)人看吧!
#include <stdio.h>
#include <windows.h>
#include <graphics.h>
#include <commctrl.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
int main()
{
keybd_event(VK_LWIN, 0, 0, 0);
keybd_event('D', 0, 0, 0);
keybd_event('D', 0, 2, 0);
keybd_event(VK_LWIN, 0, 2, 0);
Sleep(3000);
int CX = GetSystemMetrics(SM_CXSCREEN);
int CY = GetSystemMetrics(SM_CYSCREEN);
HWND hwnd = initgraph(500,500); //創(chuàng)建一個(gè)窗口hwnd
HRGN Ellip = CreateEllipticRgn(10, 32, 100+10, 100+32);//設(shè)置一個(gè)邏輯的圖形rect
SetWindowRgn(hwnd, Ellip, true); //設(shè)置窗口的區(qū)域
SetForegroundWindow(hwnd);
MoveWindow(hwnd, CX / 2 - 120, CY / 2, 500, 500, true);
mciSendString("open res/music.mp3", 0, 0, 0); //打開
mciSendString("play res/music.mp3 repeat", 0, 0, 0); //播放
bool isplay = true; //音樂是否正在播放
int sence = 3; //場(chǎng)景
int frame = 0; //幀數(shù)
IMAGE img[30];
int maxframe[] = { 20, 7, 4, 15, 10}; //每一種場(chǎng)景的圖片的張數(shù)
char picpath[30] = { 0 }; //字符串 圖片的路徑
for (int i = 0; i < maxframe[sence]; i++)
{
sprintf(picpath, "res/%02d-%02d.bmp", sence, i);
loadimage(&img[i], picpath, 100, 100);
}
/*---------------------------------------------------------*/
//獲取到桌面圖標(biāo)的窗口
HWND zmWnd = FindWindow("Progman","Program Manager"); //桌面窗口
HWND bzWnd = FindWindowEx(zmWnd, 0, "SHELLDLL_DefView", NULL); //壁紙窗口
HWND tbWnd = FindWindowEx(bzWnd, 0, "SysListView32", "FolderView"); //圖標(biāo)窗口
HWND workHwnd = NULL;
while (tbWnd == NULL)//必須存在桌面窗口層次
{
workHwnd = FindWindowExA(0, workHwnd, "WorkerW", NULL);//獲得WorkerW類的窗口
if (workHwnd == NULL)
break;//未知錯(cuò)誤
bzWnd = FindWindowExA(workHwnd, NULL, "SHELLDLL_DefView", NULL);
if (bzWnd == NULL)
continue;
tbWnd = FindWindowExA(bzWnd, NULL, "SysListView32", NULL);
}
DWORD dwStyle = (DWORD)GetWindowLong(tbWnd, GWL_STYLE);
if (dwStyle & LVS_AUTOARRANGE)
SetWindowLong(tbWnd, GWL_STYLE, dwStyle & ~LVS_AUTOARRANGE);
DWORD dwExStyle = (DWORD)ListView_GetExtendedListViewStyle(tbWnd, GWL_EXSTYLE);
if (dwExStyle & LVS_EX_SNAPTOGRID)
ListView_SetExtendedListViewStyle(tbWnd, dwExStyle & ~LVS_EX_SNAPTOGRID);
//指揮它 威脅它 圖標(biāo)的窗口
int count = SendMessage(tbWnd, LVM_GETITEMCOUNT, 0, 0);
for (int i = 0; i < count; i++)
SendMessage(tbWnd, LVM_SETITEMPOSITION, i, (3000 << 16) + 100);
POINT dir[14] = { { 868 * CX / 1920, 316 * CY / 1080 }, { 730 * CX / 1920, 207 * CY / 1080 },
{ 591 * CX / 1920, 221 * CY / 1080 }, { 515 * CX / 1920, 327 * CY / 1080 }, { 542 * CX / 1920, 469 * CY / 1080 },
{ 610 * CX / 1920, 624 * CY / 1080 }, { 723 * CX / 1920, 746 * CY / 1080 }, { 870 * CX / 1920, 814 * CY / 1080 },
{ 1012 * CX / 1920, 744 * CY / 1080 }, { 1130 * CX / 1920, 626 * CY / 1080 },
{ 1219 * CX / 1920, 485 * CY / 1080 }, { 1225 * CX / 1920, 328 * CY / 1080 }, { 1156 * CX / 1920, 225 * CY / 1080 },
{ 1012 * CX / 1920, 217 * CY / 1080 } };
ExMessage m; //定義消息
int oldsence;
while (1){
if (peekmessage(&m, EM_MOUSE)) //如果獲取到一條鼠標(biāo)消息
{
switch (m.message)
{
case WM_LBUTTONDOWN: //點(diǎn)下左鍵 換一個(gè)場(chǎng)景
oldsence = sence;
do
{
sence = rand() % (sizeof(maxframe) / sizeof(maxframe[0])); //0-5
} while (sence == oldsence);
for (int i = 0; i < maxframe[sence]; i++)
{
sprintf(picpath, "res/%02d-%02d.bmp", sence, i);
loadimage(&img[i], picpath, 100, 100);
}
SendMessage(hwnd, WM_SYSCOMMAND, 0xf012, 0);
break;
case WM_RBUTTONDOWN:
if (isplay) //正在播放
mciSendString("pause res/music.mp3", 0, 0, 0);
else
mciSendString("play res/music.mp3 repeat", 0, 0, 0);
isplay = !isplay; //取反
break;
}
}
else //繪制界面
{
putimage(0, 0, &img[frame++]);
if (frame > maxframe[sence] - 1)frame = 0;
for (int i = 0; i < 14; i++)
{
SendMessage(tbWnd, LVM_SETITEMPOSITION, i, (dir[i].y << 16) + dir[i].x);
}
int x = dir[0].x;
int y = dir[0].y;
for (int i = 0; i < 13; i++)
{
dir[i].x = dir[i + 1].x;
dir[i].y = dir[i + 1].y;
}
dir[13].x = x;
dir[13].y = y;
}
}
return 0;
}
總結(jié)
代碼真的非常的少,認(rèn)真理解每一行的作用,相信你們很快就可以理解了。圣誕節(jié)快到了,程序員的快樂無非就是可以代碼寫出一些特別的東西了,主要是確實(shí)好用,哈哈哈,不管男女,好啦,希望大家可以在圣誕節(jié)的那天都會(huì)有好運(yùn)氣,提前祝大家圣誕節(jié)快樂啦!
視頻講解
這次也有視頻講解,代碼不多,看視頻會(huì)理解的更快!?
到此這篇關(guān)于C語言百行代碼繪制圣誕水晶球的文章就介紹到這了,更多相關(guān)C語言繪制圣誕水晶球內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言實(shí)現(xiàn)對(duì)bmp格式圖片打碼
這篇文章主要介紹了C語言實(shí)現(xiàn)對(duì)bmp格式圖片打碼2016-01-01
基于MFC實(shí)現(xiàn)單個(gè)文檔的文件讀寫
這篇文章主要為大家詳細(xì)介紹了如何基于MFC實(shí)現(xiàn)單個(gè)文檔的文件讀寫功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定幫助,感興趣的可以了解一下2022-07-07
FFRPC應(yīng)用 Client/Server使用及原理解析
這篇文章主要介紹了FFRPC應(yīng)用 Client/Server使用及原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
C++實(shí)現(xiàn)校園運(yùn)動(dòng)會(huì)報(bào)名系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)校園運(yùn)動(dòng)會(huì)報(bào)名系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10

