BattViewerDlg.cpp

Go to the documentation of this file.
00001 //
00002 // Copyright 2003 Sony Corporation 
00003 //
00004 // Permission to use, copy, modify, and redistribute this software for
00005 // non-commercial use is hereby granted.
00006 //
00007 // This software is provided "as is" without warranty of any kind,
00008 // either expressed or implied, including but not limited to the
00009 // implied warranties of fitness for a particular purpose.
00010 //
00011 // BattViewerDlg.cpp : implementation file
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 // CAboutDlg dialog used for App About
00030 
00031 class CAboutDlg : public CDialog
00032 {
00033 public:
00034         CAboutDlg();
00035 
00036 // Dialog Data
00037         //{{AFX_DATA(CAboutDlg)
00038         enum { IDD = IDD_ABOUTBOX };
00039         //}}AFX_DATA
00040 
00041         // ClassWizard generated virtual function overrides
00042         //{{AFX_VIRTUAL(CAboutDlg)
00043         protected:
00044         virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
00045         //}}AFX_VIRTUAL
00046 
00047 // Implementation
00048 protected:
00049         //{{AFX_MSG(CAboutDlg)
00050         //}}AFX_MSG
00051         DECLARE_MESSAGE_MAP()
00052 };
00053 
00054 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
00055 {
00056         //{{AFX_DATA_INIT(CAboutDlg)
00057         //}}AFX_DATA_INIT
00058 }
00059 
00060 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
00061 {
00062         CDialog::DoDataExchange(pDX);
00063         //{{AFX_DATA_MAP(CAboutDlg)
00064         //}}AFX_DATA_MAP
00065 }
00066 
00067 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
00068         //{{AFX_MSG_MAP(CAboutDlg)
00069                 // No message handlers
00070         //}}AFX_MSG_MAP
00071 END_MESSAGE_MAP()
00072 
00074 // CBattViewerDlg dialog
00075 
00076 CBattViewerDlg::CBattViewerDlg(CWnd* pParent /*=NULL*/)
00077         : CDialog(CBattViewerDlg::IDD, pParent)
00078 {
00079         //{{AFX_DATA_INIT(CBattViewerDlg)
00080                 // NOTE: the ClassWizard will add member initialization here
00081         //}}AFX_DATA_INIT
00082         // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
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         //{{AFX_DATA_MAP(CBattViewerDlg)
00093         DDX_Control(pDX, IDC_PROGRESS1, m_battCapacity);
00094         //}}AFX_DATA_MAP
00095 }
00096 
00097 BEGIN_MESSAGE_MAP(CBattViewerDlg, CDialog)
00098         //{{AFX_MSG_MAP(CBattViewerDlg)
00099         ON_WM_SYSCOMMAND()
00100         ON_WM_PAINT()
00101         ON_WM_QUERYDRAGICON()
00102         //}}AFX_MSG_MAP
00103         ON_MESSAGE(WM_USER + 1, LocalUpdate)
00104 END_MESSAGE_MAP()
00105 
00107 // CBattViewerDlg message handlers
00108 
00109 BOOL CBattViewerDlg::OnInitDialog()
00110 {
00111         CDialog::OnInitDialog();
00112 
00113         // Add "About..." menu item to system menu.
00114 
00115         // IDM_ABOUTBOX must be in the system command range.
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         // Set the icon for this dialog.  The framework does this automatically
00132         //  when the application's main window is not a dialog
00133         SetIcon(m_hIcon, TRUE);                 // Set big icon
00134         SetIcon(m_hIcon, FALSE);                // Set small icon
00135         
00136         // TODO: Add extra initialization here
00137         m_battCapacity.SetRange(0, 100);
00138         m_battCapacity.SetPos(0);
00139         m_battCapacity.SetStep(1);
00140 
00141         UpdateBattCapacity();
00142 
00143         return TRUE;  // return TRUE  unless you set the focus to a control
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 // If you add a minimize button to your dialog, you will need the code below
00160 //  to draw the icon.  For MFC applications using the document/view model,
00161 //  this is automatically done for you by the framework.
00162 
00163 void CBattViewerDlg::OnPaint() 
00164 {
00165         if (IsIconic())
00166         {
00167                 CPaintDC dc(this); // device context for painting
00168 
00169                 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
00170 
00171                 // Center icon in client rectangle
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                 // Draw the icon
00180                 dc.DrawIcon(x, y, m_hIcon);
00181         }
00182         else
00183         {
00184                 CDialog::OnPaint();
00185         }
00186 }
00187 
00188 // The system calls this to obtain the cursor to display while the user drags
00189 //  the minimized window.
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         // Windows message (WM_USER + 1) comes from BattChecker program
00230         UpdateBattCapacity();
00231         return 0;
00232 }

Generated on Sun Dec 2 23:04:28 2007 for openSDK by  doxygen 1.3.9.1