数字媒体学院上机实验报告I八、附录LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {HDC hdc ;PAINTSTRUCT ps ;RECT rect ;switch (message){case WM_CREATE:PlaySound (TEXT ("123.wav"), NULL, SND_FILENAME | SND_ASYNC) ;return 0 ;case WM_PAINT:hdc = BeginPaint (hwnd, &ps) ;GetClientRect (hwnd, &rect) ;DrawText (hdc, TEXT ("Hello, Windows 7!"), -1, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;EndPaint (hwnd, &ps) ;return 0 ;case WM_DESTROY:PostQuitMessage (0) ;return 0 ;}return DefWindowProc (hwnd, message, wParam, lParam) ;}运行结果截图:上机实验报告II八、附录LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {static TCHAR szBuffer[8]=TEXT("船有20米长。
");TEXTMETRIC tm;static int cxChar,cyChar;static POINT BoatFigure[4]static int cxClient, cyClient ;HDC hdc ;int i,j ;PAINTSTRUCT ps ;POINT Boat[4] ;static int CloudFigure[12][4];int Cloud[12][4];static POINT PoleFigure[4];POINT Pole[4];static POINT FlagFigure[3];POINT Flag[3];static int BubbleFigure[3][4]int Bubble[3][4];static POINT ArrowFigure[4][3];POINT Arrow[4][3];HBRUSH hOrangeBrush,hBlueBrush,hBrownBrush,hRedBrush;switch (message){case WM_SIZE:cxClient = LOWORD (lParam) ;cyClient = HIWORD (lParam) ;return 0 ;//////////////////////////////////////////case WM_PAINT:hdc = BeginPaint (hwnd, &ps) ;GetTextMetrics(hdc,&tm);cxChar=tm.tmAveCharWidth;cyChar=tm.tmHeight+tm.tmExternalLeading;SetMapMode(hdc,MM_ISOTROPIC);SetWindowExtEx(hdc,cxClient/2,cyClient/2,NULL);SetViewportExtEx(hdc,cxClient/2,-cyClient/2,NULL);SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);for(i=0;i<4;i++){Boat[i].x=cxClient*BoatFigure[i].x/70;Boat[i].y=cyClient*BoatFigure[i].y/40;Pole[i].x=cxClient*PoleFigure[i].x/140;Pole[i].y=cyClient*PoleFigure[i].y/40;}for(i=0;i<3;i++){Flag[i].x=cxClient*FlagFigure[i].x/70;Flag[i].y=cyClient*FlagFigure[i].y/40;}for(i=0;i<3;i++){Bubble[i][0]= cxClient*BubbleFigure[i][0]/70;Bubble[i][1]= cyClient*BubbleFigure[i][1]/40;Bubble[i][2]= cxClient*BubbleFigure[i][2]/70;Bubble[i][3]= cyClient*BubbleFigure[i][3]/40;}for(i=0;i<12;i++){Cloud[i][0]= cxClient*CloudFigure[i][0]/70;Cloud[i][1]= cyClient*CloudFigure[i][1]/40;Cloud[i][2]= cxClient*CloudFigure[i][2]/70;Cloud[i][3]= cyClient*CloudFigure[i][3]/40;}for(i=0;i<4;i++)for(j=0;j<3;j++){Arrow[i][j].x=cxClient*ArrowFigure[i][j].x/140;Arrow[i][j].y=cyClient*ArrowFigure[i][j].y/80;}SelectObject (hdc, hBlueBrush) ;SetPolyFillMode (hdc, ALTERNATE) ;for(i=0;i<11;i++)Ellipse(hdc,Cloud[i][0],Cloud[i][1],Cloud[i][2],Cloud[i][3]);for(i=0;i<3;i++)Ellipse(hdc,Bubble[i][0],Bubble[i][1],Bubble[i][2],Bubble[i][3]);SelectObject (hdc,GetStockObject(NULL_PEN));Ellipse(hdc,Cloud[11][0],Cloud[11][1],Cloud[11][2],Cloud[11][3]);DeleteObject(hBlueBrush);SelectObject (hdc, hOrangeBrush) ;Polygon (hdc, Boat, 4);DeleteObject(hOrangeBrush);SelectObject(hdc,hBrownBrush);Polygon(hdc,Pole,4);DeleteObject(hBrownBrush);SelectObject(hdc,hRedBrush);Polygon(hdc,Flag,3);DeleteObject(hRedBrush);SelectObject(hdc,GetStockObject(BLACK_BRUSH));for(i=0;i<4;i++)Polygon(hdc,Arrow[i],3);SelectObject(hdc,GetStockObject(BLACK_PEN));MoveToEx(hdc,cxClient*(-29)/70,0,NULL);LineTo(hdc,cxClient*24/70,0);MoveToEx(hdc,0,cyClient*18/40,NULL);LineTo(hdc,0,cyClient*(-16)/40);MoveToEx(hdc,cxClient*(-14)/70,cyClient*(-3)/40,NULL);LineTo(hdc,cxClient*(-14)/70,cyClient*(-19)/40);MoveToEx(hdc,cxClient*18/70,cyClient*(-3)/40,NULL);LineTo(hdc,cxClient*18/70,cyClient*(-19)/40);MoveToEx(hdc,cxClient*(-14)/70,cyClient*(-18)/40,NULL);LineTo(hdc,cxClient*18/70,cyClient*(-18)/40);SetTextColor(hdc,RGB(255,66,67));TextOut(hdc,-3*cxChar,cyClient*(-18)/40+cyChar,szBuffer,lstrlen(szBuffer)); EndPaint(hwnd, &ps) ;return 0 ;////////////////////////////case WM_DESTROY:PostQuitMessage (0) ;return 0 ;}return DefWindowProc (hwnd, message, wParam, lParam) ;}运行结果截图:上机实验报告III八、附录LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {static DWORD dwCharSet = DEFAULT_CHARSET ;static int cxChar, cyChar, cxClient, cyClient, cxBuffer, cyBuffer,xCaret, yCaret ;static TCHAR * pBuffer = NULL ;HDC hdc ;int x, y, i ;PAINTSTRUCT ps ;TEXTMETRIC tm ;switch (message){case WM_INPUTLANGCHANGE:dwCharSet = wParam ;// fall throughcase WM_CREATE:hdc = GetDC (hwnd) ;SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,dwCharSet, 0, 0, 0, FIXED_PITCH, NULL)) ;GetTextMetrics (hdc, &tm) ;cxChar = tm.tmAveCharWidth ;cyChar = tm.tmHeight ;DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;ReleaseDC (hwnd, hdc) ;// fall through case WM_SIZE:// obtain window size in pixelsif (message == WM_SIZE){cxClient = LOWORD (lParam) ;cyClient = HIWORD (lParam) ;}// calculate window size in characterscxBuffer = max (1, cxClient / cxChar) ;cyBuffer = max (1, cyClient / cyChar) ;// allocate memory for buffer and clear itif (pBuffer != NULL)free (pBuffer) ;pBuffer = (TCHAR *) malloc (cxBuffer * cyBuffer * sizeof (TCHAR)) ;for (y = 0 ; y < cyBuffer ; y++)for (x = 0 ; x < cxBuffer ; x++)BUFFER(x,y) = ' ' ;// set caret to upper left cornerxCaret = 0 ;yCaret = 0 ;if (hwnd == GetFocus ())SetCaretPos (xCaret * cxChar, yCaret * cyChar) ; InvalidateRect (hwnd, NULL, TRUE) ;return 0 ;case WM_SETFOCUS:// create and show the caretCreateCaret (hwnd, NULL, cxChar, cyChar) ;SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;ShowCaret (hwnd) ;return 0 ;case WM_KILLFOCUS:// hide and destroy the caretHideCaret (hwnd) ;DestroyCaret () ;return 0 ;case WM_KEYDOWN:switch (wParam){case VK_HOME:xCaret = 0 ;break ;case VK_END:xCaret = cxBuffer - 1 ;break ;case VK_PRIOR:yCaret = 0 ;break ;case VK_NEXT:yCaret = cyBuffer - 1 ;break ;case VK_LEFT:xCaret = max (xCaret - 1, 0) ;break ;case VK_RIGHT:xCaret = min (xCaret + 1, cxBuffer - 1) ;break ;case VK_UP:yCaret = max (yCaret - 1, 0) ;break ;case VK_DOWN:yCaret = min (yCaret + 1, cyBuffer - 1) ;break ;case VK_DELETE:for (x = xCaret ; x < cxBuffer - 1 ; x++)BUFFER (x, yCaret) = BUFFER (x + 1, yCaret) ;BUFFER (cxBuffer - 1, yCaret) = ' ' ;HideCaret (hwnd) ;hdc = GetDC (hwnd) ;SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,dwCharSet, 0, 0, 0, FIXED_PITCH, NULL)) ;TextOut (hdc, xCaret * cxChar, yCaret * cyChar,& BUFFER (xCaret, yCaret),cxBuffer - xCaret) ;DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ; ReleaseDC (hwnd, hdc) ;ShowCaret (hwnd) ;break ;}SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;return 0 ;case WM_CHAR:for (i = 0 ; i < (int) LOWORD (lParam) ; i++){switch (wParam){case'\b': // backspaceif (xCaret > 0){xCaret-- ;SendMessage (hwnd, WM_KEYDOWN, VK_DELETE, 1) ;}break ;case'\t': // tabdo{SendMessage (hwnd, WM_CHAR, ' ', 1) ;}while (xCaret % 8 != 0) ;break ;case'\n': // line feedif (++yCaret == cyBuffer)yCaret = 0 ;break ;case'\r': // carriage returnxCaret = 0 ;if (++yCaret == cyBuffer)yCaret = 0 ;break ;case'\x1B': // escapefor (y = 0 ; y < cyBuffer ; y++)for (x = 0 ; x < cxBuffer ; x++)BUFFER (x, y) = ' ' ;xCaret = 0 ;yCaret = 0 ;InvalidateRect (hwnd, NULL, FALSE) ;break ;default: // character codesBUFFER (xCaret, yCaret) = (TCHAR) wParam ;HideCaret (hwnd) ;hdc = GetDC (hwnd) ;SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,dwCharSet, 0, 0, 0, FIXED_PITCH, NULL)) ;TextOut (hdc, xCaret * cxChar, yCaret * cyChar,& BUFFER (xCaret, yCaret), 1) ;DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;ReleaseDC (hwnd, hdc) ;ShowCaret (hwnd) ;if (++xCaret == cxBuffer){xCaret = 0 ;if (++yCaret == cyBuffer)yCaret = 0 ;}break ;}}SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;return 0 ;case WM_PAINT:hdc = BeginPaint (hwnd, &ps) ;SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,dwCharSet, 0, 0, 0, FIXED_PITCH, NULL)) ;for (y = 0 ; y < cyBuffer ; y++)TextOut (hdc, 0, y * cyChar, & BUFFER(0,y), cxBuffer) ;DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;EndPaint (hwnd, &ps) ;上机实验报告IV八、附录LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){static COLORREF crPrim[3] = { RGB (255, 0, 0), RGB (0, 255, 0),RGB (0, 0, 255) } ;static HBRUSH hBrush[3], hBrushStatic ;static HWND hwndScroll[3], hwndLabel[3], hwndValue[3], hwndRect ;static int color[3], cyChar ;static RECT rcColor ;static TCHAR * szColorLabel[] = { TEXT ("Red"), TEXT ("Green"),TEXT ("Blue") } ;HINSTANCE hInstance ;int i, cxClient, cyClient ;TCHAR szBuffer[10] ;switch (message){case WM_CREATE :hInstance = (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE) ;hwndRect = CreateWindow (TEXT ("static"), NULL,WS_CHILD | WS_VISIBLE | SS_WHITERECT,0, 0, 0, 0,hwnd, (HMENU) 9, hInstance, NULL) ;for (i = 0 ; i < 3 ; i++){hwndScroll[i] = CreateWindow (TEXT ("scrollbar"), NULL,WS_CHILD | WS_VISIBLE |WS_TABSTOP | SBS_VERT,0, 0, 0, 0,hwnd, (HMENU) i, hInstance, NULL) ;SetScrollRange (hwndScroll[i], SB_CTL, 0, 255, FALSE) ;SetScrollPos (hwndScroll[i], SB_CTL, 0, FALSE) ;hwndLabel [i] = CreateWindow (TEXT ("static"), szColorLabel[i],WS_CHILD | WS_VISIBLE | SS_CENTER,0, 0, 0, 0,hwnd, (HMENU) (i + 3),hInstance, NULL) ;hwndValue [i] = CreateWindow (TEXT ("static"), TEXT ("0"),WS_CHILD | WS_VISIBLE | SS_CENTER,0, 0, 0, 0,hwnd, (HMENU) (i + 6),hInstance, NULL) ;OldScroll[i] = (WNDPROC) SetWindowLong (hwndScroll[i],GWL_WNDPROC, (LONG) ScrollProc) ;hBrush[i] = CreateSolidBrush (crPrim[i]) ;}hBrushStatic = CreateSolidBrush (GetSysColor (COLOR_BTNHIGHLIGHT)) ;cyChar = HIWORD (GetDialogBaseUnits ()) ;return 0 ;case WM_SIZE :cxClient = LOWORD (lParam) ;cyClient = HIWORD (lParam) ;SetRect (&rcColor, cxClient / 2, 0, cxClient, cyClient) ;MoveWindow (hwndRect, 0, 0, cxClient / 2, cyClient, TRUE) ;for (i = 0 ; i < 3 ; i++){MoveWindow (hwndScroll[i],(2 * i + 1) * cxClient / 14, 2 * cyChar,cxClient / 14, cyClient - 4 * cyChar, TRUE) ; MoveWindow (hwndLabel[i],(4 * i + 1) * cxClient / 28, cyChar / 2,cxClient / 7, cyChar, TRUE) ;MoveWindow (hwndValue[i],(4 * i + 1) * cxClient / 28,cyClient - 3 * cyChar / 2,cxClient / 7, cyChar, TRUE) ;}SetFocus (hwnd) ;return 0 ;case WM_SETFOCUS :SetFocus (hwndScroll[idFocus]) ;return 0 ;case WM_VSCROLL :i = GetWindowLong ((HWND) lParam, GWL_ID) ;switch (LOWORD (wParam)){case SB_PAGEDOWN :color[i] += 15 ;case SB_LINEDOWN :color[i] = min (255, color[i] + 1) ;break ;case SB_PAGEUP :color[i] -= 15 ;case SB_LINEUP :color[i] = max (0, color[i] - 1) ;break ;case SB_TOP :color[i] = 0 ;break ;case SB_BOTTOM :color[i] = 255 ;break ;case SB_THUMBPOSITION :case SB_THUMBTRACK :color[i] = HIWORD (wParam) ;break ;default :break ;}SetScrollPos (hwndScroll[i], SB_CTL, color[i], TRUE) ;wsprintf (szBuffer, TEXT ("%i"), color[i]) ;SetWindowText (hwndValue[i], szBuffer) ;DeleteObject ((HBRUSH)SetClassLong (hwnd, GCL_HBRBACKGROUND, (LONG)CreateSolidBrush (RGB (color[0], color[1], color[2])))) ; InvalidateRect (hwnd, &rcColor, TRUE) ;return 0 ;case WM_CTLCOLORSCROLLBAR :i = GetWindowLong ((HWND) lParam, GWL_ID) ;return (LRESULT) hBrush[i] ;case WM_CTLCOLORSTATIC :i = GetWindowLong ((HWND) lParam, GWL_ID) ;if (i >= 3 && i <= 8) // static text controls{SetTextColor ((HDC) wParam, crPrim[i % 3]) ;SetBkColor ((HDC) wParam, GetSysColor (COLOR_BTNHIGHLIGHT));return (LRESULT) hBrushStatic ;}break ;case WM_SYSCOLORCHANGE :DeleteObject (hBrushStatic) ;hBrushStatic = CreateSolidBrush (GetSysColor (COLOR_BTNHIGHLIGHT)) ;return 0 ;case WM_DESTROY :DeleteObject ((HBRUSH)SetClassLong (hwnd, GCL_HBRBACKGROUND, (LONG)GetStockObject (WHITE_BRUSH))) ;for (i = 0 ; i < 3 ; i++)DeleteObject (hBrush[i]) ;DeleteObject (hBrushStatic) ;PostQuitMessage (0) ;return 0 ;}return DefWindowProc (hwnd, message, wParam, lParam) ;}LRESULT CALLBACK ScrollProc (HWND hwnd, UINT message,WPARAM wParam, LPARAM lParam){int id = GetWindowLong (hwnd, GWL_ID) ;switch (message){case WM_KEYDOWN :if (wParam == VK_TAB)SetFocus (GetDlgItem (GetParent (hwnd),(id + (GetKeyState (VK_SHIFT) < 0 ? 2 : 1)) % 3)) ;break ;case WM_SETFOCUS :idFocus = id ;break ;}return CallWindowProc (OldScroll[id], hwnd, message, wParam, lParam) ; }运行结果截图:八、附录int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow){HWND hwnd ;MSG msg ;WNDCLASS wndclass ;HMENU hMenu;hMenu=LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU1));wndclass.style = CS_HREDRAW | CS_VREDRAW ;wndclass.lpfnWndProc = WndProc ;wndclass.cbClsExtra = 0 ;wndclass.cbWndExtra = 0 ;wndclass.hInstance = hInstance ;wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;wndclass.lpszMenuName = szAppName;wndclass.lpszClassName = szAppName ;if (!RegisterClass (&wndclass)){MessageBox (NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR) ;return 0 ;}hwnd = CreateWindow (szAppName, TEXT ("Desktop"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT,NULL,hMenu, hInstance, NULL) ;/* hMenu=CreateMenu();hMenuPopup=CreateMenu();AppendMenu(hMenuPopup,MF_STRING,IDM_SHOW_NORMAL,TEXT("NORMAL"));AppendMenu(hMenuPopup,MF_STRING,IDM_SHOW_REVERSE,TEXT("REVERSE"));AppendMenu(hMenu,MF_POPUP,(UINT_PTR)hMenuPopup,TEXT("SHOW"));*/ShowWindow (hwnd, iCmdShow) ;UpdateWindow (hwnd) ;while (GetMessage (&msg, NULL, 0, 0)){TranslateMessage (&msg) ;DispatchMessage (&msg) ;}return msg.wParam ;}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {static int cxClient, cyClient, cxSource, cySource ;HDC hdcClient, hdcWindow ;PAINTSTRUCT ps ;HMENU hMenu;static BOOL stretched=TRUE,iscxClientlong=TRUE;static int iSelection =ID_40001,iSelection2=ID_40007;switch (message){case WM_COMMAND:hMenu=GetMenu(hwnd);switch (LOWORD(wParam)){case ID_40001:CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ;iSelection = LOWORD (wParam) ;CheckMenuItem (hMenu, iSelection, MF_CHECKED) ;iCurrent=0;InvalidateRect (hwnd, NULL, TRUE) ;return 0;case ID_40002:CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ;iSelection = LOWORD (wParam) ;CheckMenuItem (hMenu, iSelection, MF_CHECKED) ;iCurrent=1;InvalidateRect (hwnd, NULL, TRUE) ;return 0;case ID_40003:CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ;iSelection = LOWORD (wParam) ;CheckMenuItem (hMenu, iSelection, MF_CHECKED) ;iCurrent=2;InvalidateRect (hwnd, NULL, TRUE) ;return 0;case ID_40004:CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ;iSelection = LOWORD (wParam) ;CheckMenuItem (hMenu, iSelection, MF_CHECKED) ;iCurrent=3;InvalidateRect (hwnd, NULL, TRUE) ;return 0;case ID_40007:CheckMenuItem (hMenu, iSelection2, MF_UNCHECKED) ;iSelection2 = LOWORD (wParam) ;CheckMenuItem (hMenu, iSelection2, MF_CHECKED) ;stretched=TRUE;InvalidateRect (hwnd, NULL, TRUE) ;return 0;case ID_40008:CheckMenuItem (hMenu, iSelection2, MF_UNCHECKED) ;iSelection2 = LOWORD (wParam) ;CheckMenuItem (hMenu, iSelection2, MF_CHECKED) ;stretched=FALSE;InvalidateRect(hwnd, NULL, TRUE) ;return 0;}break;case WM_SIZE:cxClient = LOWORD (lParam) ;cyClient = HIWORD (lParam) ;iscxClientlong=cxClient>(cyClient*cxDesktop/cyDesktop)?TRUE:FALSE; modeorginal[0][0]=0;modeorginal[0][1]=0;modeorginal[0][2]=cxClient;modeorginal[0][3]=cy Client;modeorginal[1][0]=cxClient;modeorginal[1][1]=0;modeorginal[1][2]=-cxClient;modeorginal[ 1][3]=cyClient;modeorginal[2][0]=0;modeorginal[2][1]=cyClient;modeorginal[2][2]=cxClient;modeorginal[2 ][3]=-cyClient;modeorginal[3][0]=cxClient;modeorginal[3][1]=cyClient;modeorginal[3][2]=-cxClient;modeo rginal[3][3]=-cyClient;return 0 ;case WM_PAINT:hdcClient = BeginPaint (hwnd, &ps) ;hdcWindow = GetWindowDC (GetDesktopWindow()) ;if(stretched){for(int i=0;i<4;i++)mode[i]=modeorginal[iCurrent][i];}else{for(int i=0;i<4;i++)mode[i]=modeorginal[iCurrent][i];switch(iCurrent){case 0:if(iscxClientlong){mode[2]=mode[3]*cxDesktop/cyDesktop; }else{mode[3]=mode[2]*cyDesktop/cxDesktop; }break;case 1:if(iscxClientlong){mode[2]=-mode[3]*cxDesktop/cyDesktop;mode[0]=-mode[2];}else{mode[3]=-mode[2]*cyDesktop/cxDesktop; }break;case 2:if(iscxClientlong){mode[2]=-mode[3]*cxDesktop/cyDesktop; }else{mode[3]=-mode[2]*cyDesktop/cxDesktop;mode[1]=-mode[3];}break;case 3:if(iscxClientlong){mode[2]=mode[3]*cxDesktop/cyDesktop;mode[0]=-mode[2];}else{mode[3]=mode[2]*cyDesktop/cxDesktop;mode[1]=-mode[3];}break;}显示方式更改为等比:31。