TNB Library
TnbMfcResizeDialog.h
[詳解]
1#pragma once
13#include "TnbMfcCommon.h"
14
15
16
18#define classCResizeDialog classCDialog
19
20
21
22//TNB Library
23namespace TNB {
24namespace MFC {
25
26
27
96class CResizeDialog : public CDialog
97{
98 DEFSUPER(CDialog);
99 enum { DEFAULT_LINESTEP = 20 };
100public:
101
111 class CPaintOrgDC : public CPaintDC
112 {
113 DEFSUPER(CPaintDC);
114 CResizeDialog* m_pResizeDlg;
115 public:
120 explicit CPaintOrgDC(CResizeDialog* pWnd) : _super(pWnd), m_pResizeDlg(pWnd)
121 {
122 m_pResizeDlg->SetScrollOrg(this, true);
123 }
126 {
127 m_pResizeDlg->SetScrollOrg(this, false);
128 }
129 };
130
133 : _super(), m_originalSize(0, 0), m_lineStep(DEFAULT_LINESTEP)
134 , m_onThemeChanged(false), m_canSizeDisp(true)
135 {
136 }
137
145 CResizeDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL)
146 : _super(lpszTemplateName, pParentWnd), m_originalSize(0, 0), m_lineStep(DEFAULT_LINESTEP)
147 , m_onThemeChanged(false), m_canSizeDisp(true)
148 {
149 }
150
158 CResizeDialog(UINT nIDTemplate, CWnd* pParentWnd = NULL)
159 : _super(nIDTemplate, pParentWnd), m_originalSize(0, 0), m_lineStep(DEFAULT_LINESTEP)
160 , m_onThemeChanged(false), m_canSizeDisp(true)
161 {
162 }
163
168 CPoint GetScrollPoint(void) const
169 {
170 return CPoint(m_horizontalParam.GetPos(), m_verticalParam.GetPos());
171 }
172
180 void SetScrollOrg(CDC* pDc, bool isBegin = true) const
181 {
182 int cx = m_horizontalParam.GetPos();
183 int cy = m_verticalParam.GetPos();
184 if ( isBegin )
185 {
186 pDc->OffsetWindowOrg(cx, cy);
187 }
188 else
189 {
190 pDc->OffsetWindowOrg(-cx, -cy);
191 }
192 }
193
201 CSize GetOriginalWindowSize(void) const
202 {
203 return GetOriginalSize() + m_offsetWinToClient;
204 }
205
213 const CSize& GetOriginalSize(void) const
214 {
215 return m_originalSize;
216 }
217
224 void SetOriginalWindowSize(const SIZE& size)
225 {
227 UINT r = SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE;
228 m_canSizeDisp = false;
229 _super::SetWindowPos(NULL, 0, 0, size.cx, size.cy, r);
230 m_canSizeDisp = true;
231 CRect rc;
232 GetClientRect(&rc);
233 m_originalSize = rc.Size();
234 m_InitScrollInfo();
235 }
236
243 void SetOriginalSize(const SIZE& size)
244 {
246 m_originalSize = size;
247 CSize s = size;
248 s += m_offsetWinToClient;
249 UINT r = SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE;
250 m_canSizeDisp = false;
251 _super::SetWindowPos(NULL, 0, 0, s.cx, s.cy, r);
252 m_canSizeDisp = true;
253 m_InitScrollInfo();
254 }
255
261 void ChangeOriginalSize(const SIZE& size)
262 {
263 int dx = 0;
264 int dy = 0;
265 if ( m_horizontalParam.GetMax() < size.cx )
266 {
267 dx -= size.cx - m_horizontalParam.GetMax();
268 }
269 if ( m_verticalParam.GetMax() < size.cy )
270 {
271 dy -= size.cy - m_verticalParam.GetMax();
272 }
273 m_OnHScroll(SB_LEFT, 0, NULL);
274 m_OnVScroll(SB_TOP, 0, NULL);
275 m_horizontalParam.ShowBar(*this, SB_HORZ, false);
276 m_verticalParam.ShowBar(*this, SB_VERT, false);
277 //
279 m_originalSize = size;
280 CSize s = size;
281 s += m_offsetWinToClient;
282 UINT r = SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE;
283 _super::SetWindowPos(NULL, 0, 0, s.cx, s.cy, r);
284 m_InitScrollInfo();
285 //
286 _super::ScrollWindow(dx, 0);
287 _super::ScrollWindow(0, dy);
288 }
289
296 void SetLineStep(int s)
297 {
298 m_lineStep = s;
299 }
300
306 {
307 m_OnHScroll(SB_LEFT, 0, NULL);
308 m_OnVScroll(SB_TOP, 0, NULL);
309 SIZE sz;
311 SetWindowPos(NULL, 0, 0, sz.cx, sz.cy, SWP_NOMOVE | SWP_NOZORDER);
312 }
313
321 virtual void OnPrintClient(CDC* pDC)
322 {
323 }
324
325protected:
327 void m_CheckOffset(void)
328 {
329 if ( m_originalSize.cx == 0 )
330 {
331 CRect cliRc;
332 _super::GetClientRect(&cliRc);
333 CRect winRc;
334 _super::GetWindowRect(&winRc);
335 m_originalSize = cliRc.Size();
336 m_offsetWinToClient = winRc.Size() - cliRc.Size();
337 }
338 }
339
349 virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
350 {
351 LRESULT r = 0;
352 switch ( message )
353 {
354 case WM_NCLBUTTONDBLCLK:
355 TTRACE0("WM_NCLBUTTONDBLCLK\n");
356 if ( wParam == HTCAPTION )
357 {
358 CSize s = GetOriginalWindowSize();
359 _super::SetWindowPos(NULL, 0, 0, s.cx, s.cy, SWP_NOMOVE | SWP_NOZORDER);
360 return 0;
361 }
362 break;
363 case WM_SIZE:
364 TTRACE0("WM_SIZE\n");
365 r = _super::WindowProc(message, wParam, lParam);
366 if ( m_canSizeDisp )
367 {
368 m_OnSize(wParam, LOWORD(lParam), HIWORD(lParam));
369 }
370 return r;
371 case WM_GETMINMAXINFO:
372 TTRACE0("WM_GETMINMAXINFO\n");
374 if ( m_originalSize.cx != 0 )
375 {
376 LPMINMAXINFO P = reinterpret_cast<LPMINMAXINFO>(lParam);
377 P->ptMaxTrackSize.x = m_originalSize.cx + m_offsetWinToClient.cx;
378 P->ptMaxTrackSize.y = m_originalSize.cy + m_offsetWinToClient.cy;
379 }
380 break;
381 case WM_HSCROLL:
382 TTRACE2("WM_HSCROLL %d 0x%08X\n", LOWORD(wParam), lParam);
383 m_OnHScroll(LOWORD(wParam), HIWORD(wParam), reinterpret_cast<HWND>(lParam));
384 break;
385 case WM_VSCROLL:
386 TTRACE2("WM_VSCROLL %d 0x%08X\n", LOWORD(wParam), lParam);
387 m_OnVScroll(LOWORD(wParam), HIWORD(wParam), reinterpret_cast<HWND>(lParam));
388 break;
389 case WM_MOUSEWHEEL:
390 TTRACE0("WM_MOUSEWHEEL\n");
391 if ( m_OnMouseWheel(LOWORD(wParam), static_cast<short>(HIWORD(wParam)), lParam) )
392 {
393 return TRUE;
394 }
395 break;
396 case WM_PRINT:
397 TTRACE1("WM_PRINT 0x%08X\n", lParam);
398 r = _super::WindowProc(message, wParam, lParam & ~(PRF_OWNED | PRF_CHILDREN));
399 if ( (lParam & PRF_CHILDREN) != 0 )
400 {
401 CRect rectWin;
402 _super::GetWindowRect(rectWin);
403 CDC* pDC = CDC::FromHandle(reinterpret_cast<HDC>(wParam));
404 CPoint orgOrg = pDC->GetWindowOrg();
405 CWnd* pWnd = CWnd::GetWindow(GW_CHILD);
406 while ( pWnd != NULL )
407 {
408 CRect rect;
409 pWnd->GetWindowRect(rect);
410 CSize po = rect.TopLeft() - rectWin.TopLeft();
411 pDC->SetWindowOrg(orgOrg - po);
412 pWnd->SendMessage(WM_PRINT, wParam, lParam | PRF_CHECKVISIBLE);
413 pWnd = pWnd->GetNextWindow();
414 }
415 pDC->SetWindowOrg(orgOrg);
416 if ( (lParam & PRF_NONCLIENT) != 0 )
417 {
418 SendMessage(message, wParam, PRF_NONCLIENT);
419 }
420 }
421 return r;
422 case WM_PRINTCLIENT:
423 TTRACE0("WM_PRINTCLIENT\n");
424 {
425 CDC* pDC = CDC::FromHandle(reinterpret_cast<HDC>(wParam));
426 SetScrollOrg(pDC);
427 OnPrintClient(pDC);
428 SetScrollOrg(pDC, false);
429 }
430 break;
431 case WM_INITDIALOG:
432 TTRACE0("WM_INITDIALOG\n");
433 r = _super::WindowProc(message, wParam, lParam);
434 m_InitScrollInfo();
435 return r;
436 case WM_THEMECHANGED:
437 TTRACE0("WM_THEMECHANGED\n");
438 m_onThemeChanged = true;
439 break;
440 case WM_EXITSIZEMOVE:
441 TTRACE0("WM_EXITSIZEMOVE\n");
442 m_onThemeChanged = false;
443 break;
444 case WM_WINDOWPOSCHANGED:
445 TTRACE0("WM_WINDOWPOSCHANGED\n");
446 {
447 LRESULT r = _super::WindowProc(message, wParam, lParam);
448 WINDOWPOS* W = reinterpret_cast<WINDOWPOS*>(lParam);
449 if ( m_onThemeChanged && (W->flags & SWP_FRAMECHANGED) != 0 )
450 {
451 CRect cliRc;
452 _super::GetClientRect(&cliRc);
453 CRect winRc;
454 _super::GetWindowRect(&winRc);
455 CSize sz = m_offsetWinToClient;
456 m_offsetWinToClient = winRc.Size() - cliRc.Size();
457 if ( m_horizontalParam.IsEnable() )
458 {
459 m_offsetWinToClient.cx -= ::GetSystemMetrics(SM_CYHSCROLL);
460 }
461 if ( m_verticalParam.IsEnable() )
462 {
463 m_offsetWinToClient.cy -= ::GetSystemMetrics(SM_CXHSCROLL);
464 }
465 m_OnSize(0, cliRc.right, cliRc.bottom);
466 }
467 return r;
468 }
469 break;
470 }
471 return _super::WindowProc(message, wParam, lParam);
472 }
473
474private:
475
477 class CBarParam
478 {
479 int m_pos;
480 int m_max;
481 int m_page;
482 bool m_boIsEnable;
483 public:
485 CBarParam(void) : m_boIsEnable(false), m_pos(0), m_max(0), m_page(0) { }
487 int GetPos(void) const { return m_boIsEnable ? m_pos : 0; }
489 int GetMax(void) const { return m_max; }
491 bool IsEnable(void) const { return m_boIsEnable; }
493 void SetMax(int max)
494 {
495 m_pos = 0;
496 m_max = max;
497 m_page = max;
498 }
500 int SetPage(int pa)
501 {
502 m_page = pa;
503 int delta = 0;
504 if ( m_pos + m_page > m_max )
505 {
506 delta = m_pos - (m_max - m_page);
507 m_pos = m_max - m_page;
508 }
509 return delta;
510 }
512 void ShowBar(HWND h, int nBar, bool isEnable)
513 {
514 m_boIsEnable = isEnable;
515 if ( m_boIsEnable )
516 {
517 SCROLLINFO si = { sizeof(SCROLLINFO), SIF_ALL, 0, m_max - 1, m_page, m_pos, 0 };
518 ASSERTLIB( si.cbSize == sizeof(SCROLLINFO) && si.nPos == m_pos );
519 ::SetScrollInfo(h, nBar, &si, TRUE);
520 ::ShowScrollBar(h, nBar, TRUE);
521 }
522 else
523 {
524 ::ShowScrollBar(h, nBar, FALSE);
525// ::InvalidateRect(h, NULL, false);
526 }
527 }
529 int Scroll(UINT nSBCode, UINT nPos, int lineStep) const
530 {
531 int delta = 0;
532 switch ( nSBCode )
533 {
534 case SB_LEFT: // SB_TOP
535 delta = -m_pos;
536 break;
537 case SB_LINERIGHT: // SB_LINEDOWN
538 case SB_PAGERIGHT: // SB_PAGEDOWN
539 if ( m_pos + m_page < m_max )
540 {
541 int r = (nSBCode == SB_LINERIGHT) ? lineStep : m_page;
542 delta = min(r, m_max - (m_pos + m_page));
543 }
544 break;
545 case SB_LINELEFT: // SB_LINEUP
546 case SB_PAGELEFT: // SB_PAGEUP
547 if ( m_pos > 0 )
548 {
549 int r = (nSBCode == SB_LINELEFT) ? lineStep : m_page;
550 delta = -min(r, m_pos);
551 }
552 break;
553 case SB_THUMBTRACK:
554 case SB_THUMBPOSITION:
555 delta = static_cast<int>(nPos) - m_pos;
556 break;
557 default:
558 break;
559 }
560 if ( m_pos + delta < 0 )
561 {
562 delta = -m_pos;
563 }
564 if ( m_pos + m_page + delta > m_max )
565 {
566 delta = m_max - (m_pos + m_page);
567 }
568 return delta;
569 }
571 int AddPos(int delta)
572 {
573 m_pos += delta;
574 return m_pos;
575 }
576 };
577 CSize m_originalSize;
578 CSize m_offsetWinToClient;
579 CBarParam m_horizontalParam;
580 CBarParam m_verticalParam;
581 int m_lineStep;
582 bool m_onThemeChanged;
583 bool m_canSizeDisp;
584
586 void m_InitScrollInfo(void)
587 {
588 _super::ShowScrollBar(SB_HORZ, FALSE);
589 _super::ShowScrollBar(SB_VERT, FALSE);
590 CRect rect;
591 _super::GetClientRect(rect);
592 m_horizontalParam.SetMax(rect.Width());
593 m_verticalParam.SetMax(rect.Height());
594 }
601 void m_OnSize(WPARAM nType, int cx, int cy)
602 {
603 if ( m_originalSize.cx != 0 )
604 {
605 CRect rect;
606 _super::GetWindowRect(rect);
607 bool isEnable = !! (m_originalSize + m_offsetWinToClient != rect.Size());
608 if ( isEnable )
609 {
610 if ( (rect.Height() - m_offsetWinToClient.cy) <= ::GetSystemMetrics(SM_CYHSCROLL) )
611 {
612 isEnable = false;
613 }
614 }
615 _super::ScrollWindow(m_horizontalParam.SetPage(cx), m_verticalParam.SetPage(cy));
616 m_horizontalParam.ShowBar(*this, SB_HORZ, isEnable);
617 m_verticalParam.ShowBar(*this, SB_VERT, isEnable);
618 }
619 }
626 void m_OnHScroll(UINT nSBCode, UINT nPos, HWND hScrollBar)
627 {
628 if ( hScrollBar == NULL )
629 {
630 int delta = m_horizontalParam.Scroll(nSBCode, nPos, m_lineStep);
631 if ( delta != 0 )
632 {
633 _super::SetScrollPos(SB_HORZ, m_horizontalParam.AddPos(delta), TRUE);
634 _super::ScrollWindow(-delta, 0);
635 }
636 }
637 }
644 void m_OnVScroll(UINT nSBCode, UINT nPos, HWND hScrollBar)
645 {
646 if ( hScrollBar == NULL )
647 {
648 int delta = m_verticalParam.Scroll(nSBCode, nPos, m_lineStep);
649 if ( delta != 0 )
650 {
651 _super::SetScrollPos(SB_VERT, m_verticalParam.AddPos(delta), TRUE);
652 _super::ScrollWindow(0, -delta);
653 }
654 }
655 }
664 bool m_OnMouseWheel(UINT fFlags, short delta, LPARAM lParam)
665 {
666 if ( ! m_verticalParam.IsEnable() ) { return false; }
667 int d = 0;
668 if ( delta < 0)
669 {
670 d = m_verticalParam.Scroll(SB_LINEDOWN, 0, (-delta) / 10 + 1);
671 }
672 else if ( delta > 0 )
673 {
674 d = m_verticalParam.Scroll(SB_LINEUP, 0, (delta) / 10 + 1);
675 }
676 if ( d != 0 )
677 {
678 _super::SetScrollPos(SB_VERT, m_verticalParam.AddPos(d), TRUE);
679 _super::ScrollWindow(0, -d);
680 }
681 return true;
682 }
683};
684
685
686
687}; //MFC
688}; //TNB
689
690
691
MFCコントロール共通のヘッダ
ウィンドウ管理.
リサイズダイアログ用 PaintDC クラス
CPaintOrgDC(CResizeDialog *pWnd)
コンストラクタ
リサイズダイアログベースクラス
CSize GetOriginalWindowSize(void) const
[取得] オリジナルウィンドウサイズ取得.
CResizeDialog(UINT nIDTemplate, CWnd *pParentWnd=NULL)
コンストラクタ
void SetScrollOrg(CDC *pDc, bool isBegin=true) const
[設定] デバイスコンテキストの原点設定、解除.
const CSize & GetOriginalSize(void) const
[取得] オリジナルクライアントサイズ取得.
CResizeDialog(void)
コンストラクタ
void m_CheckOffset(void)
WinとClientの差を計算
void SetLineStep(int s)
[設定] ラインスクロール量設定.
void SetOriginalWindowSize(const SIZE &size)
[設定] オリジナルウィンドウサイズ設定.
void SetOriginalSize(const SIZE &size)
[設定] オリジナルクライアントサイズ設定.
void ChangeOriginalSize(const SIZE &size)
[設定] オリジナルクライアントサイズ変更.
virtual void OnPrintClient(CDC *pDC)
[通知] WM_PRINTCLIENT メッセージ受信.
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
[通知] for processing Windows messages.
void ResetWindowsSize(void)
[設定] オリジナルウィンドウサイズ化.
CResizeDialog(LPCTSTR lpszTemplateName, CWnd *pParentWnd=NULL)
コンストラクタ
CPoint GetScrollPoint(void) const
[取得] スクロール位置.
TNB Library
Definition: TnbDoxyTitle.txt:2