#include <windows.h>
#include "resource.h"
#pragma comment(lib, "winmm")
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM ,LPARAM );
int MP3Player(HWND,int fnNum);
HINSTANCE Hinstance;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
WNDCLASS wndclass;
HWND hwnd;
MSG msg;
WCHAR *title=L"快樂の?、贐制作";
WCHAR *MP3=L"MP3 Player";
Hinstance=hInstance;
wndclass.cbClsExtra=NULL;
wndclass.cbWndExtra=NULL;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=MP3;
wndclass.lpszMenuName=0;
wndclass.style=CS_HREDRAW |CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,L"注冊窗口失敗!",L"Error Info",MB_OK);
return 0;
}
hwnd=CreateWindowEx(WS_EX_LAYERED,MP3,title,WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU| WS_MINIMIZEBOX
,CW_USEDEFAULT,CW_USEDEFAULT,310,410,NULL,NULL,hInstance,NULL);
AnimateWindow(hwnd,1000,AW_BLEND|AW_SLIDE);//窗口效果函數(shù)
//UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc,hdcmem;
HBITMAP hBitmap;
BITMAP Bitmap;
static HWND ChildHwnd_1=0;
static HWND ChildHwnd_2=0;
if(message == WM_PRINT || message == WM_PRINTCLIENT)
{
hdc=(HDC)wParam;
hBitmap=LoadBitmap(Hinstance,MAKEINTRESOURCE(IDB_BITMAP1));
GetObject(hBitmap,sizeof(BITMAP),&Bitmap);
hdcmem=CreateCompatibleDC(hdc);
SelectObject(hdcmem,hBitmap);
BitBlt(hdc,0,0,Bitmap.bmWidth,Bitmap.bmHeight,hdcmem,0,0,SRCCOPY);
DeleteDC(hdcmem);
}
switch(message)
{
case WM_CREATE:
ChildHwnd_1=CreateWindowEx(WS_EX_LAYERED,L"Button",L"Play",WS_VISIBLE|WS_CHILD,50,330,70,30,hwnd,(HMENU)1,0,NULL);
ChildHwnd_2=CreateWindowEx(WS_EX_LAYERED,L"Button",L"Stop",WS_VISIBLE|WS_CHILD,190,330,70,30,hwnd,(HMENU)2,0,NULL);
return 0;
case WM_CLOSE:
DestroyWindow(hwnd);
PostQuitMessage(0);
return 0;
case WM_COMMAND:
switch(wParam)
{
case 1:
MP3Player(hwnd,1);
break;
case 2:
MP3Player(hwnd,2);
break;
}
return 0;
case WM_PAINT:
hBitmap=LoadBitmap(Hinstance,MAKEINTRESOURCE(IDB_BITMAP1));
GetObject(hBitmap,sizeof(BITMAP),&Bitmap);
hdc=GetDC(hwnd);
hdcmem=CreateCompatibleDC(hdc);
SelectObject(hdcmem,hBitmap);
BitBlt(hdc,0,0,Bitmap.bmWidth,Bitmap.bmHeight,hdcmem,0,0,SRCCOPY);
DeleteDC(hdcmem);
BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0 ;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
int MP3Player(HWND hwnd,int fnNum)
{
switch(fnNum)
{
case 1://播放
static MCI_OPEN_PARMS open;
static MCI_PLAY_PARMS play;
static MCIDEVICEID MP3ID;
open.dwCallback=0;
open.lpstrAlias=NULL;
open.lpstrDeviceType=L"MPEG"; //MP3設備
open.lpstrElementName=L"該死的溫柔.mp3"; //要播放的文件
open.wDeviceID=0;
mciSendCommand(0,MCI_OPEN,MCI_WAIT| MCI_OPEN_ELEMENT,(DWORD)(LPMCI_OPEN_PARMS)&open);
//初始化
MP3ID=open.wDeviceID;
play.dwCallback=(DWORD)hwnd; //播放程序的窗口句柄
play.dwFrom=0;
play.dwTo=0;
mciSendCommand(MP3ID,MCI_PLAY,MCI_NOTIFY,(DWORD)(LPMCI_OPEN_PARMS)&play);
return 0;
case 2://停止
MCI_GENERIC_PARMS close;
close.dwCallback=(DWORD)hwnd;
mciSendCommand(MP3ID,MCI_CLOSE,MCI_NOTIFY,(DWORD) (LPMCI_GENERIC_PARMS)&close);
return 0;
}
return 0;
}