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

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


ダイアログの準備
  1. ソリューションエクスプローラのリソースファイルを右クリックし、ポップアップ「追加→リソース」を選択。
  2. Dialogをダブルクリックし、ダイアログ(IDD_DIALOG1)を生成。
  3. OKボタン(IDOK)とキャンセルボタン(IDCANCEL)を削除。
  4. ツールボックスの「Picture Control」をダイアログに配置。
  5. Picture ControlのIDを"IDC_STATIC1"に変更。
ソースコードの作成
  1. ソースコードを以下のように編集。
    main.cpp
    #include <windows.h>
    #include <windowsx.h>
    #include "resource.h"
    #include <aygshell.h> 
    #pragma comment(lib,"aygshell.lib")
    
    //システム関数の宣言
    BOOL DlgProc(HWND,UINT,WPARAM,LPARAM);
    BOOL GetWorkArea(int*,int*,int*,int*);
    void Create();
    void Paint();
    void Destroy();
    
    //描画関数の宣言
    HDC G_CreateDC();
    HBITMAP G_CreateBmp(HDC,int,int);
    HFONT G_CreateFont(LPTSTR,int,int);
    void G_SetColor(int,int,int);
    void G_DrawString(LPTSTR,int,int);
    void G_DrawLine(int,int,int,int);
    void G_DrawPolyline(int*,int*,int);
    void G_DrawRect(int,int,int,int);
    void G_FillRect(int,int,int,int);
    void G_DrawCircle(int,int,int,int);
    void G_FillCircle(int,int,int,int);
    
    //システム変数の宣言
    HWND hDlg   =NULL;
    int  screenW=0;
    int  screenH=0;
    
    //描画変数の宣言
    HDC     hdcOff=NULL;
    HBITMAP hOff  =NULL;
    HGDIOBJ hPen  =NULL;
    HGDIOBJ hBrush=NULL;
    
    //メイン
    int WinMain(HINSTANCE hDlgance,HINSTANCE hPrevInstance,
        LPWSTR lpCmdLine,int nShowCmd) {
        //ダイアログの生成
        DialogBoxW(hDlgance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,DlgProc);
        return 0;
    }
    
    //ダイアログのイベント処理
    BOOL DlgProc(HWND hDialog,UINT uMsg,WPARAM wp,LPARAM lp) {
        switch(uMsg) {
        //初期化
        case WM_INITDIALOG:{
            //ダイアログの設定
            SHINITDLGINFO sidi={
                SHIDIM_FLAGS,hDialog,
                SHIDIF_DONEBUTTON|SHIDIF_SIZEDLGFULLSCREEN|SHIDIF_EMPTYMENU};
            SHInitDialog(&sidi);
    
            //生成
            hDlg=hDialog;
            Create();
            
            //描画処理
            Paint();
            }
            break;
        //コマンド
        case WM_COMMAND:
            switch(LOWORD(wp)) {
            //OKボタン
            case IDOK:
                EndDialog(hDialog,LOWORD(wp));
                break;
            }
            break;
        //クローズ
        case WM_CLOSE:
            //解放
            Destroy();
            DestroyWindow(hDialog);
            break;
        }
        return FALSE;
    }
    
    //ワークエリアの計算
    BOOL GetWorkArea(int *x,int *y,int *w,int *h) {
        SIPINFO si={sizeof(SIPINFO)};
        if (!SipGetInfo(&si)) return FALSE;
        *x=si.rcVisibleDesktop.left;
        *y=si.rcVisibleDesktop.top;
        *w=si.rcVisibleDesktop.right-*x;
        if (si.fdwFlags&SIPF_ON) {
            *h=si.rcSipRect.top-*y;
        } else {
            *h=si.rcSipRect.bottom-*y;
        }
        return TRUE;
    }
    
    //生成
    void Create() {
        //ワークエリアの計算
        int x,y;
        GetWorkArea(&x,&y,&screenW,&screenH);
    
        //ピクチャの配置
        MoveWindow(GetDlgItem(hDlg,IDC_STATIC1),
            0,0,screenW,screenH,FALSE);
    
        //オフスクリーンの生成
        hdcOff=G_CreateDC();
        hOff=G_CreateBmp(hdcOff,screenW,screenH);
    }
    
    //描画
    void Paint() {
        //前処理
        HBITMAP hOffOld=(HBITMAP)SelectObject(hdcOff,hOff);
    
        //背景の描画
        G_SetColor(255,255,255);
        G_FillRect(0,0,screenW,screenH);
    
        //ラインの描画
        G_SetColor(255,0,0);
        G_DrawLine(25,5,25,5+40);
    
        //パスの描画
        int px[5]={55,85,65,95,55};
        int py[5]={ 5,10,25,30,45};
        G_SetColor(255,0,0);
        G_DrawPolyline(px,py,5);
    
        //矩形の描画
        G_SetColor(0,255,0);
        G_DrawRect(5,55,40,40);
    
        //矩形の塗り潰し
        G_SetColor(0,255,0);
        G_FillRect(55,55,40,40);
    
        //円の描画
        G_SetColor(255,255,0);
        G_DrawCircle(5,100,40,40);
    
        //円の塗り潰し
        G_SetColor(255,255,0);
        G_FillCircle(55,100,40,40);
    
        //後処理
        SelectObject(hdcOff,hOffOld);
    
        //ピクチャの画像更新
        SendMessage(GetDlgItem(hDlg,IDC_STATIC1),
            STM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hOff);
    }
    
    //解放
    void Destroy() {
        DeleteDC(hdcOff);
        DeleteObject(hOff);
        DeleteObject(hPen);
        DeleteObject(hBrush);
    }
    
    //デバイスコンテキストの生成
    HDC G_CreateDC() {
        return CreateCompatibleDC(NULL);
    }
    
    //ビットマップの生成
    HBITMAP G_CreateBmp(HDC hdc,int w,int h) {
        BITMAPINFO bmi={0};
        void *pv;
        bmi.bmiHeader.biSize       =sizeof(BITMAPINFOHEADER);
        bmi.bmiHeader.biWidth      =w;
        bmi.bmiHeader.biHeight     =h;
        bmi.bmiHeader.biPlanes     =1;
        bmi.bmiHeader.biBitCount   =(SHORT)max(16,GetDeviceCaps(hdc,BITSPIXEL));
        bmi.bmiHeader.biCompression=BI_RGB;
        return CreateDIBSection(hdc,&bmi,DIB_RGB_COLORS,&pv,NULL,0);
    }
    
    //色の指定
    void G_SetColor(int r,int g,int b) {
        //テキスト色の指定
        SetBkMode(hdcOff,TRANSPARENT);
        SetTextColor(hdcOff,RGB(r,g,b));
    
        //ペン色とブラシ色の指定
        DeleteObject(hPen);
        DeleteObject(hBrush);
        hPen=CreatePen(PS_SOLID,1,RGB(r,g,b));
        SelectObject(hdcOff,hPen);
        hBrush=CreateSolidBrush(RGB(r,g,b));
        SelectObject(hdcOff,hBrush);
    }
    
    //ラインの描画
    void G_DrawLine(int x0,int y0,int x1,int y1) {
        MoveToEx(hdcOff,x0,y0,NULL);
        LineTo(hdcOff,x1,y1);
    }
    
    //ポリラインの描画
    void G_DrawPolyline(int* px,int* py,int len) {
        if (len<=0) return;
        MoveToEx(hdcOff,px[0],py[0],NULL);
        for (int i=1;i<len;i++) LineTo(hdcOff,px[i],py[i]);
    }
    
    //矩形の描画
    void G_DrawRect(int x,int y,int w,int h) {
        SelectObject(hdcOff,GetStockObject(NULL_BRUSH));
        Rectangle(hdcOff,x,y,x+w,y+h);
        SelectObject(hdcOff,hBrush);
    }
    
    //矩形の塗り潰し
    void G_FillRect(int x,int y,int w,int h) {
        Rectangle(hdcOff,x,y,x+w,y+h);
    }
    
    //円の描画
    void G_DrawCircle(int x,int y,int w,int h) {
        SelectObject(hdcOff,GetStockObject(NULL_BRUSH));
        Ellipse(hdcOff,x,y,x+w,y+h);
        SelectObject(hdcOff,hBrush);
    }
    
    //円の塗り潰し
    void G_FillCircle(int x,int y,int w,int h) {
        Ellipse(hdcOff,x,y,x+w,y+h);
    }



−戻る−