ssCAROのブログ

色んなとこで見つけたプログラムのメモ置き場っぽい

スクリーンセーバーの作成

//スクリーンセーバーの作成
//文字の表示位置を変えるだけのスクリーンセーバー

#include <windows.h>
#include <scrnsave.h>

#pragma comment(lib, "scrnsave.lib")

HFONT hScrnFont;

HFONT DrawFont(LPSTR pszName, LONG lSize)
{
    LOGFONT lf;

    lf.lfHeight = lSize;
    lf.lfWidth = 0;
    lf.lfEscapement = 0;
    lf.lfOrientation = 0;
    lf.lfWeight = FW_NORMAL;
    lf.lfItalic = FALSE;
    lf.lfUnderline = FALSE;
    lf.lfStrikeOut = FALSE;
    lf.lfCharSet = SHIFTJIS_CHARSET;
    lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
    lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
    lf.lfQuality = PROOF_QUALITY;
    lf.lfPitchAndFamily = FIXED_PITCH | FF_MODERN;
    lstrcpy(lf.lfFaceName, pszName);

    return CreateFontIndirect(&lf);
}

void OnPaint(HWND hWnd, HDC hDC)
{
    HFONT hFont;
    RECT rc;

    hFont = (HFONT)SelectObject(hDC, hScrnFont);

    SetBkMode(hDC, TRANSPARENT);
    SetTextColor(hDC, RGB(nRed, nGreen, nBlue));

    GetClientRect(hWnd, &rc);

    rc.top = rand() % rc.right;
    rc.left = rand() % rc.bottom;

    DrawText(hDC, "テストプログラム", -1, &rc, DT_SINGLELINE | DT_EXPANDTABS);

    SelectObject(hDC, hFont);
}

LRESULT WINAPI ScreenSaverProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_CREATE:
        hScrnFont = DrawFont("MS ゴシック", 48);
        SetTimer(hWnd, 1, 10000, NULL);
        break;

    case WM_TIMER:
        InvalidateRect(hWnd, NULL, TRUE);
        break;

    case WM_PAINT:
        {
        HDC hDC;
        PAINTSTRUCT ps;

        hDC = BeginPaint(hWnd, &ps);
        OnPaint(hWnd, hDC);
        EndPaint(hWnd, &ps);
        break;
        }

    case WM_DESTROY:
        KillTimer(hWnd, 1);
        DeleteObject(hScrnFont);
        break;

    default:
        break;
    }

    return DefScreenSaverProc(hWnd, uMsg, wParam, lParam);
}

BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
    return TRUE;
}

BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
{
    return TRUE;
}