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

    Post Alarm Clock help?

    What this does is it gets the system time and displays in a static box which then the user types in the time in the edit box which then the alarm goes of. its stores it in the registery the message loop then matches the time against the system clock from the registery and exits the program but it wont because the program will not trigger the if statment even though all values are returned?

    Code:
    void LoopTime(HWND Time){
    
    
    struct tm now;
    time_t t_time;
    char time_str[100];
    t_time = time(0);
    now = *localtime(&t_time);
    sprintf(time_str,"%02d:%02d:%02d", now.tm_hour,now.tm_min,now.tm_sec);
    SetWindowText(Time,time_str);
     
    }
    
    #define MAX_LOADSTRING 100
    
    // Global Variables:
    HINSTANCE hInst;								// current instance
    TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
    TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name
    //
    HWND hWnd;
    HWND Time;
    HWND SetAlarm;
    HWND AccessAlarm;
    //
    char TimeHold[9];
    char AlarmHold[9];
    DWORD dwType = 0;
    char szBuffer[9];
    DWORD dwBufferSize = sizeof(szBuffer);
    
    
    // Forward declarations of functions included in this code module:
    ATOM				MyRegisterClass(HINSTANCE hInstance);
    BOOL				InitInstance(HINSTANCE, int);
    LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
    INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
    
    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
    	UNREFERENCED_PARAMETER(hPrevInstance);
    	UNREFERENCED_PARAMETER(lpCmdLine);
    
     	// TODO: Place code here.
    	MSG msg;
    	HACCEL hAccelTable;
    
    	// Initialize global strings
    	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    	LoadString(hInstance, IDC_ACLOCK, szWindowClass, MAX_LOADSTRING);
    	MyRegisterClass(hInstance);
    
    	// Perform application initialization:
    	if (!InitInstance (hInstance, nCmdShow))
    	{
    		return FALSE;
    	}
    
    	
    	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_ACLOCK));
    
    
    	while (GetMessage(&msg, NULL, 0, 0))
    	{
    		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
    		{
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    			SetTimer(hWnd, ID_REFRESH, 100,(TIMERPROC) NULL); //This is the time synchroniser
    			LoopTime(Time); //this retrives the system time
    			
            }
    SendMessage(AccessAlarm,EM_LIMITTEXT,8,0);
    		
    GetWindowText(Time,TimeHold,sizeof(TimeHold));
     RegGetValue(HKEY_CURRENT_USER,"Alarm","Data",RRF_RT_ANY,NULL,(PVOID)&szBuffer,&dwBufferSize);
    
    If(szBuffer == TimeHold){
    
    return 0;   // why does the if statment not trigger?
    }
    
        }
    
    	return (int) msg.wParam;
    }
    
    
    //
    //  FUNCTION: MyRegisterClass()
    //
    //  PURPOSE: Registers the window class.
    //
    //  COMMENTS:
    //
    //    This function and its usage are only necessary if you want this code
    //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
    //    function that was added to Windows 95. It is important to call this function
    //    so that the application will get 'well formed' small icons associated
    //    with it.
    //
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
    	WNDCLASSEX wcex;
    
    	wcex.cbSize = sizeof(WNDCLASSEX);
    
    	wcex.style			= CS_HREDRAW | CS_VREDRAW;
    	wcex.lpfnWndProc	= WndProc;
    	wcex.cbClsExtra		= 0;
    	wcex.cbWndExtra		= 0;
    	wcex.hInstance		= hInstance;
    	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ACLOCK));
    	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
    	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
    	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_ACLOCK);
    	wcex.lpszClassName	= szWindowClass;
    	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
    
    	return RegisterClassEx(&wcex);
    }
    
    //
    //   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)
    {
      
      
       
       hInst = hInstance; // Store instance handle in our global variable
    
       hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
    
    
       Time = CreateWindow("static","",WS_VISIBLE|WS_CHILD,100,100,100,100,hWnd,(HMENU)ID_TIME,hInstance,0);
       SetAlarm = CreateWindow("button","Set Alarm",WS_VISIBLE|WS_CHILD,200,100,100,100,hWnd,(HMENU)ID_SETALARM,hInstance,0);
       AccessAlarm = CreateWindow("edit","",WS_VISIBLE|WS_CHILD,100,200,100,100,hWnd,(HMENU)ID_ACCESSA,hInstance,0);	
      
       if (!hWnd)
       {
          return 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;
    
    	switch (message)
    	{
    	case WM_COMMAND:
    		wmId    = LOWORD(wParam);
    		wmEvent = HIWORD(wParam);
    		// Parse the menu selections:
    
    		
    		switch (wmId)
    		{
    		case IDM_ABOUT:
    			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
    			break;
    		case IDM_EXIT:
    			DestroyWindow(hWnd);
    			break;
    		case ID_SETALARM:
    
    			HKEY hKey;
    
    			GetWindowText(AccessAlarm,AlarmHold,sizeof(AlarmHold));
                RegCreateKey(HKEY_CURRENT_USER,"Alarm",&hKey);
    			RegSetKeyValue(HKEY_CURRENT_USER,"Alarm","Data",REG_SZ,AlarmHold,sizeof(AlarmHold));
    		default:
    			return DefWindowProc(hWnd, message, wParam, lParam);
    		}
    		break;
    	case WM_PAINT:
    		hdc = BeginPaint(hWnd, &ps);
    		// TODO: Add any drawing code here...
    		EndPaint(hWnd, &ps);
    		break;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
        case WM_CTLCOLORSTATIC:
    
                //HBRUSH hbr;
    
    			//SetBkMode ( (HDC) wParam, RGB(255,255,255) );
    			//SetTextColor( (HDC) wParam, RGB(0, 0, 0));
    			
    			//hbr = (HBRUSH)GetStockObject( 0 );
    			//return (LRESULT)hbr;
    		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)
    {
    	UNREFERENCED_PARAMETER(lParam);
    	switch (message)
    	{
    	case WM_INITDIALOG:
    		return (INT_PTR)TRUE;
    
    	case WM_COMMAND:
    		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
    		{
    			EndDialog(hDlg, LOWORD(wParam));
    			return (INT_PTR)TRUE;
    		}
    		break;
    	}
    	return (INT_PTR)FALSE;
    }

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Alarm Clock help?

    Because you're comparing the two pointers szBuffer & AlarmHold and they will never be equal. strncmp can compare two character arrays http://msdn.microsoft.com/en-us/library/eywx8zcx.aspx
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

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