www.pudn.com > WriteReg.rar > WriteReg.cpp


// WriteReg.cpp : Defines the entry point for the application. 
// 
 
#include "stdafx.h" 
#include "WriteReg.h" 
#include  
 
#include  
#include  
#include  
 
#define ID_TIMER 1 
#define MAX_LOADSTRING 100 
 
#define WM_GET_REG_YEAR		    WM_USER+1000 
#define WM_GET_REG_MONTH		WM_USER+1001 
#define WM_GET_REG_DAY		    WM_USER+1002 
#define WM_GET_REG_HOUR		    WM_USER+1003 
#define WM_GET_REG_MINUTE		WM_USER+1004 
#define WM_GET_REG_SECOND		WM_USER+1005 
 
#define REG_PATH     _T("\\DataCounter") 
 
#define REG_VALUE_LAST_SESSION    _T("lastsession") 
#define REG_VALUE_TOTAL_DATA    _T("totaldata") 
#define REG_VALUE_YEAR   _T("year") 
#define REG_VALUE_MONTH   _T("month")                            
#define REG_VALUE_DAY   _T("day") 
#define REG_VALUE_HOUR   _T("hour") 
#define REG_VALUE_MINUTE   _T("minute") 
#define REG_VALUE_SECOND   _T("second") 
 
#define TIMER_MODIFY_REG 2000 
 
// Global Variables: 
HINSTANCE			g_hInst;			// current instance 
HWND				g_hWndMenuBar;		// menu bar handle 
 
//static time variable 
static DWORD dYear; 
static DWORD dMonth; 
static DWORD dDay; 
static DWORD dHour; 
static DWORD dMinute; 
static DWORD dSecond; 
 
// Forward declarations of functions included in this code module: 
ATOM			MyRegisterClass(HINSTANCE, LPTSTR); 
BOOL			InitInstance(HINSTANCE, int); 
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM); 
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM); 
 
bool IsModified();//Judge the value changed 
 
int WINAPI WinMain(HINSTANCE hInstance, 
                   HINSTANCE hPrevInstance, 
                   LPTSTR    lpCmdLine, 
                   int       nCmdShow) 
{ 
    MSG msg; 
 
    // Perform application initialization: 
    if (!InitInstance(hInstance, nCmdShow))  
    { 
        return FALSE; 
    } 
 
    HACCEL hAccelTable; 
    hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WRITEREG)); 
 
    // Main message loop: 
    while (GetMessage(&msg, NULL, 0, 0))  
    { 
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))  
        { 
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
        } 
    } 
 
    return (int) msg.wParam; 
} 
 
// 
//  FUNCTION: MyRegisterClass() 
// 
//  PURPOSE: Registers the window class. 
// 
//  COMMENTS: 
// 
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass) 
{ 
    WNDCLASS wc; 
 
    wc.style         = CS_HREDRAW | CS_VREDRAW; 
    wc.lpfnWndProc   = WndProc; 
    wc.cbClsExtra    = 0; 
    wc.cbWndExtra    = 0; 
    wc.hInstance     = hInstance; 
    wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WRITEREG)); 
    wc.hCursor       = 0; 
    wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); 
    wc.lpszMenuName  = 0; 
    wc.lpszClassName = szWindowClass; 
 
    return RegisterClass(&wc); 
} 
 
