4 Star 0 Fork 2

leegoobin/AutoPHS

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
xdialog.cpp 16.02 KB
一键复制 编辑 原始数据 按行查看 历史
uksoft 提交于 2014-11-26 16:31 . VSS代码移植到Git
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
// XDialog.cpp : implementation file
//
//ļ: XDialog.Cpp
//:
//ʱ: 2002.8
#include "stdafx.h"
#include "XDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define MM_NONE 0
#define MM_SCALETOFIT (-1)
#define WM_RECALCPARENT 0x0368 // force RecalcLayout on frame window
/////////////////////////////////////////////////////////////////////////////
// CXDialog dialog
const SIZE CXDialog::sizeDefault={0,0};
CXDialog::CXDialog(UINT nIDTemplate,CWnd* pParent /*=NULL*/)
: CDialog(nIDTemplate, pParent)
{
//{{AFX_DATA_INIT(CXDialog)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_bCenter=FALSE; // Center output if larger than total size
m_bInsideUpdate=FALSE;
m_bAccelToParentFrm=FALSE;
m_bEnableScroll=TRUE;
}
void CXDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CXDialog)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CXDialog, CDialog)
//{{AFX_MSG_MAP(CXDialog)
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_VSCROLL()
ON_WM_HSCROLL()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CXDialog message handlers
int CXDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
CRect rectTemplate;
GetClientRect(rectTemplate);
SetScrollSizes(MM_TEXT, rectTemplate.Size());
// TODO: Add your specialized creation code here
return 0;
}
void CXDialog::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if(!m_bEnableScroll)
return;
if (m_nMapMode == MM_SCALETOFIT)
{
// force recalculation of scale to fit parameters
SetScaleToFitSize(m_totalLog);
}
else
{
// UpdateBars() handles locking out recursion
UpdateBars();
}
}
BOOL CXDialog::GetTrueClientSize(CSize& size, CSize& sizeSb)
// return TRUE if enough room to add scrollbars if needed
{
CRect rect;
GetClientRect(&rect);
ASSERT(rect.top == 0 && rect.left == 0);
size.cx = rect.right;
size.cy = rect.bottom;
DWORD dwStyle = GetStyle();
// first get the size of the scrollbars for this window
GetScrollBarSizes(sizeSb);
// first calculate the size of a potential scrollbar
// (scroll bar controls do not get turned on/off)
if (sizeSb.cx != 0 && (dwStyle & WS_VSCROLL))
{
// vert scrollbars will impact client area of this window
size.cx += sizeSb.cx; // currently on - adjust now
}
if (sizeSb.cy != 0 && (dwStyle & WS_HSCROLL))
{
// horz scrollbars will impact client area of this window
size.cy += sizeSb.cy; // currently on - adjust now
}
// return TRUE if enough room
return (size.cx > sizeSb.cx && size.cy > sizeSb.cy);
}
void CXDialog::GetScrollBarState(CSize sizeClient, CSize& needSb,
CSize& sizeRange, CPoint& ptMove, BOOL bInsideClient)
{
// get scroll bar sizes (the part that is in the client area)
CSize sizeSb;
GetScrollBarSizes(sizeSb);
// enough room to add scrollbars
sizeRange = m_totalDev - sizeClient;
// > 0 => need to scroll
ptMove = GetDeviceScrollPosition();
// point to move to (start at current scroll pos)
BOOL bNeedH = sizeRange.cx > 0;
if (!bNeedH)
ptMove.x = 0; // jump back to origin
else if (bInsideClient)
sizeRange.cy += sizeSb.cy; // need room for a scroll bar
BOOL bNeedV = sizeRange.cy > 0;
if (!bNeedV)
ptMove.y = 0; // jump back to origin
else if (bInsideClient)
sizeRange.cx += sizeSb.cx; // need room for a scroll bar
if (bNeedV && !bNeedH && sizeRange.cx > 0)
{
ASSERT(bInsideClient);
// need a horizontal scrollbar after all
bNeedH = TRUE;
sizeRange.cy += sizeSb.cy;
}
// if current scroll position will be past the limit, scroll to limit
if (sizeRange.cx > 0 && ptMove.x >= sizeRange.cx)
ptMove.x = sizeRange.cx;
if (sizeRange.cy > 0 && ptMove.y >= sizeRange.cy)
ptMove.y = sizeRange.cy;
// now update the bars as appropriate
needSb.cx = bNeedH;
needSb.cy = bNeedV;
// needSb, sizeRange, and ptMove area now all updated
}
void CXDialog::GetScrollBarSizes(CSize& sizeSb)
{
sizeSb.cx = sizeSb.cy = 0;
DWORD dwStyle = GetStyle();
if (GetScrollBarCtrl(SB_VERT) == NULL)
{
// vert scrollbars will impact client area of this window
sizeSb.cx = ::GetSystemMetrics(SM_CXVSCROLL);
if (dwStyle & WS_BORDER)
sizeSb.cx -= ::GetSystemMetrics(SM_CXBORDER);
}
if (GetScrollBarCtrl(SB_HORZ) == NULL)
{
// horz scrollbars will impact client area of this window
sizeSb.cy = ::GetSystemMetrics(SM_CYHSCROLL);
if (dwStyle & WS_BORDER)
sizeSb.cy -= ::GetSystemMetrics(SM_CYBORDER);
}
}
void CXDialog::GetDeviceScrollSizes(int& nMapMode, SIZE& sizeTotal,
SIZE& sizePage, SIZE& sizeLine) const
{
if (m_nMapMode <= 0)
TRACE0("Warning: CXDialog::GetDeviceScrollSizes returning invalid mapping mode.\n");
nMapMode = m_nMapMode;
sizeTotal = m_totalDev;
sizePage = m_pageDev;
sizeLine = m_lineDev;
}
CPoint CXDialog::GetDeviceScrollPosition() const
{
CPoint pt(GetScrollPos(SB_HORZ), GetScrollPos(SB_VERT));
ASSERT(pt.x >= 0 && pt.y >= 0);
if (m_bCenter)
{
CRect rect;
GetClientRect(&rect);
// if client area is larger than total device size,
// the scroll positions are overridden to place origin such that
// output is centered in the window
// GetDeviceScrollPosition() must reflect this
if (m_totalDev.cx < rect.Width())
pt.x = -((rect.Width() - m_totalDev.cx) / 2);
if (m_totalDev.cy < rect.Height())
pt.y = -((rect.Height() - m_totalDev.cy) / 2);
}
return pt;
}
void CXDialog::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
if (pScrollBar != NULL && pScrollBar->SendChildNotifyLastMsg())
return; // eat it
// ignore scroll bar msgs from other controls
if (pScrollBar != GetScrollBarCtrl(SB_VERT))
return;
OnScroll(MAKEWORD(-1, nSBCode), nPos);
//CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}
void CXDialog::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
if (pScrollBar != NULL && pScrollBar->SendChildNotifyLastMsg())
return; // eat it
// ignore scroll bar msgs from other controls
if (pScrollBar != GetScrollBarCtrl(SB_HORZ))
return;
OnScroll(MAKEWORD(nSBCode, -1), nPos);
//CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
BOOL CXDialog::OnScroll(UINT nScrollCode, UINT nPos, BOOL bDoScroll)
{
int x = GetScrollPos(SB_HORZ);
int xOrig = x;
switch (LOBYTE(nScrollCode))
{
case SB_LEFT:
x = 0;
break;
case SB_RIGHT:
x = INT_MAX;
break;
case SB_LINELEFT:
x -= m_lineDev.cx;
break;
case SB_LINERIGHT:
x += m_lineDev.cx;
break;
case SB_PAGELEFT:
x -= m_pageDev.cx;
break;
case SB_PAGERIGHT:
x += m_pageDev.cx;
break;
case SB_THUMBTRACK:
x = nPos;
break;
}
// calc new y position
int y = GetScrollPos(SB_VERT);
int yOrig = y;
switch (HIBYTE(nScrollCode))
{
case SB_TOP:
y = 0;
break;
case SB_BOTTOM:
y = INT_MAX;
break;
case SB_LINEUP:
y -= m_lineDev.cy;
break;
case SB_LINEDOWN:
y += m_lineDev.cy;
break;
case SB_PAGEUP:
y -= m_pageDev.cy;
break;
case SB_PAGEDOWN:
y += m_pageDev.cy;
break;
case SB_THUMBTRACK:
y = nPos;
break;
}
BOOL bResult = OnScrollBy(CSize(x - xOrig, y - yOrig), bDoScroll);
if (bResult && bDoScroll)
UpdateWindow();
return bResult;
}
BOOL CXDialog::OnScrollBy(CSize sizeScroll, BOOL bDoScroll)
{
int xOrig, x;
int yOrig, y;
// don't scroll if there is no valid scroll range (ie. no scroll bar)
CScrollBar* pBar;
DWORD dwStyle = GetStyle();
pBar = GetScrollBarCtrl(SB_VERT);
if ((pBar != NULL && !pBar->IsWindowEnabled()) ||
(pBar == NULL && !(dwStyle & WS_VSCROLL)))
{
// vertical scroll bar not enabled
sizeScroll.cy = 0;
}
pBar = GetScrollBarCtrl(SB_HORZ);
if ((pBar != NULL && !pBar->IsWindowEnabled()) ||
(pBar == NULL && !(dwStyle & WS_HSCROLL)))
{
// horizontal scroll bar not enabled
sizeScroll.cx = 0;
}
// adjust current x position
xOrig = x = GetScrollPos(SB_HORZ);
int xMax = GetScrollLimit(SB_HORZ);
x += sizeScroll.cx;
if (x < 0)
x = 0;
else if (x > xMax)
x = xMax;
// adjust current y position
yOrig = y = GetScrollPos(SB_VERT);
int yMax = GetScrollLimit(SB_VERT);
y += sizeScroll.cy;
if (y < 0)
y = 0;
else if (y > yMax)
y = yMax;
// did anything change?
if (x == xOrig && y == yOrig)
return FALSE;
if (bDoScroll)
{
// do scroll and update scroll positions
ScrollWindow(-(x-xOrig), -(y-yOrig));
if (x != xOrig)
SetScrollPos(SB_HORZ, x);
if (y != yOrig)
SetScrollPos(SB_VERT, y);
}
return TRUE;
}
void CXDialog::UpdateBars()
{
// UpdateBars may cause window to be resized - ignore those resizings
if (m_bInsideUpdate)
return; // Do not allow recursive calls
// Lock out recursion
m_bInsideUpdate = TRUE;
// update the horizontal to reflect reality
// NOTE: turning on/off the scrollbars will cause 'OnSize' callbacks
ASSERT(m_totalDev.cx >= 0 && m_totalDev.cy >= 0);
CRect rectClient;
BOOL bCalcClient = TRUE;
// allow parent to do inside-out layout first
CWnd* pParentWnd = GetParent();
if (pParentWnd != NULL)
{
// if parent window responds to this message, use just
// client area for scroll bar calc -- not "true" client area
if ((BOOL)pParentWnd->SendMessage(WM_RECALCPARENT, 0,
(LPARAM)(LPCRECT)&rectClient) != 0)
{
// use rectClient instead of GetTrueClientSize for
// client size calculation.
bCalcClient = FALSE;
}
}
CSize sizeClient;
CSize sizeSb;
if (bCalcClient)
{
// get client rect
if (!GetTrueClientSize(sizeClient, sizeSb))
{
// no room for scroll bars (common for zero sized elements)
CRect rect;
GetClientRect(&rect);
if (rect.right > 0 && rect.bottom > 0)
{
// if entire client area is not invisible, assume we have
// control over our scrollbars
EnableScrollBarCtrl(SB_BOTH, FALSE);
}
m_bInsideUpdate = FALSE;
return;
}
}
else
{
// let parent window determine the "client" rect
GetScrollBarSizes(sizeSb);
sizeClient.cx = rectClient.right - rectClient.left;
sizeClient.cy = rectClient.bottom - rectClient.top;
}
// enough room to add scrollbars
CSize sizeRange;
CPoint ptMove;
CSize needSb;
// get the current scroll bar state given the true client area
GetScrollBarState(sizeClient, needSb, sizeRange, ptMove, bCalcClient);
if (needSb.cx)
sizeClient.cy -= sizeSb.cy;
if (needSb.cy)
sizeClient.cx -= sizeSb.cx;
// first scroll the window as needed
ScrollToDevicePosition(ptMove); // will set the scroll bar positions too
// this structure needed to update the scrollbar page range
SCROLLINFO info;
info.fMask = SIF_PAGE|SIF_RANGE;
info.nMin = 0;
// now update the bars as appropriate
EnableScrollBarCtrl(SB_HORZ, needSb.cx);
if (needSb.cx)
{
info.nPage = sizeClient.cx;
info.nMax = m_totalDev.cx-1;
if (!SetScrollInfo(SB_HORZ, &info, TRUE))
SetScrollRange(SB_HORZ, 0, sizeRange.cx, TRUE);
}
EnableScrollBarCtrl(SB_VERT, needSb.cy);
if (needSb.cy)
{
info.nPage = sizeClient.cy;
info.nMax = m_totalDev.cy-1;
if (!SetScrollInfo(SB_VERT, &info, TRUE))
SetScrollRange(SB_VERT, 0, sizeRange.cy, TRUE);
}
// remove recursion lockout
m_bInsideUpdate = FALSE;
}
void CXDialog::ScrollToDevicePosition(POINT ptDev)
{
ASSERT(ptDev.x >= 0);
ASSERT(ptDev.y >= 0);
// Note: ScrollToDevicePosition can and is used to scroll out-of-range
// areas as far as CXDialog is concerned -- specifically in
// the print-preview code. Since OnScrollBy makes sure the range is
// valid, ScrollToDevicePosition does not vector through OnScrollBy.
int xOrig = GetScrollPos(SB_HORZ);
SetScrollPos(SB_HORZ, ptDev.x);
int yOrig = GetScrollPos(SB_VERT);
SetScrollPos(SB_VERT, ptDev.y);
ScrollWindow(xOrig - ptDev.x, yOrig - ptDev.y);
}
void CXDialog::ScrollToPosition(POINT pt) // logical coordinates
{
ASSERT(m_nMapMode > 0); // not allowed for shrink to fit
if (m_nMapMode != MM_TEXT)
{
CWindowDC dc(NULL);
dc.SetMapMode(m_nMapMode);
dc.LPtoDP((LPPOINT)&pt);
}
// now in device coordinates - limit if out of range
int xMax = GetScrollLimit(SB_HORZ);
int yMax = GetScrollLimit(SB_VERT);
if (pt.x < 0)
pt.x = 0;
else if (pt.x > xMax)
pt.x = xMax;
if (pt.y < 0)
pt.y = 0;
else if (pt.y > yMax)
pt.y = yMax;
ScrollToDevicePosition(pt);
}
void CXDialog::SetScaleToFitSize(SIZE sizeTotal)
{
// Note: It is possible to set sizeTotal members to negative values to
// effectively invert either the X or Y axis.
ASSERT(m_hWnd != NULL);
m_nMapMode = MM_SCALETOFIT; // special internal value
m_totalLog = sizeTotal;
// reset and turn any scroll bars off
if (m_hWnd != NULL && (GetStyle() & (WS_HSCROLL|WS_VSCROLL)))
{
SetScrollPos(SB_HORZ, 0);
SetScrollPos(SB_VERT, 0);
EnableScrollBarCtrl(SB_BOTH, FALSE);
ASSERT((GetStyle() & (WS_HSCROLL|WS_VSCROLL)) == 0);
}
CRect rectT;
GetClientRect(rectT);
m_totalDev = rectT.Size();
if (m_hWnd != NULL)
{
// window has been created, invalidate
UpdateBars();
Invalidate(TRUE);
}
}
void CXDialog::SetScrollSizes(int nMapMode, SIZE sizeTotal,
const SIZE& sizePage, const SIZE& sizeLine)
{
ASSERT(sizeTotal.cx >= 0 && sizeTotal.cy >= 0);
ASSERT(nMapMode > 0);
ASSERT(nMapMode != MM_ISOTROPIC && nMapMode != MM_ANISOTROPIC);
int nOldMapMode = m_nMapMode;
m_nMapMode = nMapMode;
m_totalLog = sizeTotal;
//BLOCK: convert logical coordinate space to device coordinates
{
CWindowDC dc(NULL);
ASSERT(m_nMapMode > 0);
dc.SetMapMode(m_nMapMode);
// total size
m_totalDev = m_totalLog;
dc.LPtoDP((LPPOINT)&m_totalDev);
m_pageDev = sizePage;
dc.LPtoDP((LPPOINT)&m_pageDev);
m_lineDev = sizeLine;
dc.LPtoDP((LPPOINT)&m_lineDev);
if (m_totalDev.cy < 0)
m_totalDev.cy = -m_totalDev.cy;
if (m_pageDev.cy < 0)
m_pageDev.cy = -m_pageDev.cy;
if (m_lineDev.cy < 0)
m_lineDev.cy = -m_lineDev.cy;
} // release DC here
// now adjust device specific sizes
ASSERT(m_totalDev.cx >= 0 && m_totalDev.cy >= 0);
if (m_pageDev.cx == 0)
m_pageDev.cx = m_totalDev.cx / 10;
if (m_pageDev.cy == 0)
m_pageDev.cy = m_totalDev.cy / 10;
if (m_lineDev.cx == 0)
m_lineDev.cx = m_pageDev.cx / 10;
if (m_lineDev.cy == 0)
m_lineDev.cy = m_pageDev.cy / 10;
if (m_hWnd != NULL)
{
// window has been created, invalidate now
UpdateBars();
if (nOldMapMode != m_nMapMode)
Invalidate(TRUE);
}
}
BOOL CXDialog::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
// TODO: Add your specialized code here and/or call the base class
return CDialog::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}
BOOL CXDialog::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
ASSERT(pMsg != NULL);
ASSERT_VALID(this);
ASSERT(m_hWnd != NULL);
// allow tooltip messages to be filtered
if (m_bAccelToParentFrm && pMsg->message >= WM_KEYFIRST && pMsg->message <= WM_KEYLAST )
{
// use document specific accelerator table over m_hAccelTable
CView* pView =(CView*)GetParent();
CDocument *pDoc=NULL;
if(pView && pView->IsKindOf(RUNTIME_CLASS(CView)))
{
pDoc=pView->GetDocument();
}
if(pDoc==NULL)
goto PreMsgOut;
if(pMsg->wParam==VK_TAB)
goto PreMsgOut;
if(pMsg->wParam==VK_SPACE)
goto PreMsgOut;
if(pMsg->wParam==VK_RETURN)
goto PreMsgOut;
if(pMsg->wParam==VK_BACK)
goto PreMsgOut;
if(VK_LEFT <= pMsg->wParam && pMsg->wParam <= VK_DOWN)
goto PreMsgOut;
if(VK_END <= pMsg->wParam && pMsg->wParam <= VK_HOME)
goto PreMsgOut;
if(VK_INSERT <= pMsg->wParam && pMsg->wParam <= VK_DELETE)
goto PreMsgOut;
if(VK_NUMPAD0 <= pMsg->wParam && pMsg->wParam <= VK_DIVIDE)
goto PreMsgOut;
if(pMsg->wParam==VK_F1)
goto PreMsgOut;
char ch=(char)pMsg->wParam;
if('a'<=ch && ch<='z')
ch+='A'-'a';
if(ch=='C')
goto PreMsgOut;
if(ch=='V')
goto PreMsgOut;
if(ch=='X')
goto PreMsgOut;
if(ch=='Z')
goto PreMsgOut;
if(ch=='Y')
goto PreMsgOut;
HACCEL hAccel = pDoc->GetDefaultAccelerator();
return (hAccel != NULL &&
::TranslateAccelerator(pView->m_hWnd, hAccel, pMsg));
goto PreMsgOut;
}
PreMsgOut:
return CDialog::PreTranslateMessage(pMsg);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/uesoft/AutoPHS.git
git@gitee.com:uesoft/AutoPHS.git
uesoft
AutoPHS
AutoPHS
CAE

搜索帮助

D67c1975 1850385 1daf7b77 1850385