●オーディオプレイヤ開発記 その16
今回は、再生時間表示用ラベルを管理する CTimeLabel_MainWnd クラスを作成します。
まずは、CWnd クラスに GetWidth, GetClientWidth 関数を追加しておきます。
virtual int GetClientWidth() const {
RECT rc;
GetClientRect(m_hWnd, &rc);
return rc.right - rc.left;
}
virtual int GetWidth() const {
RECT rc;
GetWindowRect(m_hWnd, &rc);
return rc.right - rc.left;
}
それでは、CStatic クラスを継承した CTimeLabel_MainWnd クラスを作ります。
//----------------------------------------------------------------------------
// TimeLabel_MainWnd.h : 再生時間表示用ラベルの管理を行う
//----------------------------------------------------------------------------
#ifndef TimeLabel_MainWndH
#define TimeLabel_MainWndH
class CApp;
class CMainWnd;
#include "../Common/Static.h"
#include "../Common/Font.h"
//----------------------------------------------------------------------------
// 再生時間表示用ラベルの管理を行うクラス
//----------------------------------------------------------------------------
class CTimeLabel_MainWnd : public CStatic
{
public: // 関数
CTimeLabel_MainWnd(CApp & app, CMainWnd & mainWnd)
: m_rApp(app), m_rMainWnd(mainWnd) { }
virtual ~CTimeLabel_MainWnd() { }
virtual BOOL Create();
virtual void ResetPos();
virtual void SetTime(int time, int totalTime);
private: // メンバ変数
CApp & m_rApp;
CMainWnd & m_rMainWnd;
CFont m_font;
};
//----------------------------------------------------------------------------
#endif
//----------------------------------------------------------------------------
// TimeLabel_MainWnd.cpp : 再生時間表示用ラベルの管理を行うクラス
//----------------------------------------------------------------------------
#include <windows.h>
#include "../App.h"
#include "MainWnd.h"
#include "TimeLabel_MainWnd.h"
//----------------------------------------------------------------------------
// 作成
//----------------------------------------------------------------------------
BOOL CTimeLabel_MainWnd::Create()
{
CStatic::Create();
if(!m_hWnd) return FALSE;
SetParent(m_rMainWnd);
// 画面のプロパティで設定されているタイトルバーと同じシステムフォントに設定
NONCLIENTMETRICS ncm;
ZeroMemory(&ncm, sizeof(NONCLIENTMETRICS));
ncm.cbSize = sizeof(NONCLIENTMETRICS);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0);
m_font.CreateIndirect(&ncm.lfCaptionFont);
SetFont(m_font, TRUE);
SetTime(0, 0);
Show(SW_SHOW);
return TRUE;
}
//----------------------------------------------------------------------------
// 位置の再設定
//----------------------------------------------------------------------------
void CTimeLabel_MainWnd::ResetPos()
{
// ウィンドウの右端に設定
SetPos(m_rMainWnd.GetClientWidth() - GetWidth(), 2);
}
//----------------------------------------------------------------------------
// 表示する時間を秒単位で設定
//----------------------------------------------------------------------------
void CTimeLabel_MainWnd::SetTime(int time, int totalTime)
{
int hour = (time / 3600) % 60;
int second = (time / 60) % 60;
int minute = time % 60;
int totalHour = (totalTime / 3600) % 60;
int totalSecond = (totalTime / 60) % 60;
int totalMinute = totalTime % 60;
TCHAR text[20];
if(totalHour > 0)
wsprintf(text, "%02d:%02d:%02d / %02d:%02d:%02d", hour, second, minute, totalHour, totalSecond, totalMinute);
else
wsprintf(text, "%02d:%02d / %02d:%02d", second, minute, totalSecond, totalMinute);
SetText(text);
SetSizeToTextSize();
ResetPos();
}
//----------------------------------------------------------------------------
この TimeLabel_MainWnd.h を MainWnd.h からインクルードしておき、CMainWnd クラスのメンバ変数に CTimeLabel_MainWnd m_timeLabel; を持つようにし、コンストラクタで初期化を行い、CMainWnd::Create で再生時間表示用ラベルを作成するようにします。
CMainWnd(CApp & app): m_rApp(app), m_menu(app, *this), m_toolBar(app, *this),
m_timeLabel(app, *this) { }
// 再生時間表示用ラベルの作成
if(!m_timeLabel.Create())
{
MessageBox(m_hWnd, TEXT("再生時間表示用ラベルの作成に失敗しました。"
"アプリケーションを終了します。"), TEXT("エラー"), MB_ICONERROR);
Destroy();
return FALSE;
}
これで、再生時間表示用ラベルが表示されるようになりました。ついでに、CMainWnd::OnSize 関数に、m_timeLabel.ResetPos(); を追加し、ウィンドウのサイズが変更された際にラベルの位置を再設定するようにしておきます。
ラベルに表示される文字のフォントについては、とりあえず、画面のプロパティで設定されているタイトルバーと同じシステムフォントに設定してますが、いずれ、右クリックメニューなどからカスタマイズできるようにしたいです。ここまでのソースコードはこちら ( Hayaemon20060219_src.lzh ) 。