// 
//   FUNCTION: InitInstance(HINSTANCE, int) 
// 
//   PURPOSE: Saves instance handle and creates main window 
// 
//   COMMENTS: 
// 
//        In this function, we save the instance handle in a global variable and 
//        create and display the main program window. 
// 
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 
{ 
    HWND hWnd; 
    TCHAR szTitle[MAX_LOADSTRING];		// title bar text 
    TCHAR szWindowClass[MAX_LOADSTRING];	// main window class name 
 
    g_hInst = hInstance; // Store instance handle in our global variable 
 
    // SHInitExtraControls should be called once during your application's initialization to initialize any 
    // of the device specific controls such as CAPEDIT and SIPPREF. 
    SHInitExtraControls(); 
 
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);  
    LoadString(hInstance, IDC_WRITEREG, szWindowClass, MAX_LOADSTRING); 
 
    //If it is already running, then focus on the window, and exit 
    hWnd = FindWindow(szWindowClass, szTitle);	 
    if (hWnd)  
    { 
        // set focus to foremost child window 
        // The "| 0x00000001" is used to bring any owned windows to the foreground and 
        // activate them. 
        SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001)); 
        return 0; 
    }  
 
    if (!MyRegisterClass(hInstance, szWindowClass)) 
    { 
        return FALSE; 
    } 
 
    hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE, 
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); 
 
    if (!hWnd) 
    { 
        return FALSE; 
    } 
 
    // When the main window is created using CW_USEDEFAULT the height of the menubar (if one 
    // is created is not taken into account). So we resize the window after creating it 
    // if a menubar is present 
    if (g_hWndMenuBar) 
    { 
        RECT rc; 
        RECT rcMenuBar; 
 
        GetWindowRect(hWnd, &rc); 
        GetWindowRect(g_hWndMenuBar, &rcMenuBar); 
        rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top); 
 
        MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE); 
    } 
 
    ShowWindow(hWnd, nCmdShow); 
    UpdateWindow(hWnd); 
 
 
    return TRUE; 
} 
 
// 
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) 
// 
//  PURPOSE:  Processes messages for the main window. 
// 
//  WM_COMMAND	- process the application menu 
//  WM_PAINT	- Paint the main window 
//  WM_DESTROY	- post a quit message and return 
// 
// 
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    int wmId, wmEvent; 
    PAINTSTRUCT ps; 
    HDC hdc; 
 
    static SHACTIVATEINFO s_sai; 
 
    HREGNOTIFY hYearNotify = NULL;//declare a reg notify structure 
    HREGNOTIFY hMonthNotify = NULL;//declare a reg notify structure 
    HREGNOTIFY hDayNotify = NULL;//declare a reg notify structure 
    HREGNOTIFY hHourNotify = NULL;//declare a reg notify structure 
    HREGNOTIFY hMinuteNotify = NULL;//declare a reg notify structure 
    HREGNOTIFY hSecondNotify = NULL;//declare a reg notify structure 
 
    NOTIFICATIONCONDITION nc;//declare nc structure 
 
    DWORD dTotaldata;//the variable store total data 
    DWORD dLastsession;//the variable store last session 
 
    dTotaldata = 0;//set totaldata to 0 
    dLastsession = 0;//set lastsession to 0 
 
    switch (message)  
    { 
    case WM_COMMAND: 
        wmId    = LOWORD(wParam);  
        wmEvent = HIWORD(wParam);  
        // Parse the menu selections: 
        switch (wmId) 
        { 
        case IDM_HELP_ABOUT: 
            DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About); 
            break; 
        case IDM_OK: 
            SendMessage (hWnd, WM_CLOSE, 0, 0);				 
            break; 
        default: 
            return DefWindowProc(hWnd, message, wParam, lParam); 
        } 
        break; 
    case WM_CREATE: 
        SHMENUBARINFO mbi; 
 
        memset(&mbi, 0, sizeof(SHMENUBARINFO)); 
        mbi.cbSize     = sizeof(SHMENUBARINFO); 
        mbi.hwndParent = hWnd; 
        mbi.nToolBarId = IDR_MENU; 
        mbi.hInstRes   = g_hInst; 
 
        if (!SHCreateMenuBar(&mbi))  
        { 
            g_hWndMenuBar = NULL; 
        } 
        else 
        { 
            g_hWndMenuBar = mbi.hwndMB; 
        } 
 
        // Initialize the shell activate info structure 
        memset(&s_sai, 0, sizeof (s_sai)); 
        s_sai.cbSize = sizeof (s_sai); 
 
        //set nc structure 
        nc.ctComparisonType = REG_CT_ANYCHANGE; 
        nc.dwMask           = 0xffff; 
        //nc.TargetValue.dw   = 0; 
 
        //set value 
        dYear = 0; 
        dMonth = 0; 
        dDay = 0; 
        dHour = 0; 
        dMinute = 0; 
        dSecond = 0; 
 
        //watch registry 
 
        RegistryNotifyWindow(HKEY_LOCAL_MACHINE, REG_PATH,REG_VALUE_YEAR, hWnd, WM_GET_REG_YEAR, NULL, &nc, &hYearNotify); 
        RegistryNotifyWindow(HKEY_LOCAL_MACHINE, REG_PATH,REG_VALUE_MONTH, hWnd, WM_GET_REG_MONTH, NULL, &nc, &hMonthNotify); 
        RegistryNotifyWindow(HKEY_LOCAL_MACHINE, REG_PATH,REG_VALUE_DAY, hWnd, WM_GET_REG_DAY, NULL, &nc, &hDayNotify); 
        RegistryNotifyWindow(HKEY_LOCAL_MACHINE, REG_PATH,REG_VALUE_HOUR, hWnd, WM_GET_REG_HOUR, NULL, &nc, &hHourNotify); 
        RegistryNotifyWindow(HKEY_LOCAL_MACHINE, REG_PATH,REG_VALUE_MINUTE, hWnd, WM_GET_REG_MINUTE, NULL, &nc, &hMinuteNotify); 
        RegistryNotifyWindow(HKEY_LOCAL_MACHINE, REG_PATH,REG_VALUE_SECOND, hWnd, WM_GET_REG_SECOND, NULL, &nc, &hSecondNotify); 
 
        RegistrySetDWORD(HKEY_LOCAL_MACHINE, REG_PATH, REG_VALUE_LAST_SESSION, 0);//set flux 
 
        SetTimer (hWnd, ID_TIMER, TIMER_MODIFY_REG, NULL) ;//set a timer 
 
        break; 
    case WM_GET_REG_YEAR://get year change 
 
        if(IsModified()) 
            RegistrySetDWORD(HKEY_LOCAL_MACHINE, REG_PATH, REG_VALUE_TOTAL_DATA, 0);//set to zero 
 
        break; 
    case WM_GET_REG_MONTH://get month change 
 
        if(IsModified()) 
            RegistrySetDWORD(HKEY_LOCAL_MACHINE, REG_PATH, REG_VALUE_TOTAL_DATA, 0);//set to zero 
 
        break; 
    case WM_GET_REG_DAY://get day change 
 
        if(IsModified()) 
            RegistrySetDWORD(HKEY_LOCAL_MACHINE, REG_PATH, REG_VALUE_TOTAL_DATA, 0);//set to zero 
 
        break; 
    case WM_GET_REG_HOUR://get hour change 
 
        if(IsModified()) 
            RegistrySetDWORD(HKEY_LOCAL_MACHINE, REG_PATH, REG_VALUE_TOTAL_DATA, 0);//set to zero 
 
        break; 
    case WM_GET_REG_MINUTE://get minute change 
 
        if(IsModified()) 
            RegistrySetDWORD(HKEY_LOCAL_MACHINE, REG_PATH, REG_VALUE_TOTAL_DATA, 0);//set to zero 
 
        break; 
    case WM_GET_REG_SECOND://get second change 
 
        if(IsModified()) 
            RegistrySetDWORD(HKEY_LOCAL_MACHINE, REG_PATH, REG_VALUE_TOTAL_DATA, 0);//set to zero 
 
        break; 
    case WM_TIMER: 
 
        RegistryGetDWORD(HKEY_LOCAL_MACHINE, REG_PATH, REG_VALUE_TOTAL_DATA, &dTotaldata);//get total data 
        RegistryGetDWORD(HKEY_LOCAL_MACHINE, REG_PATH, REG_VALUE_LAST_SESSION, &dLastsession);//get last session 
 
        dTotaldata += 20;//increase total data 
        dLastsession += 20;//increase last session 
 
        RegistrySetDWORD(HKEY_LOCAL_MACHINE, REG_PATH, REG_VALUE_TOTAL_DATA, dTotaldata);//set total data 
        RegistrySetDWORD(HKEY_LOCAL_MACHINE, REG_PATH, REG_VALUE_LAST_SESSION, dLastsession);//set last session 
 
        break; 
    case WM_PAINT: 
        hdc = BeginPaint(hWnd, &ps); 
 
 
        EndPaint(hWnd, &ps); 
        break; 
    case WM_DESTROY: 
        CommandBar_Destroy(g_hWndMenuBar); 
        PostQuitMessage(0); 
 
        if(hYearNotify) 
        { 
            RegistryCloseNotification(hYearNotify);//close registry notification 
        } 
        if(hMonthNotify) 
        { 
            RegistryCloseNotification(hMonthNotify);//close registry notification 
        } 
        if(hDayNotify) 
        { 
            RegistryCloseNotification(hDayNotify);//close registry notification 
        } 
        if(hHourNotify) 
        { 
            RegistryCloseNotification(hHourNotify);//close registry notification 
        } 
        if(hMinuteNotify) 
        { 
            RegistryCloseNotification(hMinuteNotify);//close registry notification 
        } 
        if(hSecondNotify) 
        { 
            RegistryCloseNotification(hSecondNotify);//close registry notification 
        } 
 
        KillTimer (hWnd, ID_TIMER) ;//destroy timer 
 
        break; 
 
    case WM_ACTIVATE: 
        // Notify shell of our activate message 
        SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE); 
        break; 
    case WM_SETTINGCHANGE: 
        SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai); 
        break; 
 
    default: 
        return DefWindowProc(hWnd, message, wParam, lParam); 
    } 
    return 0; 
} 
 
// Message handler for about box. 
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    switch (message) 
    { 
    case WM_INITDIALOG: 
        { 
            // Create a Done button and size it.   
            SHINITDLGINFO shidi; 
            shidi.dwMask = SHIDIM_FLAGS; 
            shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU; 
            shidi.hDlg = hDlg; 
            SHInitDialog(&shidi); 
        } 
        return (INT_PTR)TRUE; 
 
    case WM_COMMAND: 
        if (LOWORD(wParam) == IDOK) 
        { 
            EndDialog(hDlg, LOWORD(wParam)); 
            return TRUE; 
        } 
        break; 
 
    case WM_CLOSE: 
        EndDialog(hDlg, message); 
        return TRUE; 
 
    } 
    return (INT_PTR)FALSE; 
} 
 
bool IsModified() 
{ 
    DWORD dTemp;//a variable store temporary reg value 
 
    dTemp = 0;//set to 0 
 
    bool bResult;//return variable 
 
    bResult = false;//set to false 
 
    if(S_OK != RegistryGetDWORD(HKEY_LOCAL_MACHINE, REG_PATH, REG_VALUE_SECOND, &dTemp))//get total data 
 
        return false; 
 
    if(dSecond != dTemp) 
    { 
        bResult = true; 
    } 
 
    dSecond = dTemp;//store second 
 
    if(S_OK != RegistryGetDWORD(HKEY_LOCAL_MACHINE, REG_PATH, REG_VALUE_MINUTE, &dTemp))//get total data 
    { 
        return false; 
    } 
 
    if(dMinute != dTemp) 
    { 
        bResult = true; 
    } 
 
    dMinute = dTemp; 
 
    if(S_OK != RegistryGetDWORD(HKEY_LOCAL_MACHINE, REG_PATH, REG_VALUE_HOUR, &dTemp))//get total data      
    { 
        return false; 
    } 
    if(dHour!= dTemp) 
    { 
        bResult = true; 
    } 
 
    dHour = dTemp; 
 
    if(S_OK != RegistryGetDWORD(HKEY_LOCAL_MACHINE, REG_PATH, REG_VALUE_DAY, &dTemp))//get total data     
    { 
        return false; 
    } 
    if(dDay != dTemp) 
    { 
        bResult = true; 
    } 
 
    dDay = dTemp; 
 
    if(S_OK != RegistryGetDWORD(HKEY_LOCAL_MACHINE, REG_PATH, REG_VALUE_MONTH, &dTemp))//get total data 
    { 
        return false; 
    } 
 
    if(dMonth != dTemp) 
    { 
        bResult = true; 
    } 
 
    dMonth = dTemp; 
 
    if(S_OK != RegistryGetDWORD(HKEY_LOCAL_MACHINE, REG_PATH, REG_VALUE_YEAR, &dTemp))//get total data 
    { 
        return false; 
    } 
 
    if(dYear != dTemp) 
    { 
        bResult = true; 
    } 
 
    dYear = dTemp; 
 
    return bResult; 
}