プログラミングのメモ帳(C/C++/HSP)

日々のプログラミングで気づいた点や小技集を紹介します。(Windows 10/XP/Vista、VC2017、HSP)

ドラッグ図形の描き方

2011年05月29日 09時18分00秒 | グラフィック関連

マウス操作で図形をドラッグして描画する方法を紹介します。

基本

  1. WM_LBUTTONDOWNはドラッグを開始してマウス・キャプチャも開始。
  2. WM_MOUSEMOVEはドラッグ移動中で図形の輪郭線を描画。
  3. WM_LBUTTONUPはドラッグを終了してマウス・キャプチャも解放。
  4. WM_PAINTはドラッグ開始、ドラッグ終了、ドラッグ停止の描画。

サンプル

//==============================================================================
// ドラッグ図形の描き方
//==============================================================================
#include <tchar.h>
#include <Windows.h>
#include <WindowsX.h>

//------------------------------------------------
// break付きのキーワード
//------------------------------------------------
#define CASE        break;case
#define DEFAULT     break;default

//------------------------------------------------
// 列挙定数(キャプチャ動作)
//------------------------------------------------
enum eCAPTURE {
    CAPTURE_NULL,               // キャプチャなし
    CAPTURE_START,              // キャプチャ開始
    CAPTURE_STOP,               // キャプチャ終了
};

//------------------------------------------------
// ウインドウ・プロシージャの関数
//------------------------------------------------
extern LRESULT CALLBACK mainWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
    static POINT    po[ 2 ];
    static UINT     nCapture;
    
    switch ( uMsg ){
        CASE WM_CREATE:
            
        CASE WM_CLOSE:
            DestroyWindow( hWnd );
        CASE WM_DESTROY:
            PostQuitMessage( 0 );
        CASE WM_PAINT:
        {
            PAINTSTRUCT     ps;
            HDC             hDC;
            
            hDC = BeginPaint( hWnd, &ps );
            SelectObject( hDC, GetStockObject(DC_PEN) );
            SelectObject( hDC, GetStockObject(DC_BRUSH) );
            
            switch ( nCapture ){
                CASE CAPTURE_START:
                    SetDCPenColor(   hDC, RGB(0xFF,0x00,0x00) );
                    SetDCBrushColor( hDC, RGB(0xFF,0x00,0x00) );
                    Ellipse( hDC, (po[0].x - 2), (po[0].y - 2), (po[0].x + 3), (po[0].y + 3) );
                    Ellipse( hDC, (po[1].x - 2), (po[1].y - 2), (po[1].x + 3), (po[1].y + 3) );
                    SelectObject( hDC, GetStockObject(NULL_BRUSH) );
                    Rectangle( hDC, po[0].x, po[0].y, po[1].x, po[1].y );
                    SelectObject( hDC, GetStockObject(DC_BRUSH) );
                CASE CAPTURE_STOP:
                    SetDCPenColor(   hDC, RGB(0x99,0x66,0x00) );
                    SetDCBrushColor( hDC, RGB(0xFF,0x99,0x00) );
                    Rectangle( hDC, po[0].x, po[0].y, po[1].x, po[1].y );
                    nCapture = CAPTURE_NULL;
                DEFAULT:break;
            }
            EndPaint( hWnd, &ps );
        }
        CASE WM_LBUTTONDOWN:
            if ( nCapture == CAPTURE_NULL ){
                nCapture = CAPTURE_START;
                po[ 0 ].x = po[ 1 ].x = GET_X_LPARAM( lParam );
                po[ 0 ].y = po[ 1 ].y = GET_Y_LPARAM( lParam );
                InvalidateRect( hWnd, NULL, TRUE );
                UpdateWindow( hWnd );
                SetCapture( hWnd );
            }
        CASE WM_MOUSEMOVE:
            if ( nCapture == CAPTURE_START ){
                po[ 1 ].x = GET_X_LPARAM( lParam );
                po[ 1 ].y = GET_Y_LPARAM( lParam );
                InvalidateRect( hWnd, NULL, TRUE );
                UpdateWindow( hWnd );
            }
        CASE WM_LBUTTONUP:
            if ( nCapture == CAPTURE_START ){
                nCapture = CAPTURE_STOP;
                po[ 1 ].x = GET_X_LPARAM( lParam );
                po[ 1 ].y = GET_Y_LPARAM( lParam );
                InvalidateRect( hWnd, NULL, TRUE );
                UpdateWindow( hWnd );
                ReleaseCapture();
            }
        DEFAULT:return DefWindowProc( hWnd, uMsg, wParam, lParam );
    }
    return 0;
}

ドラッグの開始

