⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timedate.c

📁 一个类似windows
💻 C
📖 第 1 页 / 共 2 页
字号:
      TimeZoneListTail = Entry;
    }
      else
    {
      Current = GetLargerTimeZoneEntry(Entry->Index);
      if (Current != NULL)
        {
          if (Current == TimeZoneListHead)
        {
          /* Prepend to head */
          Entry->Prev = NULL;
          Entry->Next = TimeZoneListHead;
          TimeZoneListHead->Prev = Entry;
          TimeZoneListHead = Entry;
        }
          else
        {
          /* Insert before current */
          Entry->Prev = Current->Prev;
          Entry->Next = Current;
          Current->Prev->Next = Entry;
          Current->Prev = Entry;
        }
        }
      else
        {
          /* Append to tail */
          Entry->Prev = TimeZoneListTail;
          Entry->Next = NULL;
          TimeZoneListTail->Next = Entry;
          TimeZoneListTail = Entry;
        }
    }

      dwIndex++;
    }

  RegCloseKey(hZonesKey);
}


static VOID
DestroyTimeZoneList(VOID)
{
  PTIMEZONE_ENTRY Entry;

  while (TimeZoneListHead != NULL)
    {
      Entry = TimeZoneListHead;

      TimeZoneListHead = Entry->Next;
      if (TimeZoneListHead != NULL)
    {
      TimeZoneListHead->Prev = NULL;
    }

      HeapFree(GetProcessHeap(), 0, Entry);
    }

  TimeZoneListTail = NULL;
}


static VOID
ShowTimeZoneList(HWND hwnd)
{
  TIME_ZONE_INFORMATION TimeZoneInfo;
  PTIMEZONE_ENTRY Entry;
  DWORD dwIndex;
  DWORD i;

  GetTimeZoneInformation(&TimeZoneInfo);

  dwIndex = 0;
  i = 0;
  Entry = TimeZoneListHead;
  while (Entry != NULL)
    {
      SendMessageW(hwnd,
           CB_ADDSTRING,
           0,
           (LPARAM)Entry->Description);

      if (!wcscmp(Entry->StandardName, TimeZoneInfo.StandardName))
    dwIndex = i;

      i++;
      Entry = Entry->Next;
    }

  SendMessageW(hwnd,
           CB_SETCURSEL,
           (WPARAM)dwIndex,
           0);
}


static VOID
SetLocalTimeZone(HWND hwnd)
{
  TIME_ZONE_INFORMATION TimeZoneInformation;
  PTIMEZONE_ENTRY Entry;
  DWORD dwIndex;
  DWORD i;

  dwIndex = SendMessage(hwnd,
            CB_GETCURSEL,
            0,
            0);

  i = 0;
  Entry = TimeZoneListHead;
  while (i < dwIndex)
    {
      if (Entry == NULL)
    return;

      i++;
      Entry = Entry->Next;
    }

  wcscpy(TimeZoneInformation.StandardName,
     Entry->StandardName);
  wcscpy(TimeZoneInformation.DaylightName,
     Entry->DaylightName);

  TimeZoneInformation.Bias = Entry->TimezoneInfo.Bias;
  TimeZoneInformation.StandardBias = Entry->TimezoneInfo.StandardBias;
  TimeZoneInformation.DaylightBias = Entry->TimezoneInfo.DaylightBias;

  memcpy(&TimeZoneInformation.StandardDate,
     &Entry->TimezoneInfo.StandardDate,
     sizeof(SYSTEMTIME));
  memcpy(&TimeZoneInformation.DaylightDate,
     &Entry->TimezoneInfo.DaylightDate,
     sizeof(SYSTEMTIME));

  /* Set time zone information */
  SetTimeZoneInformation(&TimeZoneInformation);
}


static VOID
GetAutoDaylightInfo(HWND hwnd)
{
  HKEY hKey;

  if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
            L"SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation",
            0,
            KEY_QUERY_VALUE,
            &hKey))
    return;

  if (RegQueryValueExW(hKey,
               L"DisableAutoDaylightTimeSet",
               NULL,
               NULL,
               NULL,
               NULL))
    {
      SendMessage(hwnd, BM_SETCHECK, (WPARAM)BST_CHECKED, 0);
    }
  else
    {
      SendMessage(hwnd, BM_SETCHECK, (WPARAM)BST_UNCHECKED, 0);
    }

  RegCloseKey(hKey);
}


static VOID
SetAutoDaylightInfo(HWND hwnd)
{
  HKEY hKey;
  DWORD dwValue = 1;

  if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
            L"SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation",
            0,
            KEY_SET_VALUE,
            &hKey))
    return;

  if (SendMessage(hwnd, BM_GETCHECK, 0, 0) == BST_UNCHECKED)
    {
      RegSetValueExW(hKey,
             L"DisableAutoDaylightTimeSet",
             0,
             REG_DWORD,
             (LPBYTE)&dwValue,
             sizeof(DWORD));
    }
  else
    {
      RegDeleteValueW(hKey,
              L"DisableAutoDaylightTimeSet");
    }

  RegCloseKey(hKey);
}


/* Property page dialog callback */
INT_PTR CALLBACK
TimeZonePageProc(HWND hwndDlg,
         UINT uMsg,
         WPARAM wParam,
         LPARAM lParam)
{
  switch (uMsg)
  {
    case WM_INITDIALOG:
      CreateTimeZoneList();
      ShowTimeZoneList(GetDlgItem(hwndDlg, IDC_TIMEZONELIST));
      GetAutoDaylightInfo(GetDlgItem(hwndDlg, IDC_AUTODAYLIGHT));
      break;

    case WM_COMMAND:
      if ((LOWORD(wParam) == IDC_TIMEZONELIST && HIWORD(wParam) == CBN_SELCHANGE) ||
          (LOWORD(wParam) == IDC_AUTODAYLIGHT && HIWORD(wParam) == BN_CLICKED))
        {
          /* Enable the 'Apply' button */
          PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
        }
      break;

    case WM_DESTROY:
      DestroyTimeZoneList();
      break;

    case WM_NOTIFY:
      {
          LPNMHDR lpnm = (LPNMHDR)lParam;

          switch (lpnm->code)
            {
              case PSN_APPLY:
                SetAutoDaylightInfo(GetDlgItem(hwndDlg, IDC_AUTODAYLIGHT));
                SetLocalTimeZone(GetDlgItem(hwndDlg, IDC_TIMEZONELIST));
                SetWindowLong(hwndDlg, DWL_MSGRESULT, PSNRET_NOERROR);
                return TRUE;

              default:
                break;
            }

      }
      break;
  }

  return FALSE;
}


#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383

static VOID
CreateNTPServerList(HWND hwnd)
{
    HWND hList;
    WCHAR ValName[MAX_VALUE_NAME];
    WCHAR Data[256];
    DWORD Index = 0;
    DWORD ValSize;
    DWORD dwNameSize;
    DWORD Default = 1;
    LONG Ret;
    HKEY hKey;

    hList = GetDlgItem(hwnd,
                       IDC_SERVERLIST);

    Ret = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
                        L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
                        0,
                        KEY_QUERY_VALUE,
                        &hKey);
    if (Ret != ERROR_SUCCESS)
        return;

    while (TRUE)
    {
            ValSize = MAX_VALUE_NAME;
            ValName[0] = '\0';
            Ret = RegEnumValueW(hKey,
                                Index,
                                ValName,
                                &ValSize,
                                NULL,
                                NULL,
                                (LPBYTE)Data,
                                &dwNameSize);

            if (Ret == ERROR_SUCCESS)
            {
                if (wcscmp(ValName, L"") == 0)
                {
                    Default = _wtoi(Data);
                    Index++;
                }
                else
                {
                    SendMessageW(hList,
                                 CB_ADDSTRING,
                                 0,
                                 (LPARAM)Data);
                    Index++;
                }
            }
            else if (Ret != ERROR_MORE_DATA)
                break;
    }

    if (Default < 1 || Default > Index)
        Default = 1;

    SendMessage(hList,
                CB_SETCURSEL,
                --Default,
                0);

    RegCloseKey(hKey);

}


VOID SetNTPServer(HWND hwnd)
{
    HKEY hKey;
    HWND hList;
    INT Sel;
    WCHAR szSel[4];
    LONG Ret;
//DebugBreak();
    hList = GetDlgItem(hwnd,
                       IDC_SERVERLIST);

    Sel = (INT)SendMessage(hList,
                           CB_GETCURSEL,
                           0,
                           0);

    _itow(Sel, szSel, 10);

    Ret = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
                        L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
                        0,
                        KEY_SET_VALUE,
                        &hKey);
    if (Ret != ERROR_SUCCESS)
        return;

    Ret = RegSetValueExW(hKey,
                         L"",
                         0,
                         REG_SZ,
                         (LPBYTE)szSel,
                         sizeof(szSel));
    if (Ret == ERROR_SUCCESS)
        MessageBoxW(NULL, szSel, NULL, 0);
    else
    {
        WCHAR Buff[20];
        _itow(Ret, Buff, 10);
        //MessageBoxW(NULL, Buff, NULL, 0);
    }

    RegCloseKey(hKey);



}


VOID UpdateSystemTime(HWND hwndDlg)
{
    //SYSTEMTIME systime;
    CHAR Buf[BUFSIZE];

    InitialiseConnection();
    SendData();
    RecieveData(Buf);
    DestroyConnection();

    //DateTime_SetSystemtime(hwndDlg, 0, systime);
}

/* Property page dialog callback */
INT_PTR CALLBACK
InetTimePageProc(HWND hwndDlg,
                 UINT uMsg,
                 WPARAM wParam,
                 LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_INITDIALOG:
            CreateNTPServerList(hwndDlg);

        break;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDC_UPDATEBUTTON:
                    SetNTPServer(hwndDlg);
                    //UpdateSystemTime(hwndDlg);
                    MessageBox(NULL, L"Not yet implemented", NULL, 0);
                break;

                case IDC_SERVERLIST:
                    if (HIWORD(wParam) == CBN_SELCHANGE)
                        /* Enable the 'Apply' button */
                        PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
                break;

                case IDC_AUTOSYNC:
                    if (HIWORD(wParam) == BN_CLICKED)
                    {
                        HWND hCheck = GetDlgItem(hwndDlg, IDC_AUTOSYNC);
                        //HWND hSerText = GetDlgItem(hwndDlg, IDC_SERVERTEXT);
                        //HWND hSerList = GetDlgItem(hwndDlg, IDC_SERVERLIST);
                        //HWND hUpdateBut = GetDlgItem(hwndDlg, IDC_UPDATEBUTTON);
                        //HWND hSucSync = GetDlgItem(hwndDlg, IDC_SUCSYNC);
                        //HWND hNextSync = GetDlgItem(hwndDlg, IDC_NEXTSYNC);

                        INT Check = (INT)SendMessageW(hCheck, BM_GETCHECK, 0, 0);
                        if (Check)
                            ;//show all data
                        else
                            ;//hide all data

                        /* Enable the 'Apply' button */
                        PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
                    }
                break;
            }
            break;

        case WM_DESTROY:
        break;

        case WM_NOTIFY:
        {
            LPNMHDR lpnm = (LPNMHDR)lParam;

            switch (lpnm->code)
            {
                case PSN_APPLY:
                    //DebugBreak();
                    SetNTPServer(hwndDlg);

                    return TRUE;

                default:
                break;
            }
        }
        break;
    }

    return FALSE;
}


static VOID
InitPropSheetPage(PROPSHEETPAGE *psp, WORD idDlg, DLGPROC DlgProc)
{
  ZeroMemory(psp, sizeof(PROPSHEETPAGE));
  psp->dwSize = sizeof(PROPSHEETPAGE);
  psp->dwFlags = PSP_DEFAULT;
  psp->hInstance = hApplet;
  psp->pszTemplate = MAKEINTRESOURCE(idDlg);
  psp->pfnDlgProc = DlgProc;
}


LONG APIENTRY
Applet(HWND hwnd, UINT uMsg, LONG wParam, LONG lParam)
{
  PROPSHEETHEADER psh;
  PROPSHEETPAGE psp[3];
  TCHAR Caption[256];
  LONG Ret = 0;

  if (RegisterMonthCalControl(hApplet))
  {
    LoadString(hApplet, IDS_CPLNAME, Caption, sizeof(Caption) / sizeof(TCHAR));

    ZeroMemory(&psh, sizeof(PROPSHEETHEADER));
    psh.dwSize = sizeof(PROPSHEETHEADER);
    psh.dwFlags =  PSH_PROPSHEETPAGE | PSH_PROPTITLE;
    psh.hwndParent = NULL;
    psh.hInstance = hApplet;
    psh.hIcon = LoadIcon(hApplet, MAKEINTRESOURCE(IDC_CPLICON));
    psh.pszCaption = Caption;
    psh.nPages = sizeof(psp) / sizeof(PROPSHEETPAGE);
    psh.nStartPage = 0;
    psh.ppsp = psp;

    InitPropSheetPage(&psp[0], IDD_DATETIMEPAGE, DateTimePageProc);
    InitPropSheetPage(&psp[1], IDD_TIMEZONEPAGE, TimeZonePageProc);
    InitPropSheetPage(&psp[2], IDD_INETTIMEPAGE, InetTimePageProc);

    Ret = (LONG)(PropertySheet(&psh) != -1);

    UnregisterMonthCalControl(hApplet);
  }

  return Ret;
}


/* Control Panel Callback */
LONG CALLBACK
CPlApplet(HWND hwndCpl,
      UINT uMsg,
      LPARAM lParam1,
      LPARAM lParam2)
{
  int i = (int)lParam1;

  switch (uMsg)
  {
    case CPL_INIT:
      return TRUE;

    case CPL_GETCOUNT:
      return NUM_APPLETS;

    case CPL_INQUIRE:
    {
      CPLINFO *CPlInfo = (CPLINFO*)lParam2;
      CPlInfo->lData = 0;
      CPlInfo->idIcon = Applets[i].idIcon;
      CPlInfo->idName = Applets[i].idName;
      CPlInfo->idInfo = Applets[i].idDescription;
      break;
    }

    case CPL_DBLCLK:
    {
      Applets[i].AppletProc(hwndCpl, uMsg, lParam1, lParam2);
      break;
    }
  }
  return FALSE;
}


BOOL STDCALL
DllMain(HINSTANCE hinstDLL,
    DWORD dwReason,
    LPVOID lpReserved)
{
  switch (dwReason)
  {
    case DLL_PROCESS_ATTACH:
      {
    INITCOMMONCONTROLSEX InitControls;

    InitControls.dwSize = sizeof(INITCOMMONCONTROLSEX);
    InitControls.dwICC = ICC_DATE_CLASSES | ICC_PROGRESS_CLASS | ICC_UPDOWN_CLASS;
    InitCommonControlsEx(&InitControls);

    hApplet = hinstDLL;
      }
      break;
  }

  return TRUE;
}

/* EOF */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -