CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2009
    Posts
    14

    Angry DTN_DATETIMECHANGE loop!

    hi! I have a date-time picker control.
    when I select a date it gives 2 notify and when I change month I get an endless loop of notify!!!
    here's my code...

    Code:
    #define   STRICT
    
    #include  <windows.h>
    #include  <stdio.h>
    #include  <commctrl.h>
    
    #pragma comment(lib, "comctl32.lib")
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) 
    {
       static SYSTEMTIME st;
       static HWND       hWndStartDate, hWndStartTime;
       static char       szDate[15], szTime[10], szStamp[20];
       NMDATETIMECHANGE  *Date;
       CREATESTRUCT      *cs;
    
       switch (iMsg) 
       {
       case WM_CREATE :
          cs = (LPCREATESTRUCT)lParam;
          GetLocalTime(&st);
          GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, &st, "yyyyMMdd", szDate, sizeof(szDate));
          GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, &st, "HHmmss", szTime, sizeof(szTime));
    
          hWndStartDate = CreateWindowEx(0, DATETIMEPICK_CLASS, NULL,
             WS_BORDER | WS_CHILD | WS_VISIBLE,
             10, 10, 100, 25,
             hwnd, NULL, cs->hInstance, NULL);
    
          hWndStartTime = CreateWindowEx(0, DATETIMEPICK_CLASS, NULL,
             WS_BORDER | WS_CHILD | WS_VISIBLE | DTS_TIMEFORMAT,
             10, 40, 100, 25,
             hwnd, NULL, cs->hInstance, NULL);
          break;
    
       case WM_NOTIFY :
          switch(((LPNMHDR)lParam)->code)
          {
          case DTN_DATETIMECHANGE :
             Date = (LPNMDATETIMECHANGE)lParam;
             if(Date->nmhdr.hwndFrom == hWndStartDate || Date->nmhdr.hwndFrom == hWndStartTime)
             {
                DateTime_SetRange(hWndStartDate, GDTR_MAX, &st);
                DateTime_SetRange(hWndStartTime, GDTR_MAX, &st);
                MessageBox(NULL , _T("hello"), _T("hello"), MB_OK);
             }
             break;
    
          case DTN_DROPDOWN :
             MonthCal_SetFirstDayOfWeek(DateTime_GetMonthCal(hWndStartDate), 0);
             break;
          }
          break;
    
       case WM_COMMAND :
          if (IDCANCEL == LOWORD(wParam)) 
          {  
             DateTime_GetSystemtime(hWndStartDate, &st);
             DateTime_GetSystemtime(hWndStartTime, &st);
             break;
          }
          break;
    
       case WM_CLOSE :
          DestroyWindow(hwnd);
          break;
    
       case WM_DESTROY :
          PostQuitMessage(0);
          break;
       }
       return DefWindowProc(hwnd, iMsg, wParam, lParam);
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) 
    {
       MSG                  msg;
       WNDCLASS             wndclass;
       HWND                 hwnd;
       INITCOMMONCONTROLSEX icex;
       char                 szAppName[] = "DatePickerApp";
    
       wndclass.style         = 0;
       wndclass.lpfnWndProc   = WndProc;
       wndclass.cbClsExtra    = 0;
       wndclass.cbWndExtra    = 0;
       wndclass.hInstance     = hInstance;
       wndclass.hIcon         = LoadIcon(hInstance, szAppName);
       wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
       wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
       wndclass.lpszMenuName  = szAppName;
       wndclass.lpszClassName = szAppName;
       RegisterClass(&wndclass);
    
       icex.dwSize = sizeof(icex);
       icex.dwICC = ICC_DATE_CLASSES;
       InitCommonControlsEx(&icex);
    
       hwnd = CreateWindow(szAppName,
          szAppName,
          WS_OVERLAPPEDWINDOW | WS_VISIBLE,
          0, 0, 400, 400,
          NULL, NULL, hInstance, szCmdLine);
    
       while (GetMessage(&msg, NULL, 0, 0)) 
       {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
       }
       return (int)msg.wParam;
    }
    help!!!
    Last edited by EvIl_DeViL; September 9th, 2009 at 03:36 PM.

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: DTN_DATETIMECHANGE loop!

    It seems like you need to move this:
    Code:
    DateTime_SetRange(hWndStartDate, GDTR_MAX, &st);
    DateTime_SetRange(hWndStartTime, GDTR_MAX, &st);
    from DTN_DATETIMECHANGE handler into WM_CREATE after controls are created. You don't need to set range every time control is changed. Also remove MessageBox from there. That notification might be called many times over, that coupled with a message box being shown may also deadlock your app. If you need to know when that notification is sent put a debugger breakpoint somewehre inside, or use Beep() for an audible signal.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured