C++音樂播放按鈕的封裝過程詳解
1、準(zhǔn)備工作:音樂、開發(fā)工具VS stdio及圖形庫工具
2、設(shè)計思路:先加載音樂,再通過點擊不同的按鈕執(zhí)行不同的操作(播放音樂,暫停音樂、繼續(xù)播放、結(jié)束播放)
繪制按鈕我們通過一個按鈕button類來操作,這樣數(shù)據(jù)會存在一些必要的訪問數(shù)據(jù)權(quán)限,并可以將多個函數(shù)聲明寫在同一個類中,調(diào)用只需使用 " 類名.函數(shù)名 “即可調(diào)用里面的函數(shù)
按鈕類頭文件:-----button.h
#include "graphics.h" #include <iostream> #include <string> using namespace std; class Button { public: void Show(); void InitButton(int xx, int yy, int ww, int hh, COLORREF color, string text); bool InButton(ExMessage message); bool OnClickButton(ExMessage message); private: int x; int y; int w; int h; COLORREF curColor; COLORREF oldColor; string str; };
寫類中函數(shù)的定義(即寫函數(shù)的函數(shù)體) ----button.cpp
注意:在類外寫類內(nèi)部函數(shù)的定義時,需要加類名限定
1、初始化按鈕的一些參數(shù):如按鈕的長寬高、顏色和按鈕上的文字內(nèi)容:
void Button::InitButton(int xx, int yy, int ww, int hh, COLORREF color, string text) { x = xx; y = yy; w = ww; h = hh; curColor = color; oldColor = color; str = text; }
2、繪制矩形按鈕:
void Button::Show() { //矩形框 setfillcolor(curColor); solidrectangle(x, y, x + w, y + h); //文字 settextstyle(15, 0, "FZZJ-XHFTJW.TTF"); //1、求文字所在矩形的寬高 int textw = textwidth(str.c_str()); int texth = textheight(str.c_str()); //2、求h1 w1 //(w - textw) / 2 <=> w1 //(h - texth) / 2 <=> h1 //3、求出文字所在矩形左上角的坐標(biāo) int xx = x+(w - textw) / 2; int yy = y+(h - texth) / 2; setbkmode(TRANSPARENT); settextcolor(BLACK); outtextxy(xx, yy, str.c_str()); }
注意:這里有一個文字在矩形框中居中顯示的:
如何將文字在矩形框中居中顯示?
如圖:要使文字在矩形中居中顯示: h1=h2 ; w1=w2
步驟:
1、求出文字的 高(textwidth(文字)) 與 寬(text(文字)) 返回的是一個整數(shù)
2、求出h1、w1的值
3、外矩形的寬高分別加上w1,h1就是需要繪制里面文字所在的矩形框的左上角的坐標(biāo)。繪制一個矩形只需直到矩形左上角的坐標(biāo)和矩形的寬高即可繪制繪制一個矩形。
4、判斷鼠標(biāo)是否在按鈕中---在矩形中矩形顯示一種顏色,不在顯示另外一種顏色
bool Button::InButton(ExMessage message) { if (message.x >= x && message.x <= x + w && message.y >= y && message.y <= y + h) { curColor = RGB(236, 244, 255); return true; } curColor = oldColor; return false; }
5、判斷鼠標(biāo)是否點擊矩形框
bool Button::OnClickButton(ExMessage m) { if (InButton(m) && m.message == WM_LBUTTONDOWN) { return true; } return false; }
主函數(shù)---main
加載音樂,繪制按鈕,按鈕消息的制作,顯示界面等
#include "button.h" #include <mmsystem.h> #pragma comment(lib,"winmm.lib") int main() { initgraph(800, 600); IMAGE mm; loadimage(&mm, "mm.jpg",800,600); Button* play = new Button; play->InitButton(5, 5, 100, 25, RGB(204, 213, 240), "播放(play)"); Button* pause = new Button; pause->InitButton(110, 5, 100, 25, RGB(204, 213, 240), "暫停(pause)"); Button* resume = new Button; resume->InitButton(215, 5, 100, 25, RGB(204, 213, 240), "繼續(xù)(resume)"); Button* stop = new Button; stop->InitButton(320, 5, 100, 25, RGB(204, 213, 240), "停止(stop)"); ExMessage m; BeginBatchDraw(); while (1) { putimage(0, 0, &mm); peekmessage(&m); play->Show(); if (play->OnClickButton(m)) { mciSendString("open 1.mp3", 0, 0, 0); mciSendString("play 1.mp3", 0, 0, 0); } pause->Show(); if (pause->OnClickButton(m)) { mciSendString("pause 1.mp3", 0, 0, 0); } resume->Show(); if (resume->OnClickButton(m)) { mciSendString("resume 1.mp3", 0, 0, 0); } stop->Show(); if (stop->OnClickButton(m)) { mciSendString("close 1.mp3", 0, 0, 0); } FlushBatchDraw(); } EndBatchDraw(); closegraph(); return 0; }
到此這篇關(guān)于C++音樂播放按鈕的封裝過程詳解的文章就介紹到這了,更多相關(guān)C++封裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++中new與delete、malloc與free應(yīng)用分析
這篇文章主要介紹了C++中new與delete、malloc與free應(yīng)用分析,很重要的概念,需要的朋友可以參考下2014-08-08用位圖排序無重復(fù)數(shù)據(jù)集實例代碼(C++版)
本文講解如何用位圖排序無重復(fù)的數(shù)據(jù)集,我們使用C++實現(xiàn)一下這個方法2013-11-11直觀理解C語言中指向一位數(shù)組與二維數(shù)組的指針
這篇文章主要介紹了直觀理解C語言中指向一位數(shù)組與二維數(shù)組的指針,數(shù)組指針是C語言入門學(xué)習(xí)過程中的重點和難點,需要的朋友可以參考下2016-05-05基于QT制作一個TCPServer與TCPClient的通信
這篇文章主要為大家詳細(xì)介紹了如何基于QT制作一個TCPServer與TCPClient的通信,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12