CASE WM_LBUTTONDOWN:
    if ( nCapture == CAPTURE_NULL ){
        nCapture = CAPTURE_START;
        po[ 0 ].x = po[ 1 ].x = GET_X_LPARAM( lParam );
        po[ 0 ].y = po[ 1 ].y = GET_Y_LPARAM( lParam );
        InvalidateRect( hWnd, NULL, TRUE );
        UpdateWindow( hWnd );
        SetCapture( hWnd );
    }
CASE WM_PAINT:
{
    PAINTSTRUCT     ps;
    HDC             hDC;
    
    hDC = BeginPaint( hWnd, &ps );
    SelectObject( hDC, GetStockObject(DC_PEN) );
    SelectObject( hDC, GetStockObject(DC_BRUSH) );
    
    switch ( nCapture ){
        CASE CAPTURE_START:
            SetDCPenColor(   hDC, RGB(0xFF,0x00,0x00) );
            SetDCBrushColor( hDC, RGB(0xFF,0x00,0x00) );
            Ellipse( hDC, (po[0].x - 2), (po[0].y - 2), (po[0].x + 3), (po[0].y + 3) );
            Ellipse( hDC, (po[1].x - 2), (po[1].y - 2), (po[1].x + 3), (po[1].y + 3) );
            SelectObject( hDC, GetStockObject(NULL_BRUSH) );
            Rectangle( hDC, po[0].x, po[0].y, po[1].x, po[1].y );
            SelectObject( hDC, GetStockObject(DC_BRUSH) );
        CASE CAPTURE_STOP:
              :
            (省略)
              :
        DEFAULT:break;
    }
    EndPaint( hWnd, &ps );
}

ドラッグの移動中

CASE WM_MOUSEMOVE:
    if ( nCapture == CAPTURE_START ){
        po[ 1 ].x = GET_X_LPARAM( lParam );
        po[ 1 ].y = GET_Y_LPARAM( lParam );
        InvalidateRect( hWnd, NULL, TRUE );
        UpdateWindow( hWnd );
    }
CASE WM_PAINT:
{
    PAINTSTRUCT     ps;
    HDC             hDC;
    
    hDC = BeginPaint( hWnd, &ps );
    SelectObject( hDC, GetStockObject(DC_PEN) );
    SelectObject( hDC, GetStockObject(DC_BRUSH) );
    
    switch ( nCapture ){
        CASE CAPTURE_START:
            SetDCPenColor(   hDC, RGB(0xFF,0x00,0x00) );
            SetDCBrushColor( hDC, RGB(0xFF,0x00,0x00) );
            Ellipse( hDC, (po[0].x - 2), (po[0].y - 2), (po[0].x + 3), (po[0].y + 3) );
            Ellipse( hDC, (po[1].x - 2), (po[1].y - 2), (po[1].x + 3), (po[1].y + 3) );
            SelectObject( hDC, GetStockObject(NULL_BRUSH) );
            Rectangle( hDC, po[0].x, po[0].y, po[1].x, po[1].y );
            SelectObject( hDC, GetStockObject(DC_BRUSH) );
        CASE CAPTURE_STOP:
              :
            (省略)
              :
        DEFAULT:break;
    }
    EndPaint( hWnd, &ps );
}

ドラッグの終了

CASE WM_LBUTTONUP:
    if ( nCapture == CAPTURE_START ){
        nCapture = CAPTURE_STOP;
        po[ 1 ].x = GET_X_LPARAM( lParam );
        po[ 1 ].y = GET_Y_LPARAM( lParam );
        InvalidateRect( hWnd, NULL, TRUE );
        UpdateWindow( hWnd );
        ReleaseCapture();
    }
CASE WM_PAINT:
{
    PAINTSTRUCT     ps;
    HDC             hDC;
    
    hDC = BeginPaint( hWnd, &ps );
    SelectObject( hDC, GetStockObject(DC_PEN) );
    SelectObject( hDC, GetStockObject(DC_BRUSH) );
    
    switch ( nCapture ){
        CASE CAPTURE_START:
              :
            (省略)
              :
        CASE CAPTURE_STOP:
            SetDCPenColor(   hDC, RGB(0x99,0x66,0x00) );
            SetDCBrushColor( hDC, RGB(0xFF,0x99,0x00) );
            Rectangle( hDC, po[0].x, po[0].y, po[1].x, po[1].y );
            nCapture = CAPTURE_NULL;
        DEFAULT:break;
    }
    EndPaint( hWnd, &ps );
}

コメント    この記事についてブログを書く
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする
« ドラッグ図形の描き方 | トップ | PlgBltで左右反転 »
最新の画像もっと見る

コメントを投稿

グラフィック関連」カテゴリの最新記事