鱼C论坛

 找回密码
 立即注册
查看: 1904|回复: 1

windows程序设计一个小球弹撞问题

[复制链接]
发表于 2014-10-18 20:49:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 RSNrsn 于 2014-10-18 20:51 编辑

代码如下:
  1. // Bounce.cpp : Defines the entry point for the application.
  2. //

  3. #include "stdafx.h"
  4. #include "resource.h"

  5. #define MAX_LOADSTRING 100

  6. // Global Variables:
  7. HINSTANCE hInst;                                                                // current instance
  8. TCHAR szTitle[MAX_LOADSTRING];                                                                // The title bar text
  9. TCHAR szWindowClass[MAX_LOADSTRING];                                                                // The title bar text

  10. // Foward declarations of functions included in this code module:
  11. ATOM                                MyRegisterClass(HINSTANCE hInstance);
  12. BOOL                                InitInstance(HINSTANCE, int);
  13. LRESULT CALLBACK        WndProc(HWND, UINT, WPARAM, LPARAM);
  14. LRESULT CALLBACK        About(HWND, UINT, WPARAM, LPARAM);
  15. static int cxClient,cyClient,xCenter,yCenter,xTotal,yTotal,xR,yR,xMove,yMove;
  16. int APIENTRY WinMain(HINSTANCE hInstance,
  17.                      HINSTANCE hPrevInstance,
  18.                      LPSTR     lpCmdLine,
  19.                      int       nCmdShow)
  20. {
  21.          // TODO: Place code here.
  22.         MSG msg;
  23.         HACCEL hAccelTable;

  24.         // Initialize global strings
  25.         LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  26.         LoadString(hInstance, IDC_BOUNCE, szWindowClass, MAX_LOADSTRING);
  27.         MyRegisterClass(hInstance);

  28.         // Perform application initialization:
  29.         if (!InitInstance (hInstance, nCmdShow))
  30.         {
  31.                 return FALSE;
  32.         }

  33.         hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_BOUNCE);

  34.         // Main message loop:
  35.         while (GetMessage(&msg, NULL, 0, 0))
  36.         {
  37.                 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  38.                 {
  39.                         TranslateMessage(&msg);
  40.                         DispatchMessage(&msg);
  41.                 }
  42.         }

  43.         return msg.wParam;
  44. }



  45. //
  46. //  FUNCTION: MyRegisterClass()
  47. //
  48. //  PURPOSE: Registers the window class.
  49. //
  50. //  COMMENTS:
  51. //
  52. //    This function and its usage is only necessary if you want this code
  53. //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
  54. //    function that was added to Windows 95. It is important to call this function
  55. //    so that the application will get 'well formed' small icons associated
  56. //    with it.
  57. //
  58. ATOM MyRegisterClass(HINSTANCE hInstance)
  59. {
  60.         WNDCLASSEX wcex;

  61.         wcex.cbSize = sizeof(WNDCLASSEX);

  62.         wcex.style                        = CS_HREDRAW | CS_VREDRAW;
  63.         wcex.lpfnWndProc        = (WNDPROC)WndProc;
  64.         wcex.cbClsExtra                = 0;
  65.         wcex.cbWndExtra                = 0;
  66.         wcex.hInstance                = hInstance;
  67.         wcex.hIcon                        = LoadIcon(hInstance, (LPCTSTR)IDI_BOUNCE);
  68.         wcex.hCursor                = LoadCursor(NULL, IDC_ARROW);
  69.         wcex.hbrBackground        = (HBRUSH)(COLOR_WINDOW+1);
  70.         wcex.lpszMenuName        = (LPCSTR)IDC_BOUNCE;
  71.         wcex.lpszClassName        = szWindowClass;
  72.         wcex.hIconSm                = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

  73.         return RegisterClassEx(&wcex);
  74. }

  75. //
  76. //   FUNCTION: InitInstance(HANDLE, int)
  77. //
  78. //   PURPOSE: Saves instance handle and creates main window
  79. //
  80. //   COMMENTS:
  81. //
  82. //        In this function, we save the instance handle in a global variable and
  83. //        create and display the main program window.
  84. //
  85. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  86. {
  87.    HWND hWnd;

  88.    hInst = hInstance; // Store instance handle in our global variable

  89.    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  90.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

  91.    if (!hWnd)
  92.    {
  93.       return FALSE;
  94.    }

  95.    ShowWindow(hWnd, nCmdShow);
  96.    UpdateWindow(hWnd);

  97.    return TRUE;
  98. }

  99. //
  100. //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
  101. //
  102. //  PURPOSE:  Processes messages for the main window.
  103. //
  104. //  WM_COMMAND        - process the application menu
  105. //  WM_PAINT        - Paint the main window
  106. //  WM_DESTROY        - post a quit message and return
  107. //
  108. //
  109. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  110. {
  111.         int wmId, wmEvent;
  112.         PAINTSTRUCT ps;
  113.         HDC hdc,hdcMem;
  114.         static int i=0;
  115.         static HBITMAP bitmap;
  116.         HBRUSH brush;
  117.         switch (message)
  118.         {
  119.                 case WM_CREATE:
  120.                         SetTimer(hWnd,1,100,NULL);
  121.                         break;
  122.                 case WM_SIZE:
  123.                         
  124.                         xCenter = (cxClient = LOWORD(lParam))/2;
  125.                         yCenter = (cyClient = HIWORD(lParam))/2;
  126.                         xR = 20;
  127.                         yR = 20;
  128.                         xMove = xR/2;
  129.                         yMove = yR/2;
  130.                         xTotal = 2*(xR + xMove);
  131.                         yTotal = 2*(yR + yMove);
  132.                         hdc = GetDC(hWnd);
  133.                         bitmap = CreateCompatibleBitmap(hdc,xTotal,yTotal);
  134.                         hdcMem = CreateCompatibleDC(NULL);
  135.                         ReleaseDC(hWnd,hdc);
  136.                         SelectObject(hdcMem,bitmap);
  137.                         Rectangle( hdcMem,-1,-1,xTotal+1,yTotal+1);
  138.                         brush = CreateHatchBrush(HS_DIAGCROSS,0L);
  139.                         SelectObject(hdcMem,brush);
  140.                         SetBkColor(hdcMem, RGB(255,0,255));
  141.                         Ellipse(hdcMem,xMove,yMove,xTotal-xMove,yTotal-yMove);
  142.                         DeleteObject(hdcMem);
  143.                
  144.                         break;
  145.                 case WM_COMMAND:
  146.                         wmId    = LOWORD(wParam);
  147.                         wmEvent = HIWORD(wParam);
  148.                         // Parse the menu selections:
  149.                         switch (wmId)
  150.                         {
  151.                                 case IDM_ABOUT:
  152.                                    DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
  153.                                    break;
  154.                                 case IDM_EXIT:
  155.                                    DestroyWindow(hWnd);
  156.                                    break;
  157.                                 default:
  158.                                    return DefWindowProc(hWnd, message, wParam, lParam);
  159.                         }
  160.                         break;
  161.                 case WM_TIMER:
  162.                         hdc = GetDC(hWnd);
  163.                         if(!bitmap)
  164.                                 break;
  165.                         hdcMem = CreateCompatibleDC(hdc);
  166.                         SelectObject(hdcMem, bitmap);
  167.                         BitBlt(hdc, xCenter - xTotal / 2, yCenter - yTotal / 2, xTotal, yTotal,
  168.                                 hdcMem, 0,0,SRCCOPY);
  169.                         
  170.                         xCenter += xMove;
  171.                         yCenter += yMove;
  172.                         if (((xCenter+xR/2) >= cxClient)||((xCenter-xR/2) <= 0))
  173.                         {
  174.                                 xMove = -xMove;
  175.                         }
  176.                         if ((yCenter+yR/2 >=cyClient)||(yCenter-yR/2 <= 0))
  177.                         {
  178.                                 yMove = -yMove;
  179.                         }
  180.                         //这行代码删掉就不行为啥呢?
  181.                         DeleteObject(hdcMem);

  182.                         ReleaseDC(hWnd,hdc);

  183.                                 break;
  184.                 case WM_PAINT:
  185.                         hdc = BeginPaint(hWnd, &ps);
  186.                         // TODO: Add any drawing code here...
  187.                         
  188.                         EndPaint(hWnd, &ps);
  189.                         break;
  190.                 case WM_DESTROY:
  191.                         PostQuitMessage(0);
  192.                         break;
  193.                 default:
  194.                         return DefWindowProc(hWnd, message, wParam, lParam);
  195.    }
  196.    return 0;
  197. }

  198. // Mesage handler for about box.
  199. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  200. {
  201.         switch (message)
  202.         {
  203.                 case WM_INITDIALOG:
  204.                                 return TRUE;

  205.                 case WM_COMMAND:
  206.                         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  207.                         {
  208.                                 EndDialog(hDlg, LOWORD(wParam));
  209.                                 return TRUE;
  210.                         }
  211.                         break;
  212.         }
  213.     return FALSE;
  214. }
复制代码


为啥注释这一行就运行不出效果。
如图C:\Users\Administrator\Desktop
捕获.JPG
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-8-14 08:35:46 | 显示全部楼层
把问题求助顶起来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-16 04:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表