「セットした時間になると警告音が鳴ったりダイアログボックスで知らせてくれるデジタル時計」


ソース

#include <windows.h>
#include <commctrl.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "rensyu9.h"

#define NUMBUTTONS 4

void tool(void);

LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK DialogFunc(HWND, UINT, WPARAM, LPARAM);

HINSTANCE hInst;

char szWinName[]="MyWin";

TBBUTTON tbButtons[NUMBUTTONS];
HWND tbwnd;

char c[255]="My Clock-";
time_t jikan;
struct tm *lt;
int ji, fun;

int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
				   LPSTR lpszAreg, int nWinMode)
{
	HWND hwnd;
    MSG msg;
    WNDCLASSEX wcl;


    /*ウィンドウクラスの定義*/

    wcl.hInstance=hThisInst;
    wcl.lpszClassName=szWinName;
    wcl.lpfnWndProc=WindowFunc;
    wcl.style=0;
    wcl.cbSize=sizeof(WNDCLASSEX);
    wcl.hIcon=LoadIcon(NULL, IDI_APPLICATION);
    wcl.hIconSm=LoadIcon(NULL, IDI_ASTERISK);
    wcl.hCursor=LoadCursor(NULL, IDC_ARROW);
    wcl.lpszMenuName="MYMENU";
    wcl.cbClsExtra=0;
    wcl.cbWndExtra=0;

    /*ウィンドウの背景*/
    wcl.hbrBackground=(HBRUSH) GetStockObject(BLACK_BRUSH);
    /*ウィンドウクラスの登録*/
    if(!RegisterClassEx(&wcl)) return(0);

    /*ウィンドウの生成*/
    hwnd = CreateWindow(
		szWinName,
        "簡易時計",
        WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX,
        400,
        0,
        220,
        100,
        HWND_DESKTOP,
        NULL,
        hThisInst,
        NULL
    );

    hInst=hThisInst;
    tool();
    InitCommonControls();
    tbwnd = CreateToolbarEx(hwnd, WS_VISIBLE | WS_CHILD | WS_BORDER | TBSTYLE_TOOLTIPS,
    						IDM_TOOLBAR,
                            NUMBUTTONS,
                            hThisInst,
                            IDB_BITMAP1,
                            tbButtons,
                            NUMBUTTONS,
                            0,0,16,16,
                            sizeof(TBBUTTON));


    /*ウィンドウの表示*/

    ShowWindow(hwnd, nWinMode);
    UpdateWindow(hwnd);

    /*メッセージループ*/
    while(GetMessage(&msg,NULL,0,0)){
    	TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message,
							WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT paintstruct;
    int response;
    LPTOOLTIPTEXT TTtext;
    char settei[80];

    switch(message){
    	case WM_NOTIFY:
        	TTtext = (LPTOOLTIPTEXT) lParam;
            if(TTtext->hdr.code == TTN_NEEDTEXT)
            	switch(TTtext->hdr.idFrom){
                	case IDM_START: TTtext->lpszText = "時刻表示";
                    	break;
                    case IDM_DIA: TTtext->lpszText = "アラームセット";
                    	break;
                    case IDM_EXIT: TTtext->lpszText = "閉じる";
                        break;
                    }
            break;

    	case WM_COMMAND:
        	switch(LOWORD(wParam)){
            	case IDM_START:
                	SetTimer(hwnd, IDD_TIMER, 1000, NULL);
                    break;

                case IDM_EXIT:
                	response = MessageBox(hwnd, "終了しますか?", "Exit", MB_YESNO);
                    if(response == IDYES){
                    	KillTimer(hwnd, IDD_TIMER);
                    	PostQuitMessage(0);
                    }
                    break;

                case IDM_DIA:
                	DialogBox(hInst, "MYDB", hwnd, DialogFunc);
                    break;

                case IDM_SET:
                	sprintf(settei, "%d時%d分", ji, fun);
                	MessageBox(hwnd, settei, "アラーム設定時刻", MB_OK);
                    break;
            }
            break;

	case WM_PAINT:
            hdc=BeginPaint(hwnd, &paintstruct);
            SetTextColor(hdc, RGB(0,255,0));
            SetBkColor(hdc, RGB(0,0,0));
            TextOut(hdc, 0, 32, c, (strlen(c))-1);
            EndPaint(hwnd, &paintstruct);
            break;

        case WM_TIMER:
            hdc = GetDC(hwnd);
            time(&jikan);
            lt = localtime(&jikan);
            sprintf(c, "%s", asctime(lt));
            if((lt->tm_hour)==ji && (lt->tm_min)==fun && (lt->tm_sec)==0){
            	MessageBox(hwnd, "時間ですよ〜", "アラーム", MB_OK | MB_ICONEXCLAMATION);
            }
            SetTextColor(hdc, RGB(0,255,0));
            SetBkColor(hdc, RGB(0,0,0));
            TextOut(hdc, 0, 32, c, (strlen(c))-1);
            ReleaseDC(hwnd, hdc);
            break;

   	case WM_DESTROY:
            KillTimer(hwnd, IDD_TIMER);
            PostQuitMessage(0);
       	    break;

       	default:

   		return DefWindowProc(hwnd, message,wParam,lParam);
    }
    return 0;
}

BOOL CALLBACK DialogFunc(HWND hdwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	char str[80];

	switch(message){
    	case WM_COMMAND:
        	switch(LOWORD(wParam)){
            	case IDCANCEL:
                	EndDialog(hdwnd, 0);
                    return 1;

                case IDC_BUTTON1:
                	GetDlgItemText(hdwnd, IDC_EDIT2, str, 80);
                    fun = atoi(str);
                    GetDlgItemText(hdwnd, IDC_EDIT1, str, 80);
                    ji = atoi(str);
                    EndDialog(hdwnd, 0);
                    return 1;
            }
    }
    return 0;
}

void tool(void)
{
	tbButtons[0].iBitmap = 0;
    tbButtons[0].idCommand = IDM_START;
    tbButtons[0].fsState = TBSTATE_ENABLED;
    tbButtons[0].fsStyle = TBSTYLE_BUTTON;
    tbButtons[0].dwData = 0L;
    tbButtons[0].iString = 0;

    tbButtons[1].iBitmap = 2;
    tbButtons[1].idCommand = IDM_DIA;
    tbButtons[1].fsState = TBSTATE_ENABLED;
    tbButtons[1].fsStyle = TBSTYLE_BUTTON;
    tbButtons[1].dwData = 0L;
    tbButtons[1].iString = 0;

    tbButtons[2].iBitmap = 0;
    tbButtons[2].idCommand = 0;
    tbButtons[2].fsState = TBSTATE_ENABLED;
    tbButtons[2].fsStyle = TBSTYLE_SEP;
    tbButtons[2].dwData = 0L;
    tbButtons[2].iString = 0;

    tbButtons[3].iBitmap = 1;
    tbButtons[3].idCommand = IDM_EXIT;
    tbButtons[3].fsState = TBSTATE_ENABLED;
    tbButtons[3].fsStyle = TBSTYLE_BUTTON;
    tbButtons[3].dwData = 0L;
    tbButtons[3].iString = 0;
	return;
}

リソース

うわーん!!なくしてしまったー。 めんどくさいのでもう書かない。

ヘッダーrensyu9.h

#define IDM_START	55531
#define CM_VI1	19589
#define IDM_EXIT	55530
#define IDD_TIMER	100
#define IDC_EDIT1	101
#define IDC_EDIT2	102
#define IDC_BUTTON1	103
#define IDM_DIA		55532
#define IDM_SET	55533
#define IDM_TOOLBAR	55534
#define IDB_BITMAP1	1

トップページへ