【新手】【高手勿喷】简易的音乐盒

来源:互联网 发布:点胶机编程 编辑:程序博客网 时间:2024/04/30 16:59
   
#include<Windows.h>#include<tchar.h>#include<winuser.h>#pragma comment(lib,"winmm.lib")int n = 0;HDC g_hdc = NULL;HPEN pen;HBRUSH rush;HBRUSH rush2;wchar_t song1[] = L"1.贝多芬病毒";wchar_t song2[] = L"2.let it go";wchar_t song3[] = L"3.the fox";wchar_t song4[] = L"4.卷珠帘";wchar_t a[] =L"up or down";wchar_t song5[];wchar_t song6[];LRESULT CALLBACK wndproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);BOOL init(HWND hwnd);VOID paint(HWND hwnd);BOOL clean(HWND hwnd);#pragma warning(suppress: 28251)int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){WNDCLASSEX wndclass = { 0 };wndclass.cbSize = sizeof(WNDCLASSEX);wndclass.style = CS_HREDRAW | CS_VREDRAW;wndclass.lpfnWndProc = wndproc;wndclass.cbClsExtra = 0;wndclass.cbWndExtra = 0;wndclass.hIcon = (HICON)::LoadImage(NULL,_T("1.ico"), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);wndclass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);wndclass.lpszMenuName = NULL;wndclass.lpszClassName = _T("musicbox");RegisterClassEx(&wndclass);HWND hwnd = CreateWindow(_T("musicbox"), _T("musicbox"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 400, NULL, NULL, hinstance, NULL);MoveWindow(hwnd, 200, 50, 500, 400, true);ShowWindow(hwnd, nCmdShow);UpdateWindow(hwnd);init(hwnd);UpdateWindow(hwnd);MSG msg = { 0 };while (msg.message!=WM_QUIT){if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)){TranslateMessage(&msg);DispatchMessage(&msg);}}UnregisterClass(L"musicbox", wndclass.hInstance);return 0;}LRESULT CALLBACK wndproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){PAINTSTRUCT paintstruct; switch (message){case WM_PAINT:g_hdc = BeginPaint(hwnd, &paintstruct);paint(hwnd);EndPaint(hwnd, NULL);ValidateRect(hwnd, NULL);break;case WM_KEYDOWN:SetBkMode(g_hdc, TRANSPARENT);SetTextColor(g_hdc, RGB(225,225,225));switch (wParam){case VK_UP:n = n - 1;break;case VK_ESCAPE:DestroyWindow(hwnd);break;case VK_DOWN:n = n + 1;break;default:break;} ;    switch (n){case 1:PlaySound(L"1wav.wav", NULL, SND_FILENAME | SND_ASYNC);paint(hwnd);TextOut(g_hdc, 2, 2, song1, wcslen(song1));break;case 2:PlaySound(L"2.wav", NULL, SND_FILENAME | SND_ASYNC);paint(hwnd);TextOut(g_hdc, 2,2, song2, wcslen(song2));break;case 3:PlaySound(L"3.wav", NULL, SND_FILENAME | SND_ASYNC);paint(hwnd);TextOut(g_hdc, 2, 2, song3, wcslen(song3));break;case 4:PlaySound(L"4.wav", NULL, SND_FILENAME | SND_ASYNC);paint(hwnd);TextOut(g_hdc, 2, 2, song4, wcslen(song4));default:break;}break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hwnd, message, wParam, lParam);}return 0;}BOOL init(HWND hwnd){g_hdc = GetDC(hwnd);    pen=CreatePen(PS_DASH, 1, RGB(225, 0,0 ));    rush=CreateSolidBrush(RGB(0, 0, 225));rush2 = CreateSolidBrush(RGB(0, 225, 0));ReleaseDC(hwnd, g_hdc);return TRUE;}VOID paint(HWND hwnd){SetBkMode(g_hdc, TRANSPARENT);SetTextColor(g_hdc, RGB(225, 225, 225));SelectObject(g_hdc, pen);SelectObject(g_hdc, rush);Rectangle(g_hdc, 250, 1, 500, 399);SelectObject(g_hdc, rush2);Rectangle(g_hdc, 1, 1, 250, 399);TextOut(g_hdc, 256, 1, song1, wcslen(song1));TextOut(g_hdc, 256, 30, song2, wcslen(song2));TextOut(g_hdc, 256, 60, song3, wcslen(song3));TextOut(g_hdc, 256, 90, song4, wcslen(song4));TextOut(g_hdc, 2, 200, a, wcslen(a));}BOOL clean(HWND hwnd){DeleteObject(rush);DeleteObject(pen);DeleteObject(rush2);return TRUE;}

因技术不高,故画面有些粗糙,望大家谅解。

0 0