📄 runtimedlg.cpp
字号:
// RuntimeDlg.cpp : implementation file
//
#include "stdafx.h"
#include "RuntimeDlg.h"
#include "dlgunits.h"
#include "winstyles.h"
#include "winclasses.h"
#include "wclassdefines.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRuntimeDlg
class RTDLGTEMPLATE : private DLGTEMPLATE
{
public:
RTDLGTEMPLATE(DWORD dwStyle, DWORD dwExStyle, const CRect& rect)
{
style = dwStyle;
dwExtendedStyle = dwExStyle;
x = (short)rect.left;
y = (short)rect.top;
cx = (short)rect.Width();
cy = (short)rect.Height();
CDlgUnits().FromPixels(x, y);
CDlgUnits().FromPixels(cx, cy);
cdit = 0; // always 0
wMenu = 0; // always 0
wClass = 0; // always 0
nCaption = 0; // always 0
}
virtual ~RTDLGTEMPLATE()
{
}
operator LPCDLGTEMPLATE() { return (LPCDLGTEMPLATE)this; }
private:
WORD wMenu;
WORD wClass;
short nCaption;
};
/////////////////////////////////////////////////////////////////////////////
const CRect CRuntimeDlg::rectAuto = CRect(0, 0, 0, 0);
CMapStringToString CRuntimeDlg::s_mapClasses;
CRuntimeDlg::CRuntimeDlg() : m_hILarge(NULL), m_hISmall(NULL)
{
AfxInitRichEdit();
if (!s_mapClasses.GetCount())
BuildClassMap();
SetBordersDLU(7);
}
CRuntimeDlg::~CRuntimeDlg()
{
::DestroyIcon(m_hILarge);
::DestroyIcon(m_hISmall);
}
BEGIN_MESSAGE_MAP(CRuntimeDlg, CDialog)
//{{AFX_MSG_MAP(CRuntimeDlg)
ON_WM_SETFOCUS()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRuntimeDlg message handlers
// private mfc functions we need to access
extern void AFXAPI AfxHookWindowCreate(CWnd* pWnd);
extern BOOL AFXAPI AfxUnhookWindowCreate();
void CRuntimeDlg::OnCancel()
{
// filter out if we're a child
if (GetStyle() & WS_CHILD)
return;
CDialog::OnCancel();
}
void CRuntimeDlg::OnOK()
{
// filter out if we're a child
if (GetStyle() & WS_CHILD)
return;
CDialog::OnOK();
}
int CRuntimeDlg::DoModal(LPCTSTR szCaption, DWORD dwStyle, DWORD dwExStyle, const CRect& rect, CWnd* pParentWnd, UINT nID)
{
// fail immediately if no controls have been added
if (!m_lstControls.GetCount())
return IDCANCEL;
// setup for modal loop and creation
m_nModalResult = -1;
m_nFlags |= WF_CONTINUEMODAL;
// disable parent (before creating dialog)
HWND hWndParent = PreModal();
AfxUnhookWindowCreate();
BOOL bEnableParent = FALSE;
if (hWndParent != NULL && ::IsWindowEnabled(hWndParent))
{
::EnableWindow(hWndParent, FALSE);
bEnableParent = TRUE;
}
try
{
// create modeless dialog
if (Create(szCaption, dwStyle, dwExStyle, rect, pParentWnd ? pParentWnd : CWnd::FromHandle(hWndParent), nID))
{
if (m_nFlags & WF_CONTINUEMODAL)
{
// enter modal loop
DWORD dwFlags = MLF_SHOWONIDLE;
if (GetStyle() & DS_NOIDLEMSG)
dwFlags |= MLF_NOIDLEMSG;
VERIFY(RunModalLoop(dwFlags) == m_nModalResult);
}
// hide the window before enabling the parent, etc.
if (m_hWnd != NULL)
SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
}
}
catch(...)
{
m_nModalResult = -1;
}
if (bEnableParent)
::EnableWindow(hWndParent, TRUE);
if (hWndParent != NULL && ::GetActiveWindow() == m_hWnd)
::SetActiveWindow(hWndParent);
// destroy modal window
DestroyWindow();
PostModal();
return m_nModalResult;
}
BOOL CRuntimeDlg::OnInitDialog()
{
// make sure we create the controls before calling the base class
CreateControls();
CDialog::OnInitDialog();
// add icons only if we have no parent
if (!GetParent())
{
CWinApp* pApp = AfxGetApp();
char szAppPath[MAX_PATH + 1];
::GetModuleFileName(NULL, szAppPath, MAX_PATH);
if (::ExtractIconEx(szAppPath, 0, &m_hILarge, &m_hISmall, 1))
{
SetIcon(m_hILarge ? m_hILarge : m_hISmall, TRUE);
SetIcon(m_hISmall ? m_hISmall : m_hILarge, FALSE);
}
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
BOOL CRuntimeDlg::Create(LPCTSTR szCaption, DWORD dwStyle, DWORD dwExStyle, const CRect& rect, CWnd* pParentWnd, UINT nID)
{
// cache and remove visibility
BOOL bVisible = (dwStyle & WS_VISIBLE);
dwStyle &= ~WS_VISIBLE;
// remove DS_SETFONT (not supported)
dwStyle &= ~DS_SETFONT;
// create modeless dialog
AfxHookWindowCreate(this);
if (CreateDlgIndirect(RTDLGTEMPLATE(dwStyle, dwExStyle, rect), pParentWnd, NULL) != NULL)
{
// size to fit?
if (rect == rectAuto)
AutoFit();
else
MoveWindow(rect);
// center
if (dwStyle & DS_CENTER)
CenterWindow();
// set window text
SetWindowText(szCaption);
// set control id
SetDlgCtrlID(nID);
PostCreate(); // for derived classes
// reshow?
if (bVisible)
ShowWindow(SW_SHOW);
return TRUE;
}
return FALSE;
}
void CRuntimeDlg::AutoFit()
{
// iterate all the child controls accumulating the largest bottom right coord
CRect rClient(0, 0, 0, 0);
CWnd* pCtrl = GetWindow(GW_CHILD);
while (pCtrl)
{
CRect rCtrl;
pCtrl->GetWindowRect(rCtrl);
ScreenToClient(rCtrl);
rClient.right = max(rClient.right, rCtrl.right);
rClient.bottom = max(rClient.bottom, rCtrl.bottom);
pCtrl = pCtrl->GetNextWindow();
}
// add border
rClient.right += m_rBorders.right;
rClient.bottom += m_rBorders.bottom;
// calc new window rect
CRect rWindow;
GetWindowRect(rWindow);
CalcWindowRect(rClient);
// match centerpoints of old and new
rClient = CRect(rWindow.TopLeft(), rClient.Size());
rClient.OffsetRect(rWindow.CenterPoint() - rClient.CenterPoint());
MoveWindow(rClient);
}
BOOL CRuntimeDlg::AddControl(LPCTSTR szClass, LPCTSTR szCaption, DWORD dwStyle, DWORD dwExStyle,
const CRect& rect, UINT nID)
{
return AddControl(szClass, szCaption, dwStyle, dwExStyle, rect.left, rect.top, rect.Width(), rect.Height(), nID);
}
BOOL CRuntimeDlg::AddControl(LPCTSTR szClass, LPCTSTR szCaption, DWORD dwStyle, DWORD dwExStyle,
int x, int y, int cx, int cy, UINT nID)
{
if (GetSafeHwnd())
return (NULL != CreateControl(szClass, szCaption, dwStyle, dwExStyle, x, y, cx, cy, nID, FALSE));
else
m_lstControls.AddTail(RTCONTROL(NULL, szClass, szCaption, dwStyle, dwExStyle, CRect(x, y, x + cx, y + cy), nID, FALSE));
return TRUE;
}
BOOL CRuntimeDlg::AddControl(CWnd* pWnd, LPCTSTR szCaption, DWORD dwStyle, DWORD dwExStyle,
const CRect& rect, UINT nID)
{
return AddControl(pWnd, szCaption, dwStyle, dwExStyle, rect.left, rect.top, rect.Width(), rect.Height(), nID);
}
BOOL CRuntimeDlg::AddControl(CWnd* pWnd, LPCTSTR szCaption, DWORD dwStyle, DWORD dwExStyle,
int x, int y, int cx, int cy, UINT nID)
{
if (GetSafeHwnd())
return CreateControl(pWnd, szCaption, dwStyle, dwExStyle, x, y, cx, cy, nID, FALSE);
else
m_lstControls.AddTail(RTCONTROL(pWnd, NULL, szCaption, dwStyle, dwExStyle, CRect(x, y, x + cx, y + cy), nID, FALSE));
return TRUE;
}
BOOL CRuntimeDlg::AddRCControl(LPCTSTR szRCType, LPCTSTR szClass, LPCTSTR szCaption, DWORD dwStyle,
DWORD dwExStyle, int x, int y, int cx, int cy, UINT nID, UINT nIconID)
{
CString sClass(szClass);
// get the win32 class name
if (sClass.IsEmpty() || stricmp(szRCType, "CONTROL") != 0)
{
if (!CRCCtrlParser::GetClassName(szRCType, sClass) || sClass.IsEmpty())
return FALSE;
}
if (!dwStyle)
dwStyle = CRCCtrlParser::GetDefaultStyles(sClass);
if (!(dwStyle & WS_NOTVISIBLE))
dwStyle |= WS_VISIBLE;
else
dwStyle &= ~WS_NOTVISIBLE;
if (CRCCtrlParser::CtrlWantsClientEdge(sClass))
dwExStyle |= WS_EX_CLIENTEDGE;
if (GetSafeHwnd())
return (NULL != CreateControl(sClass, szCaption, dwStyle, dwExStyle, x, y, cx, cy, nID, TRUE, nIconID));
else
m_lstControls.AddTail(RTCONTROL(NULL, sClass, szCaption, dwStyle, dwExStyle, CRect(x, y, x + cx, y + cy), nID, TRUE, nIconID));
return TRUE;
}
BOOL CRuntimeDlg::AddRCControl(CWnd* pWnd, LPCTSTR szCaption, DWORD dwStyle,
DWORD dwExStyle, int x, int y, int cx, int cy, UINT nID, UINT nIconID)
{
CString sClass = GetClassName(pWnd);
if (!dwStyle)
dwStyle = CRCCtrlParser::GetDefaultStyles(sClass);
if (!(dwStyle & WS_NOTVISIBLE))
dwStyle |= WS_VISIBLE;
else
dwStyle &= ~WS_NOTVISIBLE;
if (CRCCtrlParser::CtrlWantsClientEdge(sClass))
dwExStyle |= WS_EX_CLIENTEDGE;
if (GetSafeHwnd())
return (NULL != CreateControl(pWnd, szCaption, dwStyle, dwExStyle, x, y, cx, cy, nID, TRUE, nIconID));
else
m_lstControls.AddTail(RTCONTROL(pWnd, sClass, szCaption, dwStyle, dwExStyle, CRect(x, y, x + cx, y + cy), nID, TRUE, nIconID));
return TRUE;
}
HWND CRuntimeDlg::CreateControl(LPCTSTR szClass, LPCTSTR szCaption, DWORD dwStyle, DWORD dwExStyle,
int x, int y, int cx, int cy, UINT nID, BOOL bDLU, UINT nIconID)
{
if (bDLU)
{
CDlgUnits dlu(*this);
dlu.ToPixels(x, y);
dlu.ToPixels(cx, cy);
}
// validate the topleft position against the borders
x = max(x, m_rBorders.left);
y = max(y, m_rBorders.top);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -