设为首页收藏本站

SKY外语、计算机论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 7689|回复: 4

[C] c语言用windows api实现逃跑按钮源码

[复制链接]

6

主题

0

好友

272

积分

中级会员

Rank: 3Rank: 3

性别
保密

最佳新人 活跃会员 热心会员 灌水之王 论坛元老

发表于 2012-7-31 14:06:51 |显示全部楼层
本帖最后由 sky_yx 于 2015-12-30 14:18 编辑
  1. // RunButton.cpp : Defines the entry point for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h"
  5. #define MAX_LOADSTRING 100
  6. #define IDC_BUTTON 1                // 按钮ID
  7. // Global Variables:
  8. HINSTANCE hInst;                                                                // current instance
  9. TCHAR szTitle[MAX_LOADSTRING];                                                                // The title bar text
  10. TCHAR szWindowClass[MAX_LOADSTRING];                                                                // The title bar text
  11. // 自定义全局变量
  12. BOOL isVisible;                                                                        // 按钮是否可见
  13. WNDPROC OldButton;                                                                                        // 按钮默认处理过程
  14. int cxButton, cyButton, cxClient, cyClient;                                                        // 按钮宽高,客户区宽高
  15. // Foward declarations of functions included in this code module:
  16. ATOM                                MyRegisterClass(HINSTANCE hInstance);
  17. BOOL                                InitInstance(HINSTANCE, int);
  18. LRESULT CALLBACK        WndProc(HWND, UINT, WPARAM, LPARAM);
  19. LRESULT CALLBACK        About(HWND, UINT, WPARAM, LPARAM);
  20. LRESULT CALLBACK        Button(HWND, UINT, WPARAM, LPARAM);
  21. int APIENTRY WinMain(HINSTANCE hInstance,
  22.                      HINSTANCE hPrevInstance,
  23.                      LPSTR     lpCmdLine,
  24.                      int       nCmdShow)
  25. {
  26.          // TODO: Place code here.
  27.         MSG msg;
  28.         HACCEL hAccelTable;
  29.         // Initialize global strings
  30.         LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  31.         LoadString(hInstance, IDC_RUNBUTTON, szWindowClass, MAX_LOADSTRING);
  32.         MyRegisterClass(hInstance);
  33.         // Perform application initialization:
  34.         if (!InitInstance (hInstance, nCmdShow))
  35.         {
  36.                 return FALSE;
  37.         }
  38.         hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_RUNBUTTON);
  39.         // Main message loop:
  40.         while (GetMessage(&msg, NULL, 0, 0))
  41.         {
  42.                 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  43.                 {
  44.                         TranslateMessage(&msg);
  45.                         DispatchMessage(&msg);
  46.                 }
  47.         }
  48.         return msg.wParam;
  49. }
  50. //
  51. //  FUNCTION: MyRegisterClass()
  52. //
  53. //  PURPOSE: Registers the window class.
  54. //
  55. //  COMMENTS:
  56. //
  57. //    This function and its usage is only necessary if you want this code
  58. //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
  59. //    function that was added to Windows 95. It is important to call this function
  60. //    so that the application will get 'well formed' small icons associated
  61. //    with it.
  62. //
  63. ATOM MyRegisterClass(HINSTANCE hInstance)
  64. {
  65.         WNDCLASSEX wcex;
  66.         wcex.cbSize = sizeof(WNDCLASSEX);
  67.         wcex.style                        = CS_HREDRAW | CS_VREDRAW;
  68.         wcex.lpfnWndProc        = (WNDPROC)WndProc;
  69.         wcex.cbClsExtra                = 0;
  70.         wcex.cbWndExtra                = 0;
  71.         wcex.hInstance                = hInstance;
  72.         wcex.hIcon                        = LoadIcon(hInstance, (LPCTSTR)IDI_RUNBUTTON);
  73.         wcex.hCursor                = LoadCursor(NULL, IDC_ARROW);
  74.         wcex.hbrBackground        = (HBRUSH)(COLOR_WINDOW+1);
  75.         wcex.lpszMenuName        = (LPCSTR)IDC_RUNBUTTON;
  76.         wcex.lpszClassName        = szWindowClass;
  77.         wcex.hIconSm                = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
  78.         return RegisterClassEx(&wcex);
  79. }
  80. //
  81. //   FUNCTION: InitInstance(HANDLE, int)
  82. //
  83. //   PURPOSE: Saves instance handle and creates main window
  84. //
  85. //   COMMENTS:
  86. //
  87. //        In this function, we save the instance handle in a global variable and
  88. //        create and display the main program window.
  89. //
  90. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  91. {
  92.    HWND hWnd;
  93.    hInst = hInstance; // Store instance handle in our global variable
  94.    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  95.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  96.    if (!hWnd)
  97.    {
  98.       return FALSE;
  99.    }
  100.    ShowWindow(hWnd, nCmdShow);
  101.    UpdateWindow(hWnd);
  102.    return TRUE;
  103. }
  104. //
  105. //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
  106. //
  107. //  PURPOSE:  Processes messages for the main window.
  108. //
  109. //  WM_COMMAND        - process the application menu
  110. //  WM_PAINT        - Paint the main window
  111. //  WM_DESTROY        - post a quit message and return
  112. //
  113. //
  114. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  115. {
  116.         static int cxBorder, cyBorder; // 边框尺寸
  117.         static HWND hWndButton; // 按钮句柄
  118.         int wmId, wmEvent;
  119.         PAINTSTRUCT ps;
  120.         HDC hdc;
  121.         TCHAR szHello[MAX_LOADSTRING];
  122.         LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
  123.         switch (message)
  124.         {
  125.                 case WM_CREATE:
  126.                         // 创建按钮
  127.                         hWndButton = CreateWindow(TEXT("Button"), TEXT("跑!跑!跑!"),
  128.                                 WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
  129.                                 0, 0, 0, 0, hWnd,
  130.                                 (HMENU)IDC_BUTTON, hInst, NULL);
  131.                         // 变量赋初值
  132.                         isVisible = TRUE;
  133.                         cxButton = 200;
  134.                         cyButton = 60;
  135.                         // 得到边框的尺寸,为了确定窗口最小尺寸
  136.                         RECT rt, rect;
  137.                         GetClientRect(hWnd, &rt);
  138.                         GetWindowRect(hWnd, &rect);
  139.                         cxBorder = rect.right - rect.left - rt.right;
  140.                         cyBorder = rect.bottom - rect.top - rt.bottom;
  141.                         // 修改按钮消息处理过程
  142.                         OldButton = (WNDPROC)SetWindowLong(hWndButton, GWL_WNDPROC, (LONG)Button);
  143.                         
  144.                         break;
  145.                 // 修改窗口最小宽度和高度,避免出现按钮显示不全的现象。
  146.                 case WM_GETMINMAXINFO:
  147.                         PMINMAXINFO mmInfo;
  148.                         mmInfo = (PMINMAXINFO)lParam;
  149.                         mmInfo->ptMinTrackSize.x = cxButton + cxBorder;
  150.                         mmInfo->ptMinTrackSize.y = cyButton + cyBorder;
  151.                         break;
  152. /*                case WM_SIZING:
  153.                         PRECT rect;
  154.                         rect = (PRECT)lParam;
  155.                         if ((rect->right - rect->left) < cxButton || (rect->bottom - rect->top) < cyButton)
  156.                         {
  157.                                 rect->right = max(cxButton, rect->right - rect->left) + rect->left;
  158.                                 rect->bottom = max(cyButton, rect->bottom - rect->top) + rect->top;
  159.                         }
  160.                         break;*/
  161.                 case WM_SIZE:
  162.                         // 如果按钮为隐藏状态,将按钮显示
  163.                         if (!isVisible)
  164.                         {
  165.                                 ShowWindow(hWndButton, SW_SHOW);
  166.                                 isVisible = TRUE;
  167.                         }
  168.                         // 随即移动按钮位置,位置最小值为1,以免出现除数为0的情况。
  169.                         cxClient = LOWORD(lParam);
  170.                         cyClient = HIWORD(lParam);
  171.                         MoveWindow(hWndButton, rand() % max(cxClient - cxButton, 1),
  172.                                 rand() % max(cyClient - cyButton, 1), cxButton,
  173.                                 cyButton, TRUE);
  174.                         break;
  175.                 case WM_COMMAND:
  176.                         wmId    = LOWORD(wParam);
  177.                         wmEvent = HIWORD(wParam);
  178.                         // Parse the menu selections:
  179.                         switch (wmId)
  180.                         {
  181.                                 case IDM_ABOUT:
  182.                                    DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
  183.                                    break;
  184.                                 case IDM_EXIT:
  185.                                    DestroyWindow(hWnd);
  186.                                    break;
  187.                                 default:
  188.                                    return DefWindowProc(hWnd, message, wParam, lParam);
  189.                         }
  190.                         break;
  191. /*                case WM_PAINT:
  192.                         hdc = BeginPaint(hWnd, &ps);
  193.                         // TODO: Add any drawing code here...
  194.                         RECT rt;
  195.                         GetClientRect(hWnd, &rt);
  196.                         EndPaint(hWnd, &ps);
  197.                         break;*/
  198.                 case WM_DESTROY:
  199.                         PostQuitMessage(0);
  200.                         break;
  201.                 default:
  202.                         return DefWindowProc(hWnd, message, wParam, lParam);
  203.    }
  204.    return 0;
  205. }
  206. // Mesage handler for about box.
  207. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  208. {
  209.         switch (message)
  210.         {
  211.                 case WM_INITDIALOG:
  212.                                 return TRUE;
  213.                 case WM_COMMAND:
  214.                         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  215.                         {
  216.                                 EndDialog(hDlg, LOWORD(wParam));
  217.                                 return TRUE;
  218.                         }
  219.                         break;
  220.         }
  221.     return FALSE;
  222. }
  223. // 按钮消息处理过程
  224. LRESULT CALLBACK Button(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  225. {
  226.         switch(message)
  227.         {
  228.         // 如果被按到了,就隐藏
  229.         case WM_LBUTTONDOWN:
  230.                 ShowWindow(hWnd, SW_HIDE);
  231.                 isVisible = FALSE;
  232.                 break;
  233.         // 鼠标移动到按钮上时,按钮随即移动
  234.         case WM_MOUSEMOVE:
  235.                 MoveWindow(hWnd, rand() % max(cxClient - cxButton, 1),
  236.                         rand() % max(cyClient - cyButton, 1), cxButton,
  237.                         cyButton, TRUE);
  238.                 break;
  239.         default:
  240.                 break;
  241.         }
  242.         return CallWindowProc(OldButton, hWnd, message, wParam, lParam);
  243. }
复制代码


65

主题

3

好友

739

积分

超级版主

Rank: 8Rank: 8

自我介绍
新年第一天据说有雨,全民齐赏日出的计划恐要泡汤。”宋仁宗拍着包拯的肩,“朕决定把你悬挂在城门上。”“但微臣额上的不是太阳是月亮啊!”“没事,挂久一点就会升级成太阳
生肖
星座
狮子座
性别

最佳新人 活跃会员 热心会员 推广达人 宣传达人 灌水之王 突出贡献 优秀版主 论坛元老

发表于 2012-7-31 20:17:46 |显示全部楼层
围观
回复

使用道具 评分 举报

0

主题

0

好友

110

积分

注册会员

Rank: 2

性别
保密
发表于 2012-8-8 19:11:06 |显示全部楼层
本帖最后由 sky_yx 于 2015-12-30 14:18 编辑

感觉太繁琐

回复

使用道具 评分 举报

6

主题

0

好友

272

积分

中级会员

Rank: 3Rank: 3

性别
保密

最佳新人 活跃会员 热心会员 灌水之王 论坛元老

发表于 2012-8-9 13:33:45 |显示全部楼层
本帖最后由 sky_yx 于 2015-12-30 14:18 编辑

感觉太繁琐[/quote]
其实,大部分代码还是vs帮忙生成的!

回复

使用道具 评分 举报

12

主题

5

好友

425

积分

中级会员

Rank: 3Rank: 3

生肖
星座
摩羯座
性别

最佳新人 活跃会员 灌水之王 论坛元老

发表于 2012-8-9 23:38:10 |显示全部楼层
本帖最后由 sky_yx 于 2015-12-30 14:18 编辑

表示看不懂

回复

使用道具 评分 举报

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


手机版|SKY外语计算机学习 ( 粤ICP备12031577 )    

GMT+8, 2024-3-29 01:32 , Processed in 0.144550 second(s), 29 queries .

回顶部