GDI-trail

here is the code, I try to draw trail of points.
blue point is top.
red points are trails of blue points.
trails.png

#pragma once
#include <stdio.h>
#include <windows.h>
#include <assert.h>
#include <sstream>
#pragma comment(lib,"Dwmapi.lib")
using namespace std;
RECT m_WindowRect;
RECT m_ClientRect;
HDC m_MemoryHDC;
HDC m_DeviceHDC;
HWND m_hwnd;
const int TRAIL_NUM=10;
const int RAND_RANGE=10;
POINT m_Pts[TRAIL_NUM];
HPEN m_BluePen;
HPEN m_RedPen;
#pragma pack(2)//to copy POINTS struct
LRESULT  CALLBACK  WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
    ostringstream cdbg;
    switch( msg )
    {
    case WM_ERASEBKGND:
        return 1;
    case WM_CREATE:
        memset(m_Pts,0,sizeof(POINTS)*TRAIL_NUM);
        SYSTEMTIME st;
        GetLocalTime(&st);
        srand((unsigned)st.wMilliseconds);
        m_BluePen=CreatePen(PS_SOLID,4,RGB(0,0,255));
        m_RedPen=CreatePen(PS_SOLID,4,RGB(255,0,0));
        return 0;
    case WM_CHAR:       
        if( wp==VK_ESCAPE ){
            PostQuitMessage(0);    
        }
        return 0;
    case WM_LBUTTONDOWN:
        return 0;
    case WM_DESTROY:
        PostQuitMessage( 0 );
        return 0;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            //clear screen
            PatBlt(m_DeviceHDC,0,0,m_ClientRect.right,m_ClientRect.bottom,WHITENESS);
 
            m_Pts[0].x+=rand()%RAND_RANGE;
            m_Pts[0].y+=rand()%RAND_RANGE;
            SelectObject(m_DeviceHDC,m_RedPen);
            //draw trails
            for(int i=0;i<TRAIL_NUM;i++){
                MoveToEx(m_DeviceHDC,m_Pts[i].x,m_Pts[i].y,NULL);
                LineTo(m_DeviceHDC,m_Pts[i].x+1,m_Pts[i].y+1);
            }
 
            SelectObject(m_DeviceHDC,m_BluePen);
            MoveToEx(m_DeviceHDC,m_Pts[0].x,m_Pts[0].y,NULL);
            LineTo(m_DeviceHDC,m_Pts[0].x+1,m_Pts[0].y+1);
            cdbg<<"["<<0<<"]="<<m_Pts[0].x<<","<<m_Pts[0].y<<endl;
            POINTS past[TRAIL_NUM];
            for(int i=0;i<TRAIL_NUM;i++){
                past[i].x=m_Pts[i].x;
                past[i].y=m_Pts[i].y;
            }
            for(int i=1;i<TRAIL_NUM;i++){
                m_Pts[i].x=past[i-1].x;
                m_Pts[i].y=past[i-1].y;
                cdbg<<"["<<i<<"]="<<m_Pts[i].x<<","<<m_Pts[i].y<<endl;
            }
            cdbg<<endl;
            OutputDebugString(cdbg.str().c_str());
            Sleep(200);
            //EndPaint(hWnd,&ps);
            return 0;
        }
    case WM_SIZING:
        GetClientRect(hWnd, &m_ClientRect);
        return 0;
    }
    return DefWindowProc( hWnd, msg, wp, lp );
}
 
// ウィンドウを作成する
HWND WindowCreate(HINSTANCE hInst)
{
    int dispx = GetSystemMetrics(SM_CXSCREEN)>>1;
    int dispy = GetSystemMetrics(SM_CYSCREEN)>>1;
    m_WindowRect.left=dispx-(m_ClientRect.right>>1);
    m_WindowRect.top=dispy-(m_ClientRect.bottom>>1);
    m_WindowRect.right=dispx+(m_ClientRect.right>>1);
    m_WindowRect.bottom=dispy+(m_ClientRect.bottom>>1);
    WNDCLASSEX wc;
 
    // ウィンドウクラスの情報を設定
    wc.cbSize = sizeof(wc);               
    wc.style = CS_HREDRAW | CS_VREDRAW;   
    wc.lpfnWndProc = WndProc; 
    wc.cbClsExtra = 0;              
    wc.cbWndExtra = 0;                   
    wc.hInstance = hInst;                
    wc.hIcon = (HICON)LoadImage(        
        NULL, MAKEINTRESOURCE(IDI_APPLICATION), IMAGE_ICON,
        0, 0, LR_DEFAULTSIZE | LR_SHARED
    );
    wc.hIconSm = wc.hIcon;               
    wc.hCursor = (HCURSOR)LoadImage(     
        NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR,
        0, 0, LR_DEFAULTSIZE | LR_SHARED
        );
    wc.hbrBackground =CreateSolidBrush(RGB(0,0,0));
    wc.lpszMenuName = NULL;                     
    wc.lpszClassName = "CGLWindow";
    if( RegisterClassEx( &wc ) == 0 ){ return NULL; }
 
    return CreateWindow(
        //WS_EX_OVERLAPPEDWINDOW | WS_EX_TRANSPARENT,//WS_EX_LAYERED
        wc.lpszClassName,    
        "trail",
        WS_OVERLAPPEDWINDOW|WS_VISIBLE,
        m_WindowRect.left,         
        m_WindowRect.top,       
        m_ClientRect.right,    
        m_ClientRect.bottom,      
        NULL,              
        NULL,         
        hInst,     
        NULL   
        );
 
}
 
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, int showCmd)
{
    m_ClientRect.right=1024;
    m_ClientRect.bottom=768;
    m_hwnd = WindowCreate( hInst );
    GetClientRect(m_hwnd,&m_ClientRect);
    m_DeviceHDC=(HDC)GetDC(m_hwnd);
    m_MemoryHDC=CreateCompatibleDC(m_DeviceHDC);
    MSG msg;
    while(GetMessage( &msg, NULL, 0, 0 ))
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }
    return 0;
}
en.gif en
ja.gif ja


edit
サポートサイト Wikidot.com