3.1 マウス (1) マウスの位置 #include "myWin.h" static TCHAR str[128]; void procNcHitTest(HWND hw,WPARAM wp,LPARAM lp){ int X= LOWORD(lp), Y=HIWORD(lp); wsprintf(str, TEXT("\n X座標=%d\n Y座標=%d"),X,Y); InvalidateRect(hw,NULL,TRUE); } void procPaint(HWND hw){ PAINTSTRUCT ps;RECT rect; HDC hdc=BeginPaint(hw, &ps); GetClientRect(hw,&rect); DrawText(hdc,str,-1,&rect,DT_LEFT); 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_NCHITTEST: procNcHitTest(hw,wp,lp); break; case WM_PAINT : procPaint(hw) ; return 0; } return DefWindowProc(hw,msg,wp,lp); } (2) 非クライアント領域でのダブルクリック #include "myWin.h" static TCHAR str[128]; static unsigned short int x=0, y=0; void procPaint(HWND hw){ PAINTSTRUCT ps;RECT rect; HDC hdc=BeginPaint(hw, &ps); GetClientRect(hw,&rect); DrawText(hdc, str,-1,&rect,DT_LEFT); EndPaint(hw,&ps); } void procNcLeftButtonDoubleClick(HWND hw, WPARAM wp, LPARAM lp){ x=LOWORD(lp); y=HIWORD(lp); wsprintf(str,TEXT("\n X座標=%d\n Y座標\n HitTest=%d"),x,y,wp); InvalidateRect(hw,NULL,TRUE); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0); return 0; case WM_NCLBUTTONDBLCLK:procNcLeftButtonDoubleClick(hw,wp,lp); ;return 0; case WM_PAINT:procPaint(hw) ; return 0; } return DefWindowProc(hw,msg,wp,lp); } (3) クライアント領域でのダブルクリック #include "myWin.h" static LPCTSTR str=TEXT("ここでダブルクリック"); static unsigned short int x=0, y=0; void procPaint(HWND hw){ PAINTSTRUCT ps;RECT rect; HDC hdc=BeginPaint(hw, &ps); TextOut(hdc, x, y, str,lstrlen(str)); EndPaint(hw,&ps); } void procLeftButtonDoubleClick(HWND hw, WPARAM wp,LPARAM lp){ x=LOWORD(lp); y=HIWORD(lp); InvalidateRect(hw,NULL,TRUE); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ; return 0; case WM_LBUTTONDBLCLK : procLeftButtonDoubleClick(hw,wp, lp) ; return 0; case WM_PAINT : procPaint(hw) ; return 0; } return DefWindowProc(hw,msg,wp,lp); } (4) クライアント領域でのクリック,マウス移動 #include "myWin.h" static RECT rect; static BOOL pushMouse=FALSE; void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw,&ps); Rectangle(hdc,rect.left, rect.top, rect.right, rect.bottom); EndPaint(hw,&ps); } void procLeftButtonDown(HWND hw, WPARAM wp,LPARAM lp){{ pushMouse=TRUE; rect.left=LOWORD(lp); rect.top=HIWORD(lp); SetCapture(hw); } void procLeftButtonUp(HWND hw, WPARAM wp,LPARAM lp){ pushMouse=FALSE; ReleaseCapture(); } void procMouseMove(HWND hw, WPARAM wp,LPARAM lp){ if(pushMouse){ rect.right=LOWORD(lp);rect.bottom=HIWORD(lp); InvalidateRect(hw,NULL,TRUE); } } 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_LBUTTONUP : procLeftButtonUp (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.2 キーボード (2) 仮想キーコードの取り出し #include "myWin.h" void procKeyDown(HWND hw, WPARAM wp,LPARAM lp){ POINT P; GetCursorPos(&P); switch(wp){ case VK_UP : P.y--;break; case VK_DOWN : P.y++;break; case VK_LEFT : P.x--;break; case VK_RIGHT : P.x++;break; } SetCursorPos(P.x, P.y); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ; return 0; case WM_KEYDOWN : procKeyDown(hw, wp,lp); return 0; } return DefWindowProc(hw,msg,wp,lp); } (3) キー入力値の確認 #include "myWin.h" static TCHAR str[128], strStat[128],strW[2]; void procKeyDown(HWND hw, WPARAM wp,LPARAM lp){ if(wp==VK_SHIFT) lstrcpy(strStat,TEXT("シフトキーが押されました。")); wsprintf(str,TEXT("Key Down Character =%02Xh"),wp); InvalidateRect(hw,NULL,TRUE); } void procKeyUp(HWND hw, WPARAM wp,LPARAM lp){ if(wp==VK_SHIFT) return ; if(GetKeyState(VK_SHIFT)<0) lstrcpy(strStat,TEXT("シフトキーが押されています。")); else lstrcpy(strStat,TEXT("シフトキーは押されていません。")); InvalidateRect(hw,NULL,TRUE); } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw,&ps); TextOut(hdc,10, 10,str ,lstrlen(str )); TextOut(hdc,10, 60,strW ,lstrlen(strW )); TextOut(hdc,10,100,strStat,lstrlen(strStat)); EndPaint(hw,&ps); } void procChar(HWND hw, WPARAM wp,LPARAM lp){ strW[0]=wp; strW[1]=0; InvalidateRect(hw,NULL,TRUE); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ; return 0; case WM_KEYDOWN : procKeyDown(hw,wp,lp); return 0; case WM_KEYUP : procKeyUp (hw,wp,lp); return 0; case WM_CHAR : procChar (hw,wp,lp); return 0; case WM_PAINT : procPaint (hw,wp,lp); return 0; } return DefWindowProc(hw,msg,wp,lp); } 3.3 タイマー (1) タイマの作成 #include "myWin.h" static RECT rctD; static BOOL moveRight=TRUE; static int X=0, dX=5; void procCreate(HWND hw, WPARAM wp,LPARAM lp){SetTimer(hw,1,10,NULL);} void procTimer(HWND hw, WPARAM wp,LPARAM lp){ GetClientRect(hw,&rctD); if (X+50>=rctD.right) moveRight=FALSE; else if(X<=0) moveRight=TRUE; if(moveRight) X +=dX; else X-=dX; InvalidateRect(hw,NULL,TRUE); } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw,&ps); SelectObject(hdc, GetStockObject(BLACK_BRUSH)); Ellipse(hdc,X,50,X+50,100); 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_TIMER : procTimer (hw,wp,lp); return 0; case WM_PAINT : procPaint (hw,wp,lp); return 0; } return DefWindowProc(hw,msg,wp,lp); } (2) タイマの破棄 #include "myWin.h" static int iCount=0; void procCreate(HWND hw, WPARAM wp,LPARAM lp){ SetTimer(hw,1,200,NULL);} void procTimer(HWND hw, WPARAM wp,LPARAM lp){ iCount++; if(iCount==50) KillTimer(hw,1); InvalidateRect(hw,NULL,TRUE); } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw,&ps); TCHAR strCount[64]; wsprintf(strCount, TEXT("%d"),iCount); TextOut(hdc,10,10,strCount, lstrlen(strCount)); 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_TIMER : procTimer (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 TIMER_1 1 #define TIMER_2 2 static int iCount1=0,iCount2=0; void procCreate(HWND hw,WPARAM wp,LPARAM lp){ SetTimer(hw,TIMER_1,100,NULL); SetTimer(hw,TIMER_2,500,NULL); } void procTimer(HWND hw,WPARAM wp,LPARAM lp){ switch(wp){ case TIMER_1: iCount1++;break; case TIMER_2: iCount2++;break; } InvalidateRect(hw,NULL,TRUE); } void procPaint(HWND hw,WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw,&ps); TCHAR strCount[64]; wsprintf(strCount, TEXT("Timer 1 = %d, Timer 2 = %d"), iCount1, iCount2); TextOut(hdc,10,10,strCount, lstrlen(strCount)); 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_TIMER : procTimer (hw,wp,lp); return 0; case WM_PAINT : procPaint (hw,wp,lp); return 0; } return DefWindowProc(hw,msg,wp,lp); } ? (4) システム時間の取得 #include "myWin.h" static TCHAR strT[255]; static TCHAR WD[7][4]= { TEXT("日"),TEXT("月"),TEXT("火"),TEXT("水"), TEXT("木"),TEXT("金"),TEXT("土")}; void procCreate(HWND hw, WPARAM wp,LPARAM lp){SetTimer(hw, 1,50,NULL);} void procTimer(HWND hw, WPARAM wp,LPARAM lp){ SYSTEMTIME sysT; GetLocalTime(&sysT); wsprintf(strT, TEXT("\n %4d年%3d月%3d日(%s)\n\n %3d時%3d分%3d秒%4dミリ秒"), sysT.wYear, sysT.wMonth, sysT.wDay,WD[sysT.wDayOfWeek], sysT.wHour,sysT.wMinute,sysT.wSecond,sysT.wMilliseconds); InvalidateRect(hw,NULL,TRUE); } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; RECT rct; HDC hdc=BeginPaint(hw,&ps); GetClientRect(hw,&rct); DrawText(hdc, strT,-1,&rct, DT_LEFT); 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_TIMER : procTimer (hw,wp,lp); return 0; case WM_PAINT : procPaint (hw,wp,lp); return 0; } return DefWindowProc(hw,msg,wp,lp); } 3.4 メッセージ 【SendMessage】 #include "myWin.h" void procLeftButtonDown(HWND hw, WPARAM wp,LPARAM lp){ SendMessage(hw,WM_CLOSE,0,0); } 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); } ? 【SendMessageとPostMessageの違い】 #include "myWin.h" void procLeftButtonDown(HWND hw, WPARAM wp,LPARAM lp){ SendMessage(hw,WM_RBUTTONDOWN,wp,lp); //PostMessage(hw,WM_RBUTTONDOWN,wp,lp); HDC hdc=GetDC(hw); SelectObject(hdc, GetStockObject(BLACK_BRUSH)); Rectangle(hdc,30,30,150,80); ReleaseDC(hw,hdc); } void procRightButtonDown(HWND hw, WPARAM wp,LPARAM lp){ HDC hdc=GetDC(hw); for(int x=0;x<100; x++) for(int y=0;y<100;y++)SetPixel(hdc,x,y,0xFFFF); 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; case WM_RBUTTONDOWN: procRightButtonDown(hw,wp,lp);return 0; } return DefWindowProc(hw,msg,wp,lp); }