📄 basedialog.cpp
字号:
/*******************************************************************************
* Copyright (C) 1980-2008 Xumingxsh
* All rights reserved.
*
* 文件名称: BaseDialog.cpp
* 创建日期: 2008-04-08
* 创 建 人: 徐敏荣
* 说 明: 类CBaseDialog的实现文件
*-------------------------------------------------------------------------------
* 版本 日 期 修改人 修改说明
*-------------------------------------------------------------------------------
* 1.0.0 2008-04-08 徐敏荣 完成初始版本
*******************************************************************************/
#include "stdafx.h"
#include "BaseDialog.h"
#include "ToolKit.h"
#include "LogFile.h"
#include "MFCINi.h"
#include "SystemConfig.h"
#include "ItemKey.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
XMRArray<CBaseDialog*> CBaseDialog::s_DialogArr;
HWND CBaseDialog::CretateWnd(CreateWndStruct &wndStruct)
{
return ::CreateWindow(
wndStruct.lpszClass,
wndStruct.lpszName,
wndStruct.style,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
wndStruct.hwndParent,
wndStruct.hMenu,
wndStruct.hInstance,
wndStruct.lpCreateParams
);
}
// 回调函数的公共处理方法
void CBaseDialog::CommonProc(CBaseDialog &wndDlg, HWND hWnd,
UINT message, WPARAM wParam, LPARAM lParam)
{
wndDlg.CommandProc(hWnd, message, wParam, lParam);
}
void CBaseDialog::CommandProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
{
this->m_hWnd = hWnd; // 设置控件句柄
this->CreateControls(); // 创建,定位主窗口上按钮
this->SetControlPropertity(); // 在代码中设置控件的属性
this->MoveWnd();
this->InitDataBeforeShow();
ToolKit::OnShowWnd(hWnd);
this->InitDataAfterShow();
this->MoveWnd();
}
break;
case WM_PAINT:
{
this->OnPaint();
}
break;
case MFC_SHOWWND:
{
this->InitDataBeforeShow();
ToolKit::OnShowWnd(hWnd);
this->InitDataAfterShow();
this->MoveWnd();
}
break;
case WM_COMMAND:
{
// 相应主窗口上按钮操作
this->Command(wParam, lParam);
}
break;
case WM_DRAWITEM:
{
this->DrawItem(wParam,lParam);
}
break;
default:
this->OtherMsgProc(hWnd, message, wParam, lParam);
break;
}
}
void CBaseDialog::OtherMsgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
}
CBaseDialog* CBaseDialog::GetBaseDialog(HWND hwnd)
{
int size = CBaseDialog::s_DialogArr.GetSize();
for (int i = 0; i < size; i++)
{
if (CBaseDialog::s_DialogArr.GetAt(i)->m_hWnd == hwnd)
{
return CBaseDialog::s_DialogArr.GetAt(i);
}
}
return NULL;
}
// 构造函数
CBaseDialog::CBaseDialog()
{
this->m_strFile = "";
this->m_hwndControls = NULL;
this->m_ControlCount = 0;
this->m_hWnd = NULL;
this->m_WndStruct.dwExStyle = 0;
this->m_WndStruct.style = WS_POPUP | WS_CAPTION | WS_VISIBLE;
this->m_WndStruct.hwndParent = NULL;
this->m_WndStruct.hInstance = GetModuleHandle(NULL);//g_hInst;
this->m_WndStruct.hMenu = NULL;
this->m_WndStruct.lpszName = SystemConfig::GetConfig()->m_chTitle;
this->m_WndStruct.lpCreateParams = NULL;
this->m_MainRect = *SystemConfig::GetConfig()->m_MainRECT;
}
// 析构函数
CBaseDialog::~CBaseDialog()
{
if (this->m_hwndControls)
{
delete[] this->m_hwndControls;
}
this->OnDestroyWindow();
}
// 界面处理命令函数,应该写成纯虚函数
void CBaseDialog::Command(WPARAM wParam, LPARAM lParam)
{
DWORD dCommand = LOWORD(wParam);
int size = this->m_MsgMap.GetSize();
for (int i = 0; i < size; i++)
{
MsgEvent event = this->m_MsgMap.GetAt(i);
if (dCommand == event.mMessage)
{
(this->*(event.pEvent))(wParam, lParam);
return;
}
}
}
// 显示界面
void CBaseDialog::OnShow()
{
// 如果界面没有创建,则首先创建界面
if (m_hWnd)
{
::SendMessage(this->m_hWnd, MFC_SHOWWND, 0, 1);
}
else
{
this->m_WndStruct.lpszClass = this->m_ClassName;
CBaseDialog::CretateWnd(this->m_WndStruct);
}
}
void CBaseDialog::InitDataAfterShow()
{
}
void CBaseDialog::InitDataBeforeShow()
{
}
// 隐藏界面
void CBaseDialog::OnHide()
{
::ShowWindow(this->m_hWnd, SW_HIDE);
}
// 销毁窗体
void CBaseDialog::OnDestroyWindow()
{
::DestroyWindow(m_hWnd);
}
// 根据配置信息创建当前界面所需的控件
void CBaseDialog::CreateControls()
{
// 打开配置文件,根据配置项个数创建存储控件句柄的数组
MFCINi ini;
ini.Open(ItemKey::INI_FOLDER + m_strFile);
if (ini.m_nItemCount < 1)
{
return;
}
this->m_hwndControls = new BaseControl[ini.m_nItemCount];
this->m_ControlCount = ini.m_nItemCount;
int nID = 102;
for (int i = 0; i < ini.m_nItemCount; i++)
{
this->m_hwndControls[i].m_nID = nID + i;
this->m_hwndControls[i].CreateControl(
ini.m_chItemValues[i], ini.m_chItems[i], this->m_hWnd);
}
}
// 在创建控件后设置控件的属性
void CBaseDialog::SetControlPropertity()
{
}
// 绘制控件
void CBaseDialog::DrawItem(WPARAM wParam,LPARAM lParam)
{
DWORD hMenu = LOWORD(wParam);
BaseControl* control = this->GetControlByID(hMenu);
if (control)
{
control->DrawItem(wParam, lParam);
}
}
// 设置界面及控件的位置
void CBaseDialog::MoveWnd()
{
::MoveWindow(this->m_hWnd, this->m_MainRect.left,
this->m_MainRect.top, this->m_MainRect.right - this->m_MainRect.left,
this->m_MainRect.bottom - this->m_MainRect.top, true);
for (int i = 0; i < this->m_ControlCount; i++)
{
this->m_hwndControls[i].OnMove();
}
}
// 根据控件的键值取得控件ID
unsigned long CBaseDialog::GetIDByKey(char *chKey)
{
CString strKey;
strKey.Format("%s", chKey);
for (int i = 0; i < this->m_ControlCount; i++)
{
if (this->m_hwndControls[i].m_CProperty.m_strKey == strKey)
{
return this->m_hwndControls[i].m_nID;
}
}
return -1;
}
// 根据控件ID取得控件
BaseControl* CBaseDialog::GetControlByID(int nID)
{
for (int i = 0; i < this->m_ControlCount; i++)
{
if (this->m_hwndControls[i].m_nID == nID)
{
return &this->m_hwndControls[i];
}
}
return NULL;
}
// 根据控件主键取得控件
BaseControl* CBaseDialog::GetControlByKey(CString strKey)
{
for (int i = 0; i < this->m_ControlCount; i++)
{
if (this->m_hwndControls[i].m_CProperty.m_strKey == strKey)
{
return &this->m_hwndControls[i];
}
}
return NULL;
}
// 取得界面中的所有控件
BaseControl* CBaseDialog::GetControls()
{
return this->m_hwndControls;
}
// 取得控件的个数
int CBaseDialog::GetControlsCount()
{
return this->m_ControlCount;
}
void CBaseDialog::OnPaint()
{
PAINTSTRUCT pt;
HDC hdc;
hdc = BeginPaint(this->m_hWnd, &pt);
this->OnDraw(&hdc);
EndPaint(this->m_hWnd, &pt);
}
void CBaseDialog::OnDraw(HDC *hDC)
{
if (!SystemConfig::GetConfig()->m_bIsDevelop)
{
return;
}
CDC* dc = CDC::FromHandle(*hDC);
CRect rcClient;
GetClientRect(this->m_hWnd, &rcClient);
int height = rcClient.Height();
int width = rcClient.Width();
// 设置光栅操作模式
int nDrawMode;
nDrawMode = dc->GetROP2();
dc->SetROP2(R2_NOTXORPEN);
CPoint* point1;
CPoint* point2;
CPen myPen;
CPen* pOldPen;
myPen.CreatePen(PS_SOLID, 1, RGB(192, 192, 192));
pOldPen = dc->SelectObject(&myPen);
CPen myPen2;
myPen2.CreatePen(PS_SOLID, 3, RGB(0, 192, 192));
CString kk;
for (int i = 0; i < height; i += 10)
{
if ((i/10)%5 == 0)
{
dc->SelectObject(&myPen2);
kk.Format("%d",i);
dc->TextOut(8, i, kk);
dc->TextOut(width - 20, i, kk);
}
point1 = new CPoint(0, i);
point2 = new CPoint(width, i);
dc->MoveTo(*point1);
dc->LineTo(*point2);
dc->SelectObject(&myPen);
}
for (int j = 0; j < width; j += 10)
{
if ((j/10)%5 == 0)
{
dc->SelectObject(&myPen2);
kk.Format("%d",j);
dc->TextOut(j, 8, kk);
dc->TextOut(j, height - 20, kk);
}
point1 = new CPoint(j, 0);
point2 = new CPoint(j, height);
dc->MoveTo(*point1);
dc->LineTo(*point2);
dc->SelectObject(&myPen);
}
// 恢复原设置
dc->SetROP2(nDrawMode);
dc->SelectObject(pOldPen);
}
void CBaseDialog::EnableControl(int nID, bool isEnable)
{
HWND hwnd = ::GetDlgItem(this->m_hWnd, nID);
if (hwnd != NULL)
{
::EnableWindow(hwnd, isEnable);
}
}
void CBaseDialog::SetCtrlText(int nId, CString text)
{
HWND hwnd = ::GetDlgItem(this->m_hWnd, nId);
if (hwnd != NULL)
{
::SetWindowText(hwnd, TEXT(text));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -