00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "stdafx.h"
00015 #include "BattViewer.h"
00016 #include "BattViewerDlg.h"
00017
00018
00019 #ifdef _DEBUG
00020 #define new DEBUG_NEW
00021 #undef THIS_FILE
00022 static char THIS_FILE[] = __FILE__;
00023 #endif
00024
00025 static const char MapName[] = "MemMap1";
00026 static const char MutexName[] = "Mutex1";
00027
00029
00030
00031 class CAboutDlg : public CDialog
00032 {
00033 public:
00034 CAboutDlg();
00035
00036
00037
00038 enum { IDD = IDD_ABOUTBOX };
00039
00040
00041
00042
00043 protected:
00044 virtual void DoDataExchange(CDataExchange* pDX);
00045
00046
00047
00048 protected:
00049
00050
00051 DECLARE_MESSAGE_MAP()
00052 };
00053
00054 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
00055 {
00056
00057
00058 }
00059
00060 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
00061 {
00062 CDialog::DoDataExchange(pDX);
00063
00064
00065 }
00066
00067 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
00068
00069
00070
00071 END_MESSAGE_MAP()
00072
00074
00075
00076 CBattViewerDlg::CBattViewerDlg(CWnd* pParent )
00077 : CDialog(CBattViewerDlg::IDD, pParent)
00078 {
00079
00080
00081
00082
00083 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
00084 m_hMem1 = NULL;
00085 m_pShrMem1 = NULL;
00086 m_hMutex = NULL;
00087 }
00088
00089 void CBattViewerDlg::DoDataExchange(CDataExchange* pDX)
00090 {
00091 CDialog::DoDataExchange(pDX);
00092
00093 DDX_Control(pDX, IDC_PROGRESS1, m_battCapacity);
00094
00095 }
00096
00097 BEGIN_MESSAGE_MAP(CBattViewerDlg, CDialog)
00098
00099 ON_WM_SYSCOMMAND()
00100 ON_WM_PAINT()
00101 ON_WM_QUERYDRAGICON()
00102
00103 ON_MESSAGE(WM_USER + 1, LocalUpdate)
00104 END_MESSAGE_MAP()
00105
00107
00108
00109 BOOL CBattViewerDlg::OnInitDialog()
00110 {
00111 CDialog::OnInitDialog();
00112
00113
00114
00115
00116 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
00117 ASSERT(IDM_ABOUTBOX < 0xF000);
00118
00119 CMenu* pSysMenu = GetSystemMenu(FALSE);
00120 if (pSysMenu != NULL)
00121 {
00122 CString strAboutMenu;
00123 strAboutMenu.LoadString(IDS_ABOUTBOX);
00124 if (!strAboutMenu.IsEmpty())
00125 {
00126 pSysMenu->AppendMenu(MF_SEPARATOR);
00127 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
00128 }
00129 }
00130
00131
00132
00133 SetIcon(m_hIcon, TRUE);
00134 SetIcon(m_hIcon, FALSE);
00135
00136
00137 m_battCapacity.SetRange(0, 100);
00138 m_battCapacity.SetPos(0);
00139 m_battCapacity.SetStep(1);
00140
00141 UpdateBattCapacity();
00142
00143 return TRUE;
00144 }
00145
00146 void CBattViewerDlg::OnSysCommand(UINT nID, LPARAM lParam)
00147 {
00148 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
00149 {
00150 CAboutDlg dlgAbout;
00151 dlgAbout.DoModal();
00152 }
00153 else
00154 {
00155 CDialog::OnSysCommand(nID, lParam);
00156 }
00157 }
00158
00159
00160
00161
00162
00163 void CBattViewerDlg::OnPaint()
00164 {
00165 if (IsIconic())
00166 {
00167 CPaintDC dc(this);
00168
00169 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
00170
00171
00172 int cxIcon = GetSystemMetrics(SM_CXICON);
00173 int cyIcon = GetSystemMetrics(SM_CYICON);
00174 CRect rect;
00175 GetClientRect(&rect);
00176 int x = (rect.Width() - cxIcon + 1) / 2;
00177 int y = (rect.Height() - cyIcon + 1) / 2;
00178
00179
00180 dc.DrawIcon(x, y, m_hIcon);
00181 }
00182 else
00183 {
00184 CDialog::OnPaint();
00185 }
00186 }
00187
00188
00189
00190 HCURSOR CBattViewerDlg::OnQueryDragIcon()
00191 {
00192 return (HCURSOR) m_hIcon;
00193 }
00194
00195
00196 BOOL CBattViewerDlg::UpdateBattCapacity()
00197 {
00198 int capacity;
00199 if (!(m_hMutex = CreateMutex(NULL, FALSE, MutexName))) {
00200 return FALSE;
00201 }
00202 if (!m_pShrMem1) {
00203 HANDLE MapFileHandle = (HANDLE) 0xFFFFFFFF;
00204 WaitForSingleObject(m_hMutex, INFINITE);
00205 if (!(m_hMem1 = CreateFileMapping(MapFileHandle, NULL, PAGE_READWRITE, 0, 4096, MapName))) {
00206 ReleaseMutex(m_hMutex);
00207 return FALSE;
00208 }
00209 if (!(m_pShrMem1 = MapViewOfFile(m_hMem1, FILE_MAP_WRITE | FILE_MAP_READ, 0, 0, 4096))) {
00210 ReleaseMutex(m_hMutex);
00211 return FALSE;
00212 }
00213 m_pUnionShrMem.p_v = m_pShrMem1;
00214 m_pUnionShrMem.p_d->hWnd = GetSafeHwnd();
00215 m_pUnionShrMem.p_d->cap = 0;
00216 ReleaseMutex(m_hMutex);
00217 }
00218 WaitForSingleObject(m_hMutex, INFINITE);
00219 capacity = m_pUnionShrMem.p_d->cap;
00220 if (!m_pUnionShrMem.p_d->hWnd)
00221 m_pUnionShrMem.p_d->hWnd = GetSafeHwnd();
00222 ReleaseMutex(m_hMutex);
00223 m_battCapacity.SetPos(capacity);
00224 return TRUE;
00225 }
00226
00227 LRESULT CBattViewerDlg::LocalUpdate(WPARAM a, LPARAM b)
00228 {
00229
00230 UpdateBattCapacity();
00231 return 0;
00232 }