Skip to content

Instantly share code, notes, and snippets.

@SimpleDrunk
Created December 26, 2013 08:44
Show Gist options
  • Save SimpleDrunk/8131344 to your computer and use it in GitHub Desktop.
Save SimpleDrunk/8131344 to your computer and use it in GitHub Desktop.
Flow Font, transparent window.悬浮文字,透明窗口
#include "Windows.h"
#define ID_TIMER_MOVE 1
int i, j;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow)
{
WNDCLASS wndclass;
MSG msg;
HWND hwnd;
static TCHAR szName[] = TEXT("AlphaTest");
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = CreateSolidBrush(RGB(19, 32, 23));
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WndProc;
wndclass.lpszClassName = szName;
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"),
szName, MB_ICONERROR);
return 0;
}
int cx = GetSystemMetrics(SM_CXSCREEN);
int cy = GetSystemMetrics(SM_CYSCREEN);
i = cx/2 - 100;
j = cy/2 - 100;
hwnd = CreateWindow(szName,
TEXT("Demo"),
WS_POPUPWINDOW,
i,
j,
200,
200,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, TRUE);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HRGN hRgn;
static HBITMAP hBmp;
BITMAP bmp;
static HDC hdc, hdcMem;
static HRGN hRgnWnd = CreateRectRgn(0, 0, 800, 600);
static HFONT hFont = CreateFont(50, 20, 0, 0, FW_NORMAL, FALSE, FALSE, 0, ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH | FF_SWISS,
L"myFont");
static HFONT hOldFont;
static int nOldMode;
static int cxBmp, cyBmp;
PAINTSTRUCT ps;
switch(message)
{
case WM_CREATE:
SetTimer(hwnd, ID_TIMER_MOVE, 20, NULL);
hBmp = LoadBitmap(((LPCREATESTRUCT)lParam)->hInstance, MAKEINTRESOURCE(101));
GetObject(hBmp, sizeof(BITMAP), &bmp);
cxBmp = bmp.bmWidth;
cyBmp = bmp.bmHeight;
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
BeginPath(hdc);
nOldMode = SetBkMode(hdc, TRANSPARENT);
hOldFont = (HFONT)SelectObject(hdc, hFont);
TextOut(hdc, 0, 0, TEXT("圣诞快乐!"), wcslen(L"圣诞快乐!"));
SelectObject(hdc, hOldFont);
EndPath(hdc);
hRgn = PathToRegion(hdc);
SetWindowRgn(hwnd, hRgn, TRUE);
SetBkMode(hdc, nOldMode);
EndPaint(hwnd, &ps);
return 0;
case WM_TIMER:
{
int xScreen = GetSystemMetrics(SM_CXSCREEN);
RECT rcWnd;
GetWindowRect(hwnd, &rcWnd);
i -= 2;
if(i < rcWnd.left - rcWnd.right)
i = xScreen;
MoveWindow(hwnd, i, j, rcWnd.right - rcWnd.left, rcWnd.bottom - rcWnd.top, TRUE);
}
return 0;
case WM_DESTROY:
KillTimer(hwnd, ID_TIMER_MOVE);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
@SimpleDrunk
Copy link
Author

//浮动的时钟

include <Windows.h>

include <time.h>

define ID_TIMER_CLOCK (WM_USER + 1)

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR szCmdLine,
int iCmdShow)
{
Sleep(10000);
WNDCLASS wndclass;
MSG msg;
HWND hwnd;
static TCHAR szAppName[] = TEXT("NewYearClock");

wndclass.cbClsExtra             = 0;
wndclass.cbWndExtra             = 0;
wndclass.hbrBackground          = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.hCursor                = LoadCursor(NULL, IDC_ARROW);
wndclass.hIcon                  = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hInstance              = hInstance;
wndclass.lpfnWndProc            = WndProc;
wndclass.lpszClassName          = szAppName;
wndclass.lpszMenuName           = NULL;
wndclass.style                  = CS_HREDRAW | CS_VREDRAW;

if(!RegisterClass(&wndclass))
{
    MessageBox(NULL, TEXT("This program requires Windows NT!"),
        szAppName, MB_ICONERROR);
    return 0;
}

int cx = GetSystemMetrics(SM_CXSCREEN);
int cy = GetSystemMetrics(SM_CYSCREEN);

hwnd = CreateWindow(szAppName,
    TEXT("NewYearClock"),
    WS_POPUPWINDOW,
    cx -620,
    cy - 400,
    200,
    100,
    NULL,
    NULL,
    hInstance,
    NULL);

ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);

while(GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return msg.wParam;

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HRGN hRgn = CreateRectRgn(0, 0, 200, 100);;
static HDC hdc;
static int nOldMode;
static HFONT hFont = CreateFont(25, 10, 0, 0, FW_NORMAL, FALSE, FALSE, 0, ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH | FF_SWISS,
L"myFont");
static HFONT hOldFont;
PAINTSTRUCT ps;

switch(message)
{

case WM_CREATE:
    SetTimer(hwnd, ID_TIMER_CLOCK, 10, NULL);
    SelectObject(hdc, hFont);
    ::SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
        SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);  //窗口置顶
    return 0;
case WM_PAINT:
    {
        hdc = BeginPaint(hwnd, &ps);
        BeginPath(hdc);
        nOldMode = SetBkMode(hdc, TRANSPARENT);
        hOldFont = (HFONT)SelectObject(hdc, hFont);
        struct tm *newtime;
        char tmpbuf[128];
        time_t lt;
        time(&lt);
        newtime = localtime(&lt);
        strftime(tmpbuf, 128, "%H:%M:%S", newtime);
        TextOutA(hdc, 2, 0, tmpbuf, strlen(tmpbuf));
        SelectObject(hdc, hOldFont);
        EndPath(hdc);
        hRgn = PathToRegion(hdc);
        SetWindowRgn(hwnd, hRgn, TRUE);
        SetBkMode(hdc, nOldMode);
        EndPaint(hwnd, &ps);
    }
    return 0;

case WM_TIMER:
    InvalidateRect(hwnd, NULL, TRUE);
    return 0;

case WM_DESTROY:
    KillTimer(hwnd, ID_TIMER_CLOCK);
    PostQuitMessage(0);
    return 0;
}

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

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment