我建立了SDI的应用程序,在View中需要调用两个Dialog,在View仅中使用了OnDraw,两个Dialog里有未使用OnPaint,结果发现当窗体最小化还原的时候,GDI句柄会增加。下面给出View和其中一个Dialog的代码,请高手指点!Doc代码:
void COPASDoc::OnGenCurve()
{
CString lpszSQL,value,strPlan;
CURVEPOINT PointT,Point1,Point2,Point4;
BOOL bCreate=FALSE;
if (dlg==NULL&&Dutydlg==NULL)
{
dlg = new CRtShow();
Dutydlg = new CDutyDlg();
dlg->Create(IDD_RTDlg,NULL);
Dutydlg->Create(IDD_DUTY,NULL);
dlg->ShowWindow(SW_SHOW);
Dutydlg->ShowWindow(SW_SHOW);
bCreate=TRUE;
}

POSITION pos=GetFirstViewPosition();
COPASView *pView=(COPASView *)GetNextView(pos);
//中止之前所有绘制曲线的定时线程
pView->KillTimer(POWER_CURVE);
pView->KillTimer(POWER_ONE);
pView->KillTimer(POWER_TWO);
pView->KillTimer(POWER_FOUR);
pView->KillTimer(GEN_ONE);
pView->KillTimer(GEN_TWO);
pView->KillTimer(GEN_FOUR); pView->SetTimer(GEN_CURVE,5000,NULL);
}View代码:
void COPASView::OnDraw(CDC* pDC)
{
int i; DrawAxis(pDC);
DrawLabel(pDC);
for(i=0;i<m_nCurves;i++)
{ DrawCurve(pDC,i);
}
for(i=0;i<m_aryMarks.GetSize();i++)
{ DrawMark(i,pDC);
}}其中一个Dialog的代码:
// DutyDlg.cpp : implementation file
//#include "stdafx.h"
#include "opas.h"
#include "DutyDlg.h"
#include "ADOconnect.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endifCADOconnect Conn_duty;/////////////////////////////////////////////////////////////////////////////
// CDutyDlg dialog
CDutyDlg::CDutyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDutyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDutyDlg)
//}}AFX_DATA_INIT
}
void CDutyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDutyDlg)
DDX_Control(pDX, IDC_PowStatic, m_ShowPow);
DDX_Control(pDX, IDC_GenStatic, m_ShowGen);
DDX_Control(pDX, IDC_SelectRTDevi, m_RTPow);
DDX_Control(pDX, IDC_STATICLIST, m_StaticList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDutyDlg, CDialog)
//{{AFX_MSG_MAP(CDutyDlg)
ON_WM_CTLCOLOR()
ON_BN_CLICKED(IDC_PowStatic, OnPowStatic)
ON_BN_CLICKED(IDC_GenStatic, OnGenStatic)
ON_WM_TIMER()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CDutyDlg message handlersHBRUSH CDutyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
BOOL bReleaseDC=FALSE;
if(pDC==NULL)
{
pDC=GetDC();
bReleaseDC=TRUE;
} HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

// TODO: Change any attributes of the DC here
if (nCtlColor==CTLCOLOR_STATIC)
{
pDC->SetTextColor(RGB(255,255,0));
}
if (nCtlColor==CTLCOLOR_DLG||nCtlColor==CTLCOLOR_STATIC)
{
pDC->SetBkColor(RGB(0,0,0));
HBRUSH m_Bkdlg=CreateSolidBrush(RGB(0,0,0));
return m_Bkdlg;
}

if(bReleaseDC)
{ ReleaseDC(pDC);
} // TODO: Return a different brush if the default is not desired
return hbr;
}double CDutyDlg::GetDevi()
{
if (m_RTPow.GetCurSel()==1)
{
RTDevi=0.02;
m_RTPow.SetCurSel(1);
}
else if (m_RTPow.GetCurSel()==2)
{
RTDevi=0.03;
m_RTPow.SetCurSel(2);
}
else if (m_RTPow.GetCurSel()==3)
{
RTDevi=0.04;
m_RTPow.SetCurSel(3);
}
else if (m_RTPow.GetCurSel()==4)
{
RTDevi=0.05;
m_RTPow.SetCurSel(4);
}
else
{
RTDevi=0.01;
m_RTPow.SetCurSel(0);
} return RTDevi;
}BOOL CDutyDlg::OnInitDialog() 
{
CDialog::OnInitDialog(); // TODO: Add extra initialization here
Conn_duty.ConnectDB("NARI-CHENNING","OPAS","CHENHIT","CHENHIT");
t=CTime::GetCurrentTime();
strTime=t.Format("%Y-%m-%d");

Flag.Format("elec"); Init(id,plan,real);
SetTimer(222,5000,NULL);

return TRUE;  // return TRUE unless you set the focus to a control
              // EXCEPTION: OCX Property Pages should return FALSE
}BOOL CDutyDlg::PreTranslateMessage(MSG* pMsg) 
{
// TODO: Add your specialized code here and/or call the base class

return CDialog::PreTranslateMessage(pMsg);
}void CDutyDlg::Init(CString id[],CString plan[],CString real[])
{
CString lpszSQL,strTemp,value; lpszSQL.Format("select * from generating_plan_daily where device_id=0 and date='%s'",strTime);
if(Conn_duty.Open(lpszSQL,adCmdText,adOpenStatic,adLockOptimistic))
Conn_duty.QueryOneValue(3,vPlanT); m_StaticList.DeleteAllItems();
DWORD dwExStyle = LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_UNDERLINEHOT|LVS_EX_REGIONAL;
m_StaticList.SetExtendedStyle(dwExStyle); m_StaticList.InsertColumn(0,"编号",LVCFMT_CENTER,40);
m_StaticList.InsertColumn(1,"计划",LVCFMT_CENTER,60);
m_StaticList.InsertColumn(2,"实际",LVCFMT_CENTER,60); for (int i=0;i<288;i++)
{
id[i].Format("%d",i+1);
real[i].Format("0.0");
} lpszSQL.Format("select * from generating_act_realtime where device_id=0");
if(Conn_duty.Open(lpszSQL,adCmdText,adOpenStatic,adLockOptimistic))
Conn_duty.QueryOneValue(1,value);
strTemp.Format("%s",value);
SetDlgItemText(IDC_STATIC_T,strTemp); if (Flag=="generating")
{
lpszSQL.Format("select * from generating_plan_daily where device_id=0 and date='%s'",strTime);
if(Conn_duty.Open(lpszSQL,adCmdText,adOpenStatic,adLockOptimistic))
Conn_duty.QueryAllsValue(3,291,plan);
}
else
{
lpszSQL.Format("select * from elec_plan_daily where device_id=0 and date='%s'",strTime);
if(Conn_duty.Open(lpszSQL,adCmdText,adOpenStatic,adLockOptimistic))
Conn_duty.QueryAllsValue(3,291,plan);
} InsertTypeItem(&m_StaticList,id,plan,real);
}void CDutyDlg::InsertTypeItem(CListCtrl *pList,CString id[],CString plan[],CString real[])
{
int nIndex=pList->GetItemCount();
LV_ITEM lvItem;
for (int i=0;i<288;i++)
{
lvItem.mask=LVIF_TEXT;
lvItem.iItem=nIndex;
lvItem.iSubItem=0;
lvItem.pszText=(char*)(LPCTSTR)id[i];
pList->InsertItem(&lvItem);
pList->SetItemText(nIndex,1,plan[i]);
pList->SetItemText(nIndex++,2,real[i]);
}
}void CDutyDlg::OnPowStatic() 
{
// TODO: Add your control notification handler code here
Flag.Format("elec");
}void CDutyDlg::OnGenStatic() 
{
// TODO: Add your control notification handler code here
Flag.Format("generating");
}void CDutyDlg::OnTimer(UINT nIDEvent) 
{
CString lpszSQL,ReFreshTime,RTData,strTemp,RTime;
int CountT=0,CountM=0,CountA=0,CountN=0;
double value; t=CTime::GetCurrentTime();
RTime=t.Format("%H:%M:%S"); // TODO: Add your message handler code here and/or call default
if (nIDEvent==222)
{
lpszSQL.Format("select * from generating_act_realtime where device_id=0");
if(Conn_duty.Open(lpszSQL,adCmdText,adOpenStatic,adLockOptimistic))
Conn_duty.QueryOneValue(2,ReFreshTime);
if (RTime=="07:00:00")
{
CountT=0;
strTemp.Format("%d",CountT);
SetDlgItemText(IDC_STATIC_TI,strTemp);
CountM=0;
strTemp.Format("%d",CountM);
SetDlgItemText(IDC_STATIC_MI,strTemp);
CountA=0;
strTemp.Format("%d",CountA);
SetDlgItemText(IDC_STATIC_AI,strTemp);
CountN=0;
strTemp.Format("%d",CountN);
SetDlgItemText(IDC_STATIC_NI,strTemp);
}
if (value>vPlanT*1.02||value<vPlanT*0.98)
{
if (strcmp(ReFreshTime,"07:00:00")>0&&strcmp(ReFreshTime,"15:00:00")<0)
{
CountT++;
strTemp.Format("%d",CountT);
SetDlgItemText(IDC_STATIC_TI,strTemp);
CountM++;
strTemp.Format("%d",CountM);
SetDlgItemText(IDC_STATIC_MI,strTemp);
SetDlgItemText(IDC_STATIC_vTIME,ReFreshTime);
strTemp.Format("%.2lf",vPlanT);
SetDlgItemText(IDC_STATIC_vPLAN,strTemp);
SetDlgItemText(IDC_STATIC_vACT,RTData);
strTemp.Format("%.2lf%%",100*(value-vPlanT)/vPlanT);
SetDlgItemText(IDC_STATIC_vDEVI,strTemp);
}
if (strcmp(ReFreshTime,"15:00:00")>0&&strcmp(ReFreshTime,"23:00:00")<0)
{
CountT++;
strTemp.Format("共有%d个不合格点",CountT);
SetDlgItemText(IDC_STATIC_TI,strTemp);
CountM++;
strTemp.Format("有%d个不合格点",CountM);
SetDlgItemText(IDC_STATIC_AI,strTemp);
SetDlgItemText(IDC_STATIC_vTIME,ReFreshTime);
strTemp.Format("%.2lf",vPlanT);
SetDlgItemText(IDC_STATIC_vPLAN,strTemp);
SetDlgItemText(IDC_STATIC_vACT,RTData);
strTemp.Format("%.2lf%%",100*(value-vPlanT)/vPlanT);
SetDlgItemText(IDC_STATIC_vDEVI,strTemp);
}
if (strcmp(ReFreshTime,"23:00:00")>0&&strcmp(ReFreshTime,"07:00:00")<0)
{
CountT++;
strTemp.Format("共有%d个不合格点",CountT);
SetDlgItemText(IDC_STATIC_TI,strTemp);
CountM++;
strTemp.Format("有%d个不合格点",CountN);
SetDlgItemText(IDC_STATIC_MI,strTemp);
SetDlgItemText(IDC_STATIC_vTIME,ReFreshTime);
strTemp.Format("%.2lf",vPlanT);
SetDlgItemText(IDC_STATIC_vPLAN,strTemp);
SetDlgItemText(IDC_STATIC_vACT,RTData);
strTemp.Format("%.2lf%%",100*(value-vPlanT)/vPlanT);
SetDlgItemText(IDC_STATIC_vDEVI,strTemp);
}
}

if (Flag!=OldFlag)
{
Init(id,plan,real);
}
} OldFlag=Flag;

CDialog::OnTimer(nIDEvent);
}