Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.17 KB | None | 0 0
  1. // ヘッダのインクルード
  2. // 独自のヘッダ
  3. #include "WindowListItemsPanel.h" // ウィンドウリストアイテムズパネルクラス
  4.  
  5. // ウィンドウクラス登録関数RegisterClass
  6. BOOL CWindowListItemsPanel::RegisterClass(HINSTANCE hInstance) {
  7.  
  8. // ウィンドウリストアイテムを登録.
  9. CWindowListItem::RegisterClass(hInstance); // 子ウィンドウを登録.
  10.  
  11. // ユーザコントロールとして登録.
  12. return CUserControl::RegisterClass(hInstance, _T("WindowListItemsPanel")); // CUserControl::RegisterClassでウィンドウクラス"WindowListItemsPanel"を登録.
  13.  
  14. }
  15.  
  16. // コンストラクタCWindowListItemsPanel()
  17. CWindowListItemsPanel::CWindowListItemsPanel() : CUserControl() {
  18.  
  19. // メンバの初期化.
  20. m_nId = 0; // m_nIdを0に初期化.
  21.  
  22. }
  23.  
  24. // デストラクタ~CWindowListItemsPanel()
  25. CWindowListItemsPanel::~CWindowListItemsPanel() {
  26.  
  27. // メンバの終了処理.
  28. Destroy(); // Destroyで破棄.
  29. m_nId = 0; // m_nIdを0にセット.
  30.  
  31. }
  32.  
  33. // ウィンドウ作成関数Create
  34. BOOL CWindowListItemsPanel::Create(LPCTSTR lpctszWindowName, DWORD dwStyle, int x, int y, int iWidth, int iHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance) {
  35.  
  36. // ユーザコントロールで作成.
  37. return CUserControl::Create(_T("WindowListItemsPanel"), lpctszWindowName, dwStyle, x, y, iWidth, iHeight, hWndParent, hMenu, hInstance); // CUserControl::Createでウィンドウクラス"WindowListItemsPanel"なウィンドウを作成.
  38.  
  39. }
  40.  
  41. // ウィンドウ破棄関数Destroy
  42. void CWindowListItemsPanel::Destroy() {
  43.  
  44. // アイテムの一斉削除.
  45. std::list<CWindowListItem *>::iterator itor = m_lstWindowList.begin(); // イテレータ.
  46. while (itor != m_lstWindowList.end()) {
  47. (*itor)->Destroy();
  48. delete (*itor);
  49. (*itor) = NULL;
  50. itor++;
  51. }
  52. m_lstWindowList.clear();
  53.  
  54. // ペンとブラシの破棄.
  55. if (m_hBrush) {
  56. DeleteObject(m_hBrush); // ブラシの破棄.
  57. m_hBrush = NULL;
  58. }
  59. if (m_hPen) {
  60. DeleteObject(m_hPen); // ペンの破棄.
  61. m_hPen = NULL;
  62. }
  63. // 自分のウィンドウも破棄.
  64. CWindow::Destroy(); // CWindow::Destroyで自身のウィンドウも破棄.
  65.  
  66. }
  67.  
  68. // アイテム挿入関数Insert
  69. BOOL CWindowListItemsPanel::Insert(LPCTSTR lpctszWindowName, int iIndex, int iHeight, HINSTANCE hInstance) {
  70.  
  71. // 変数の宣言.
  72. int iIdx; // 値のチェック後のインデックスint型iIdx.
  73. CWindowListItem *pItem; // ウィンドウリストアイテムポインタpItem.
  74. int iTotalHeight = 0; // パネル全体の高さiTotalHeightを0に初期化.
  75.  
  76. // インデックスの値で振り分け.
  77. if (iIndex < 0) { // 負の値
  78. iIdx = 0; // 0をセット.
  79. }
  80. else if (iIndex >(int)m_lstWindowList.size()) { // サイズより大きい.
  81. iIdx = m_lstWindowList.size(); // サイズをセット.
  82. }
  83. else {
  84. iIdx = iIndex; // iIndexをセット.
  85. }
  86. std::list<CWindowListItem *>::iterator itor = m_lstWindowList.begin(); // イテレータ.
  87. int y = 0;
  88. for (int i = 0; i < (int)m_lstWindowList.size() + 1; i++) {
  89. if (i == iIdx) {
  90. pItem = new CWindowListItem(); // 生成.
  91. pItem->Create(lpctszWindowName, 0, PADDING, iTotalHeight + PADDING, m_iWidth - (PADDING * 2), iHeight - (PADDING * 2), m_hWnd, (HMENU)(IDC_WINDOWLISTITEM_ID_START + m_nId), hInstance); // ウィンドウ生成.
  92. iTotalHeight = iTotalHeight + iHeight; // 挿入するウィンドウの高さを足す.
  93. m_lstWindowList.insert(itor, pItem); // 挿入.
  94. m_nId++;
  95. itor = m_lstWindowList.begin();
  96. for (int j = 0; j < iIdx; j++) {
  97. itor++;
  98. }
  99. }
  100. else if (i < iIdx) {
  101. y = y + (*itor)->m_iHeight; // 位置計算.
  102. y = y + (PADDING * 2); // 余白の大きさ
  103. iTotalHeight = y;
  104. }
  105. else if (i > iIdx) {
  106. if (itor != m_lstWindowList.end()) {
  107. iTotalHeight = iTotalHeight + (*itor)->m_iHeight;
  108. iTotalHeight = iTotalHeight + (PADDING * 2);
  109. (*itor)->MoveWindow(false, 0, iHeight); // iHeight分ずらす.
  110. }
  111. else {
  112. break;
  113. }
  114. }
  115. if (itor != m_lstWindowList.end()) {
  116. itor++;
  117. }
  118. else {
  119. break;
  120. }
  121. }
  122.  
  123. //アイテムズパネルの大きさを拡張する.
  124. MoveWindow(3, iTotalHeight); // 高さをiTotalHeightにする.
  125.  
  126. // とりあえず成功にしておく.
  127. return TRUE;
  128.  
  129. }
  130.  
  131. // アイテム削除Remove
  132. BOOL CWindowListItemsPanel::Remove(int iIndex) {
  133.  
  134. // 変数の宣言.
  135. int iIdx; // 値のチェック後のインデックスint型iIdx.
  136. int iHeight = 0; // 削除されたアイテムの高さiHeightを0に初期化.
  137. int iTotalHeight = 0; // パネル全体の高さiTotalHeightを0に初期化.
  138.  
  139. // インデックスの値で振り分け.
  140. if (iIndex < 0) { // 負の値
  141. iIdx = 0; // 0をセット.
  142. }
  143. else if (iIndex >(int)m_lstWindowList.size() - 1) { // 最後尾より大きい.
  144. iIdx = m_lstWindowList.size() - 1; // 最後尾をセット.
  145. }
  146. else {
  147. iIdx = iIndex; // iIndexをセット.
  148. }
  149. std::list<CWindowListItem *>::iterator itor = m_lstWindowList.begin(); // イテレータ.
  150. for (int i = 0; i < (int)m_lstWindowList.size(); i++) {
  151. if (i == iIdx) {
  152. iHeight = (*itor)->m_iHeight; // 高さを保存.
  153. (*itor)->Destroy();
  154. delete (*itor);
  155. m_lstWindowList.remove((*itor));
  156. itor = m_lstWindowList.begin();
  157. for (int j = 0; j < iIdx; j++) {
  158. itor++;
  159. }
  160. if (itor != m_lstWindowList.end()) {
  161. iTotalHeight = iTotalHeight + (*itor)->m_iHeight;
  162. (*itor)->MoveWindow(false, 0, -iHeight); // iHeight分ずらす.
  163. }
  164. else {
  165. break;
  166. }
  167. }
  168. else if (i > iIdx) {
  169. if (itor != m_lstWindowList.end()) {
  170. iTotalHeight = iTotalHeight + (*itor)->m_iHeight;
  171. (*itor)->MoveWindow(false, 0, -iHeight); // iHeight分ずらす.
  172. }
  173. else {
  174. break;
  175. }
  176. }
  177. else {
  178. iTotalHeight = iTotalHeight + (*itor)->m_iHeight;
  179. }
  180. if (itor != m_lstWindowList.end()) {
  181. itor++;
  182. }
  183. else {
  184. break;
  185. }
  186. }
  187.  
  188. //アイテムズパネルの大きさを縮小する.
  189. MoveWindow(3, iTotalHeight); // 高さをiTotalHeightにする.
  190.  
  191. // とりあえず成功にしておく.
  192. return TRUE;
  193.  
  194. }
  195.  
  196. // アイテム取得関数Get
  197. CWindowListItem * CWindowListItemsPanel::Get(int iIndex) {
  198.  
  199. // iIndex番目の要素を返す.
  200. int i = 0; // iを0に初期化.
  201. std::list<CWindowListItem *>::iterator itor = m_lstWindowList.begin(); // イテレータ.
  202. while (itor != m_lstWindowList.end()) {
  203. if (i == iIndex) {
  204. return (*itor);
  205. }
  206. i++;
  207. itor++;
  208. }
  209. return NULL;
  210.  
  211. }
  212.  
  213. // ウィンドウ作成時のハンドラOnCreate.
  214. int CWindowListItemsPanel::OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct) {
  215.  
  216. // ペンとブラシの作成.
  217. m_hPen = (HPEN)CreatePen(PS_SOLID, 1, RGB(0, 0, 0xff)); // 青(濃)のペンを作成.
  218. m_hBrush = (HBRUSH)CreateSolidBrush(RGB(0x9d, 0xcc, 0xe0)); // 青(淡)のブラシを作成.
  219.  
  220. // 成功なので0を返す.
  221. return 0;
  222.  
  223. }
  224.  
  225. // ウィンドウサイズが変更された時のハンドラOnSize.
  226. void CWindowListItemsPanel::OnSize(UINT nType, int cx, int cy) {
  227.  
  228. // 実際のウィンドウサイズを格納.
  229. m_iWidth = cx; // m_iWidthにcxをセット.
  230. m_iHeight = cy; // m_iHeightにcyをセット.
  231.  
  232. // アイテムの一斉リサイズ.
  233. std::list<CWindowListItem *>::iterator itor = m_lstWindowList.begin(); // イテレータ.
  234. while (itor != m_lstWindowList.end()) {
  235. (*itor)->MoveWindow(2, m_iWidth - (PADDING * 2)); // 親ウィンドウの幅に合わせる.
  236. itor++;
  237. }
  238.  
  239. }
  240.  
  241. // ウィンドウの描画を要求された時のハンドラOnPaint.
  242. void CWindowListItemsPanel::OnPaint() {
  243.  
  244. // 変数の宣言
  245. HDC hDC; // デバイスコンテキストハンドルHDC型hDC.
  246. PAINTSTRUCT ps; // PAINTSTRUCT構造体変数ps.
  247.  
  248. // 描画開始.
  249. hDC = BeginPaint(m_hWnd, &ps); // BeginPaintで描画開始.
  250. // ペンとブラシの選択.
  251. HPEN hOldPen = (HPEN)SelectObject(hDC, m_hPen); // 青のペンを選択.
  252. HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC, m_hBrush); // 青のブラシを選択.
  253. // 矩形を描画.(クライアント領域はスクロールバーを含まない.)
  254. RECT rc;
  255. GetClientRect(m_hWnd, &rc); // GetClientRectでクライアント領域の矩形を取得.
  256. Rectangle(hDC, 0, 0, rc.right - rc.left, rc.bottom - rc.top); // Rectangleで矩形を描画.
  257. // ペンとブラシの復元
  258. SelectObject(hDC, hOldBrush); // 古いブラシを選択.
  259. SelectObject(hDC, hOldPen); // 古いペンを選択.
  260. // 描画終了.
  261. EndPaint(m_hWnd, &ps); // EndPaintで描画終了.
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement