2.1 文字列の表示 (1) 画像としての文字列 #include "myWin.h" void procLeftButtonDown(HWND hw, WPARAM wp,LPARAM lp){ HDC hdc=GetDC(hw);LPTSTR lptstr=TEXT("ようこそ Windowsへ"); TextOut(hdc, 10,10, lptstr, lstrlen(lptstr)); ReleaseDC(hw,hdc); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ; return 0; case WM_LBUTTONDOWN : procLeftButtonDown(hw,wp,lp); return 0; } return DefWindowProc(hw,msg,wp,lp); } (2) クライアントエリアの更新 #include "myWin.h" void procPaint(HWND hw, WPARAM wp,LPARAM lp){ HDC hdc=GetDC(hw);LPTSTR lptstr=TEXT("ようこそ Windowsへ"); TextOut(hdc, 10,10, lptstr, lstrlen(lptstr)); ReleaseDC(hw,hdc); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ; return 0; case WM_PAINT : procPaint(hw,wp,lp); return 0; } return DefWindowProc(hw,msg,wp,lp); } (3) 2行に分けて描画する 【procPaintのみ】 void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps;TEXTMETRIC tm; HDC hdc=BeginPaint(hw, &ps); GetTextMetrics(hdc,&tm); LPTSTR lptstr=TEXT("ようこそ Windowsへ"); int len=lstrlen(lptstr); TextOut(hdc, 10,10 , lptstr, len); TextOut(hdc, 10,10+tm.tmHeight, lptstr, len); ReleaseDC(hw,hdc); } (4) 文字色・背景色 【procPaintのみ】 void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps;TEXTMETRIC tm; HDC hdc=BeginPaint(hw, &ps); GetTextMetrics(hdc,&tm); LPTSTR lptstr=TEXT("ようこそ Windowsへ"); int len=lstrlen(lptstr); SetTextColor(hdc,0xFF0000); SetBkColor(hdc,RGB(0xCF,0xCF,0xCF)); TextOut(hdc, 10,10 , lptstr, len); SetTextColor(hdc,RGB(255,0,0)); SetBkMode(hdc,TRANSPARENT); TextOut(hdc, 10,10+tm.tmHeight, lptstr, len); EndPaint(hw,&ps); } (5) 描画領域のサイズと動的な改行 【procPaintのみ】 void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps;RECT rect; HDC hdc=BeginPaint(hw, &ps); GetClientRect(hw,&rect); LPTSTR lptstr=TEXT("the world, the flesh, and the devil"); DrawText(hdc, lptstr,-1,&rect,DT_CENTER | DT_WORDBREAK); EndPaint(hw,&ps); } (6) フォントの指定 【procPaintのみ】 void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps;LPTSTR lptstr = TEXT("She twisted flowers into a wreath."); HDC hdc=BeginPaint(hw, &ps); HFONT hF =CreateFont( 32, 0, 0, 0, FW_BOLD, TRUE, TRUE, FALSE, SHIFTJIS_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, VARIABLE_PITCH | FF_ROMAN,NULL); SelectObject(hdc, hF); TextOut(hdc,10,10,lptstr,lstrlen(lptstr)); EndPaint(hw,&ps); } 【回転】 HFONT hF=CreateFont( 32, 0,-350,-350, FW_BOLD, TRUE, TRUE, FALSE, SHIFTJIS_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, VARIABLE_PITCH | FF_ROMAN,NULL); SelectObject(hdc, hF); TextOut(hdc,100,10,lptstr,lstrlen(lptstr)); (7) LOGFONT構造体を使用する方法 #include "myWin.h" static LOGFONT lfF; void procCreate(HWND hw, WPARAM wp,LPARAM lp){ lfF.lfHeight = 32; lfF.lfWidth = lfF.lfEscapement = lfF.lfOrientation = 0; lfF.lfWeight = FW_BOLD; lfF.lfItalic = lfF.lfUnderline = TRUE; lfF.lfStrikeOut = FALSE; lfF.lfCharSet = SHIFTJIS_CHARSET; lfF.lfOutPrecision = OUT_DEFAULT_PRECIS; lfF.lfClipPrecision = CLIP_DEFAULT_PRECIS; lfF.lfQuality = DEFAULT_QUALITY; lfF.lfPitchAndFamily= 0; lfF.lfFaceName[0] = '\0'; } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; LPTSTR lptstr = TEXT("She twisted flowers into a wreath."); HDC hdc=BeginPaint(hw, &ps); HFONT hF= CreateFontIndirect(&lfF); SelectObject(hdc, hF); TextOut(hdc,100,10,lptstr,lstrlen(lptstr)); SelectObject(hdc,GetStockObject(SYSTEM_FONT)); DeleteObject(hF); EndPaint(hw,&ps); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ; return 0; case WM_CREATE : procCreate(hw,wp,lp); return 0; case WM_PAINT : procPaint (hw,wp,lp); return 0; } return DefWindowProc(hw,msg,wp,lp); } 2.2 点の描画 void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw, &ps); for(int i=0;i<100;i++) SetPixel(hdc,10+i,10,0xFF0000); EndPaint(hw,&ps); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ; return 0; case WM_PAINT : procPaint (hw,wp,lp); return 0; } return DefWindowProc(hw,msg,wp,lp); } 2.3 線の描画 (1)カレントポジションと線分 【procPaintのみ】 void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw, &ps); MoveToEx(hdc,10,10,NULL); LineTo(hdc,60,110); LineTo(hdc,110,10); EndPaint(hw,&ps); } (2)連続線分の描画 #include "myWin.h" #define NUMPOLY 5 static POINT lpP[NUMPOLY]; void procCreate(HWND hw, WPARAM wp,LPARAM lp){ lpP[0].x=150; lpP[0].y= 50; lpP[1].x=125; lpP[1].y=100; lpP[2].x= 75; lpP[2].y=100; lpP[3].x= 50; lpP[3].y= 50; lpP[4].x=100; lpP[4].y= 10; } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw, &ps); MoveToEx(hdc,100,10,NULL); PolylineTo(hdc,lpP,NUMPOLY); EndPaint(hw,&ps); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ; return 0; case WM_CREATE : procCreate(hw,wp,lp); return 0; case WM_PAINT : procPaint (hw,wp,lp); return 0; } return DefWindowProc(hw,msg,wp,lp); } (3)連続線分を分割して描画 #include "myWin.h" #define NUMPOLY 5 static POINT lpP[NUMPOLY];static CONST DWORD dw[]={3, 2}; void procCreate(HWND hw, WPARAM wp,LPARAM lp){ lpP[0].x=150; lpP[0].y= 50; lpP[1].x=125; lpP[1].y=100; lpP[2].x= 75; lpP[2].y=100; lpP[3].x= 50; lpP[3].y= 50; lpP[4].x=100; lpP[4].y= 10; } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw, &ps); PolyPolyline(hdc,lpP,dw,2); EndPaint(hw,&ps); } (WndProc以降は,前項(2)と同じ) 2.4 閉じた図形 (1) 長方形と楕円 #include "myWin.h" void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw, &ps); MoveToEx(hdc, 0, 0,NULL); LineTo(hdc,270,70); Rectangle(hdc,10,10,80,60); Ellipse (hdc,100,10,170,60); RoundRect(hdc,190,10,260,60,20,20); EndPaint(hw,&ps); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ; return 0; case WM_PAINT : procPaint(hw,wp,lp); return 0; } return DefWindowProc(hw,msg,wp,lp); } (2) 円弧・扇形・弓形 【procPaintのみ】 void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw, &ps); Arc (hdc, 10, 10, 100, 100, 100, 10, 10,55); Pie (hdc, 110, 10, 200, 100, 200, 10, 110,55); Chord(hdc, 210, 10, 300, 100, 300, 10, 210,55); EndPaint(hw,&ps); } 2.5 色々な曲線 (1) ベジェ曲線 #include "myWin.h" static POINT pt[3]; void procCreate(HWND hw, WPARAM wp,LPARAM lp){ pt[0].x=100; pt[0].y=100; pt[1].x=200; pt[1].y=50; pt[2].x=300; pt[2].y=100; } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps;RECT rect; HDC hdc=BeginPaint(hw, &ps); GetClientRect(hw,&rect); MoveToEx(hdc, 10,10,NULL); PolyBezierTo(hdc,pt,3); MoveToEx(hdc,10,10,NULL); PolylineTo(hdc,pt,3); EndPaint(hw,&ps); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ; return 0; case WM_CREATE : procCreate(hw,wp,lp); return 0; case WM_PAINT : procPaint(hw,wp,lp) ; return 0; } return DefWindowProc(hw,msg,wp,lp); } (2) りサージュ図形 #include "myWin.h" #include "math.h" #define PI 3.14159265358979 #define SIZE 100 void Lissajous(HDC hdc){ MoveToEx(hdc, SIZE*2+10, SIZE+10,NULL); for(int i=0;i<=360;i++){ double A=PI*i/180; LineTo(hdc, (int)(SIZE*(cos(3*A)+1))+10, (int) (SIZE * (sin(5*A) +1)) + 10); } } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps;RECT rect; HDC hdc=BeginPaint(hw, &ps); GetClientRect(hw,&rect); Lissajous(hdc); EndPaint(hw,&ps); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ; return 0; case WM_PAINT : procPaint(hw,wp,lp) ; return 0; } return DefWindowProc(hw,msg,wp,lp); } (3) コッホ曲線 #include "myWin.h" #include "math.h" #define PI 3.14159265358979 static double curX,curY;static double Theta; void t_pos(double X,double Y){ curX=X; curY=Y;} void t_deg (double T){ Theta =T;} void t_turn(double T){ Theta +=T;} void t_forward(HDC hdc,double length){ double TH=Theta*PI/180; double X=curX+length*cos(TH); double Y=curY+length*sin(TH); MoveToEx(hdc,(int)curX, 100-(int)curY, NULL); LineTo (hdc,(int) X, 100-(int) Y); curX=X; curY=Y; } void koch(HDC hdc,int n, double length){ if(n<=0) t_forward(hdc,length); else{ int nn=n-1;double len=length/3; koch(hdc,nn,len);t_turn(60); koch(hdc,nn,len);t_turn(-120); koch(hdc,nn,len);t_turn(60); koch(hdc,nn,len); } } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw, &ps); t_pos(0,0);t_deg(0); koch(hdc,5,300); EndPaint(hw,&ps); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ; return 0; case WM_PAINT : procPaint(hw,wp,lp) ; return 0; } return DefWindowProc(hw,msg,wp,lp); } 2.6 ペンとブラシ (1) ペンの作成 #include "myWin.h" void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw,&ps); HPEN hpen=CreatePen(PS_DASH,1,0xFF0000); SelectObject(hdc, hpen); Rectangle(hdc,20,20,100,60); DeleteObject(hpen); EndPaint(hw,&ps); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0); return 0; case WM_PAINT:procPaint(hw, wp, lp); } return DefWindowProc(hw,msg,wp,lp); } (2) CreatePenIndirectを使う #include "myWin.h" Static LOGPEN logpen; void procCreate(HWND hw, WPARAM wp,LPARAM lp){ logpen.lopnStyle = PS_SOLID; logpen.lopnWidth.x = 4; logpen.lopnColor = 0xFF; } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw,&ps); SelectObject(hdc,CreatePenIndirect(&logpen)); Rectangle(hdc,20,20,100,60); DeleteObject(SelectObject(hdc,GetStockObject(BLACK_PEN)); EndPaint(hw,&ps); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY: PostQuitMessage(0); return 0; case WM_CREATE : procCreate(hw, wp, lp); case WM_PAINT : procPaint(hw, wp, lp); } return DefWindowProc(hw,msg,wp,lp); } (3) ブラシ 【procPaintのみ】 void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw,&ps); SelectObject(hdc,CreateSolidBrush(0xFF)); Rectangle(hdc, 10, 10,200,100); DeleteObject(SelectObject(hdc,CreateSolidBrush(0xFF00))); Rectangle(hdc,30,30,240,60); DeleteObject(SelectObject(hdc,GetStockObject(WHITE_BRUSH))); EndPaint(hw,&ps); } 【procPaintのみ】 void procPaint(HWND hw){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw,&ps); HPEN hpen = CreatePen(PS_SOLID,4,0xFF) ;SelectObject(hdc,hpen); HBRUSH hbrush = CreateHatchBrush(HS_DIAGCROSS,0XFF00); SelectObject(hdc,hbrush); Rectangle(hdc, 10, 10,200,100); DeleteObject(hbrush); hbrush = CreateHatchBrush(HS_CROSS,0XFF0000); SelectObject(hdc,hbrush); Ellipse(hdc, 20, 20,240,80); DeleteObject(hbrush); DeleteObject(hpen); EndPaint(hw,&ps); } (4) ビットマップブラシ 【myBitmap.rcの内容】"myBitmap.bmp"は32×32のビットマップ myBitmap BITMAP "myBitmap.bmp" 【ソースプログラム】 //ビットマップブラシ #include static HBITMAP hB; // ビットマップハンドル static HBRUSH hBack; // パターンブラシのハンドル LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ; return 0; } return DefWindowProc(hw,msg,wp,lp); } BOOL initApp(HINSTANCE hCurI){ WNDCLASS wc; hB = LoadBitmap(hCurI,TEXT("myBitmap"));// ビットマップロード hBack = CreatePatternBrush(hB); // ブラシ生成 wc.style = CS_HREDRAW | CS_VREDRAW|CS_DBLCLKS; wc.lpfnWndProc = WndProc; wc.cbClsExtra = wc.cbWndExtra=0; wc.hInstance = hCurI; wc.hIcon = LoadIcon(NULL,IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground= hBack; // 生成したブラシを背景用とする wc.lpszMenuName = NULL; wc.lpszClassName= TEXT("クラス名"); return RegisterClass(&wc); } HWND initInst(HINSTANCE hCurI){ HWND hw; hw=CreateWindow(TEXT("クラス名"),TEXT("こんばんは"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hCurI,NULL); return hw; } int WINAPI WinMain(HINSTANCE hCurI, HINSTANCE hPreI, PSTR lp, int cmd){ MSG msg; if(!initApp(hCurI)) return 0; if(initInst(hCurI)==NULL) return 0; while(GetMessage(&msg,NULL,0,0)){ TranslateMessage(&msg); DispatchMessage(&msg); } DeleteObject(hB);// リソース解放 DeleteObject(hBack); return msg.wParam; } 2.7 ラスタオペレーション (2) ラスタオペレーションの例 #include "myWin.h" void procPaint(HWND hw){ PAINTSTRUCT ps;HBRUSH h1; HDC hdc=BeginPaint(hw,&ps); h1= CreateSolidBrush(0xFF0000);SelectObject(hdc,h1); Ellipse(hdc,10,10,200,100); DeleteObject(h1); SetROP2(hdc,R2_XORPEN); //ラスタオペレーションの指定 h1= CreateSolidBrush(0xFF); SelectObject(hdc,h1); Rectangle(hdc,100,30,240,80); DeleteObject(h1); EndPaint(hw,&ps); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0); return 0; case WM_PAINT : procPaint(hw) ; return 0; } return DefWindowProc(hw,msg,wp,lp); } 2.8 ビットマップの表示 (1) 原寸大の表示 【suzume.rcの内容】 suzume BITMAP "suzume.bmp" 【ソースプログラム】 #include "myWin.h" static HBITMAP hB; void procCreate(HWND hw, WPARAM wp,LPARAM lp){ hB=LoadBitmap(((LPCREATESTRUCT)lp)->hInstance, TEXT("suzume")); } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc = BeginPaint(hw,&ps); HDC hBuff= CreateCompatibleDC(hdc); SelectObject(hBuff,hB); BitBlt(hdc,10,10,1000,600,hBuff,0,0,SRCCOPY); DeleteDC(hBuff); EndPaint(hw,&ps); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ; return 0; case WM_CREATE : procCreate(hw,wp,lp); return 0; case WM_PAINT : procPaint (hw,wp,lp); return 0; } return DefWindowProc(hw,msg,wp,lp); } (2) 伸縮表示 //ビットマップの表示(ストレッチ) #include "myWin.h" #define SCALE 2 //縮小率 static HBITMAP hB; static BITMAP bB; void procCreate(HWND hw, WPARAM wp,LPARAM lp){ hB=LoadBitmap(((LPCREATESTRUCT)lp)->hInstance, TEXT("suzume")); GetObject(hB,sizeof(BITMAP),&bB); } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; RECT rect; HDC hdc = BeginPaint(hw,&ps);GetClientRect(hw,&rect); HDC hBuff= CreateCompatibleDC(hdc); SelectObject(hBuff,hB); SetStretchBltMode(hdc,COLORONCOLOR); StretchBlt(hdc, rect.right /2 - bB.bmWidth /(SCALE*2), rect.bottom/2 - bB.bmHeight/(SCALE*2), bB.bmWidth/SCALE, bB.bmHeight/SCALE, hBuff,0,0, bB.bmWidth, bB.bmHeight,SRCCOPY); DeleteDC(hBuff); EndPaint(hw,&ps); } (以下,WndProc以降省略) 2.9 その他の描画 (1) 多角形塗りつぶしモード #include "myWin.h" static POINT P1[5]={{100,10},{120,90},{70,40},{130,40},{80,90}}; static POINT P2[5]; void procCreate(HWND hw, WPARAM wp,LPARAM lp){ for(int i=0;i<5;i++){P2[i].x=P1[i].x+100; P2[i].y=P1[i].y;} } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw,&ps); HBRUSH hbrush=CreateSolidBrush(0xFF); SelectObject(hdc, hbrush); SetPolyFillMode(hdc,WINDING); Polygon(hdc,P1,5); SetPolyFillMode(hdc,ALTERNATE); Polygon(hdc,P2,5); DeleteObject(hbrush); EndPaint(hw,&ps); } (以下,WndProc以降省略) (2) ビットマップに描画 #include static HDC hBuff; static HBITMAP hBM; void procCreate(HWND hw, WPARAM wp,LPARAM lp){ HDC hdc=GetDC(hw); hBM=CreateCompatibleBitmap(hdc,1000,600); hBuff=CreateCompatibleDC(hdc); SelectObject(hBuff,hBM); SelectObject(hBuff,GetStockObject(NULL_PEN)); PatBlt(hBuff,0,0,1000,600,WHITENESS); ReleaseDC(hw,hdc); } void procMouseMove(HWND hw, WPARAM wp,LPARAM lp){ if(GetKeyState(VK_LBUTTON)<0){ SelectObject(hBuff,CreateSolidBrush(0xFF)); Ellipse(hBuff,LOWORD(lp)-2, HIWORD(lp)-2, LOWORD(lp)+2, HIWORD(lp)+2); DeleteObject(SelectObject(hBuff,GetStockObject(WHITE_BRUSH))); InvalidateRect(hw,NULL,FALSE); } } void procPaint(HWND hw,WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw,&ps); BitBlt(hdc,0,0,1000,600,hBuff,0,0,SRCCOPY); EndPaint(hw,&ps); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : DeleteDC(hBuff);DeleteObject(hBM); PostQuitMessage(0) ; return 0; case WM_CREATE : procCreate(hw,wp,lp) ; return 0; case WM_MOUSEMOVE: procMouseMove(hw,wp,lp); return 0; case WM_PAINT : procPaint(hw,wp,lp) ; return 0; } return DefWindowProc(hw,msg,wp,lp); } (3) リージョン BOOL FrameRgn(HDC hdc, HRGN hrgn, HBRUSH hbr, int nWidth, int nHight); void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw,&ps); HRGN hrg=CreateRoundRectRgn(10,10,200,100,10,10); SelectObject(hdc,CreateHatchBrush(HS_DIAGCROSS,RGB(0,0,0xFF))); PaintRgn(hdc,hrg); FrameRgn(hdc,hrg, (HBRUSH)GetStockObject(GRAY_BRUSH),3,10); DeleteObject(hrg); DeleteObject(SelectObject(hdc,GetStockObject(BLACK_BRUSH))); EndPaint(hw,&ps); } (WndProc以降省略) 【リージョン内だけを再描画】 void procLeftButtonDown(HWND hw, WPARAM wp,LPARAM lp){ HDC hdc=GetDC(hw); SelectObject(hdc,GetStockObject(WHITE_PEN)); Rectangle(hdc,0,0,210,110); HRGN hrg=CreateRoundRectRgn(10,10,200,100,10,10); SelectObject(hdc,CreateHatchBrush(HS_DIAGCROSS,RGB(0,0,0xFF))); PaintRgn(hdc,hrg); FrameRgn(hdc,hrg, (HBRUSH)GetStockObject(GRAY_BRUSH),3,10); InvalidateRgn(hw,hrg,FALSE); DeleteObject(hrg); DeleteObject(SelectObject(hdc,GetStockObject(BLACK_BRUSH))); ReleaseDC(hw,hdc); } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; TCHAR str[10]; HDC hdc=BeginPaint(hw,&ps); SelectObject(hdc,GetStockObject(BLACK_BRUSH)); Ellipse(hdc,0,0,50,50); EndPaint(hw,&ps); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ; return 0; case WM_LBUTTONDOWN : procLeftButtonDown(hw,wp,lp); return 0; case WM_PAINT : procPaint(hw,wp,lp) ; return 0; } return DefWindowProc(hw,msg,wp,lp); }