●オーディオプレイヤ開発記 その18
今回は、前回作成した CSliderCtrl クラスを継承して、再生時間の設定用に CTimeSlider_MainWnd クラスを作ります。スライダの縦位置をツールバー、再生時間表示用ラベルより下にする必要があるので、CTimeSlider_MainWnd クラスからツールバー、再生時間表示用ラベルの高さを得られるようにします。そのために、CMainWnd クラスに GetToolBar, GetTimeLabel 関数を追加しておきます。
const CToolBar_MainWnd & GetToolBar() const { return m_toolBar; }
const CTimeLabel_MainWnd & GetTimeLabel() const { return m_timeLabel; }
それでは、CTimeSlider_MainWnd クラスを作成します。
//----------------------------------------------------------------------------
// TimeSlider_MainWnd.h : 再生時間設定用スライダの管理を行う
//----------------------------------------------------------------------------
#ifndef TimeSlider_MainWndH
#define TimeSlider_MainWndH
class CApp;
class CMainWnd;
#include "../Common/SliderCtrl.h"
//----------------------------------------------------------------------------
// 再生時間設定用スライダの管理を行うクラス
//----------------------------------------------------------------------------
class CTimeSlider_MainWnd : public CSliderCtrl
{
public: // 関数
CTimeSlider_MainWnd(CApp & app, CMainWnd & mainWnd)
: m_rApp(app), m_rMainWnd(mainWnd) { }
virtual ~CTimeSlider_MainWnd() { }
virtual BOOL Create();
virtual void ResetSize();
virtual void SetTime(LONG time, LONG totalTime);
private: // メンバ変数
CApp & m_rApp;
CMainWnd & m_rMainWnd;
};
//----------------------------------------------------------------------------
#endif
//----------------------------------------------------------------------------
// TimeSlider_MainWnd.cpp : 再生時間表示用ラベルの管理を行う
//----------------------------------------------------------------------------
#include <windows.h>
#include "../App.h"
#include "MainWnd.h"
#include "TimeSlider_MainWnd.h"
//----------------------------------------------------------------------------
// 作成
//----------------------------------------------------------------------------
BOOL CTimeSlider_MainWnd::Create()
{
CSliderCtrl::Create(m_rMainWnd);
if(!m_hWnd) return FALSE;
SetStyle(GetStyle() | WS_TABSTOP | TBS_AUTOTICKS | TBS_HORZ | TBS_NOTICKS);
int nToolBarHeight = m_rMainWnd.GetToolBar().GetHeight();
int nTimeLabelHeight = m_rMainWnd.GetTimeLabel().GetHeight();
int nBigger = nToolBarHeight > nTimeLabelHeight ? nToolBarHeight : nTimeLabelHeight;
SetPos(0, nBigger);
ResetSize();
Show(SW_SHOW);
return TRUE;
}
//----------------------------------------------------------------------------
// サイズの再設定
//----------------------------------------------------------------------------
void CTimeSlider_MainWnd::ResetSize()
{
SetSize(m_rMainWnd.GetClientWidth(), GetSystemMetrics(SM_CYHSCROLL) * 1.5);
}
//----------------------------------------------------------------------------
// スライダをバイト単位で設定
//----------------------------------------------------------------------------
void CTimeSlider_MainWnd::SetTime(LONG bytes, LONG totalBytes)
{
SetRange(0, totalBytes);
SetThumbPos(bytes);
}
//----------------------------------------------------------------------------
とりあえずスライダの縦のサイズは、画面のプロパティで設定するスクロールバーの高さ×1.5倍にしておきましたけど、これってそんなアバウトでいいんでしょうか。どのぐらいにするのが普通なんでしょう。
それでは、この TimeSlider_MainWnd.h を MainWnd.h からインクルードしておき、CMainWnd クラスのメンバ変数に CTimeSlider_MainWnd m_timeSlider; を持つようにし、コンストラクタで初期化を行い、CMainWnd::Create で再生時間表示用ラベルを作成するようにします。
CMainWnd(CApp & app): m_rApp(app), m_menu(app, *this), m_toolBar(app, *this),
m_timeLabel(app, *this), m_timeSlider(app, *this) { }
// 再生時間設定用スライダの作成
if(!m_timeSlider.Create())
{
MessageBox(m_hWnd, TEXT("再生時間設定用スライダの作成に失敗しました。"
"アプリケーションを終了します。"), TEXT("エラー"), MB_ICONERROR);
Destroy();
return FALSE;
}
これで、再生時間設定用スライダが表示されるようになりました。ついでに、CMainWnd::OnSize 関数に、m_timeSlider.ResetSize(); を追加し、ウィンドウのサイズが変更された際にスライダのサイズを再設定するようにしておきます。ここまでのソースコードはこちら ( Hayaemon20060223_src.lzh ) 。