▼Windows Mobileプログラミングメモ▼
グラフィックスの描画

グラフィックスの描画を行うプログラムを作成する。



ソースコードの作成
  1. ソースコードを以下のように編集。
    main.cpp
    #include <windows.h>
    
    //関数の宣言
    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
    ATOM InitApp(HINSTANCE);
    BOOL InitInstance(HINSTANCE,int,LPTSTR);
    void Create();
    void Paint();
    void Destroy();
    void SetColor(int,int,int);
    void DrawLine(int,int,int,int);
    void DrawRect(int,int,int,int);
    void FillRect(int,int,int,int);
    void DrawCircle(int,int,int,int);
    void FillCircle(int,int,int,int);
    
    //変数の宣言
    HDC     hdc;   //デバイスコンテキスト
    HGDIOBJ hPen;  //ペン
    HGDIOBJ hBrush;//ブラシ
    
    //メイン
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
        LPWSTR lpCmdLine,int nShowCmd) {
        MSG msg;
        BOOL bRet;
    
        //ウィンドウの生成
        if (!InitApp(hInstance)) return FALSE;
        if (!InitInstance(hInstance,nShowCmd,
            _T("GraphicsEx"))) return FALSE;
    
        //メインループ
        while((bRet=GetMessage(&msg,NULL,0,0))!=0) {
            if (bRet==-1) {
                break;
            } else {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
        return (int)msg.wParam;
    }
    
    //ウィンドウクラスの登録
    ATOM InitApp(HINSTANCE hInst) {
        WNDCLASSW wc;
        wc.style=CS_HREDRAW|CS_VREDRAW;
        wc.lpfnWndProc=WndProc;
        wc.cbClsExtra=0;
        wc.cbWndExtra=0;
        wc.hInstance=hInst;
        wc.hIcon=NULL;
        wc.hCursor=NULL;
        wc.hbrBackground=(HBRUSH)COLOR_WINDOW;
        wc.lpszMenuName=NULL;
        wc.lpszClassName=_T("win01");
        return (RegisterClassW(&wc));
    }
    
    //ウィンドウの生成
    BOOL InitInstance(HINSTANCE hInst,int nShowCmd,
        LPTSTR title) {
        HWND hWnd;
        hWnd=CreateWindowW(_T("win01"),title,
            WS_CLIPCHILDREN,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            NULL,
            NULL,
            hInst,
            NULL);
        if (!hWnd) return FALSE;
        ShowWindow(hWnd,nShowCmd);
        UpdateWindow(hWnd);
        return TRUE;
    }
    
    //ウィンドウのイベント処理
    LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp) {
        PAINTSTRUCT ps;
        HGDIOBJ hPenOld;
        HGDIOBJ hBrushOld;
        
        switch (msg){
        //描画
        case WM_PAINT:
            //前処理
            hdc=BeginPaint(hWnd,&ps);
            hPen=CreatePen(PS_SOLID,1,RGB(0,0,0));
            hPenOld=SelectObject(hdc,hPen);
            hBrush=CreateSolidBrush(RGB(0,0,0));
            hBrushOld=SelectObject(hdc,hBrush);
    
            //描画
            Paint();
    
            //後処理
            SelectObject(hdc,hPenOld);
            SelectObject(hdc,hBrushOld);
            DeleteObject(hPen);
            DeleteObject(hBrush);
            EndPaint(hWnd,&ps);
            break;
        //破棄
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        //その他
        default:
            return (DefWindowProc(hWnd,msg,wp,lp));
        }
        return 0;
    }
    
    //描画
    void Paint() {
        //ラインの描画
        SetColor(255,0,0);
        DrawLine(25,5,25,5+40);
    
        //パスの描画
        SetColor(255,0,0);
        MoveToEx(hdc,55+0,5+0,NULL);
        LineTo(hdc,55+30,5+ 5);
        LineTo(hdc,55+10,5+20);
        LineTo(hdc,55+40,5+25);
        LineTo(hdc,55+ 0,5+40);
    
        //矩形の描画
        SetColor(0,255,0);
        DrawRect(5,55,40,40);
    
        //矩形の塗り潰し
        SetColor(0,255,0);
        FillRect(55,55,40,40);
    
        //円の描画
        SetColor(255,255,0);
        DrawCircle(5,100,40,40);
    
        //円の塗り潰し
        SetColor(255,255,0);
        FillCircle(55,100,40,40);
    }
    
    //色の指定
    void SetColor(int r,int g,int b) {
        //ペンとブラシの解放
        DeleteObject(hPen);
        DeleteObject(hBrush);
    
        //ペンとブラシの生成
        hPen=CreatePen(PS_SOLID,1,RGB(r,g,b));
        SelectObject(hdc,hPen);
        hBrush=CreateSolidBrush(RGB(r,g,b));
        SelectObject(hdc,hBrush);
    }
    
    //ラインの描画
    void DrawLine(int x0,int y0,int x1,int y1) {
        MoveToEx(hdc,x0,y0,NULL);
        LineTo(hdc,x1,y1);
    }
    
    //矩形の描画
    void DrawRect(int x,int y,int w,int h) {
        SelectObject(hdc,GetStockObject(NULL_BRUSH));
        Rectangle(hdc,x,y,x+w,y+h);
        SelectObject(hdc,hBrush);
    }
    
    //矩形の塗り潰し
    void FillRect(int x,int y,int w,int h) {
        Rectangle(hdc,x,y,x+w,y+h);
    }
    
    //円の描画
    void DrawCircle(int x,int y,int w,int h) {
        SelectObject(hdc,GetStockObject(NULL_BRUSH));
        Ellipse(hdc,x,y,x+w,y+h);
        SelectObject(hdc,hBrush);
    }
    
    //円の塗り潰し
    void FillCircle(int x,int y,int w,int h) {
        Ellipse(hdc,x,y,x+w,y+h);
    }



−戻る−