SKY外语计算机学习
标题:
c语言用windows api实现逃跑按钮源码
[打印本页]
作者:
kuanger
时间:
2012-7-31 14:06
标题:
c语言用windows api实现逃跑按钮源码
本帖最后由 sky_yx 于 2015-12-30 14:18 编辑
// RunButton.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
#define IDC_BUTTON 1 // 按钮ID
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// 自定义全局变量
BOOL isVisible; // 按钮是否可见
WNDPROC OldButton; // 按钮默认处理过程
int cxButton, cyButton, cxClient, cyClient; // 按钮宽高,客户区宽高
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK Button(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_RUNBUTTON, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_RUNBUTTON);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_RUNBUTTON);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_RUNBUTTON;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxBorder, cyBorder; // 边框尺寸
static HWND hWndButton; // 按钮句柄
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_CREATE:
// 创建按钮
hWndButton = CreateWindow(TEXT("Button"), TEXT("跑!跑!跑!"),
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
0, 0, 0, 0, hWnd,
(HMENU)IDC_BUTTON, hInst, NULL);
// 变量赋初值
isVisible = TRUE;
cxButton = 200;
cyButton = 60;
// 得到边框的尺寸,为了确定窗口最小尺寸
RECT rt, rect;
GetClientRect(hWnd, &rt);
GetWindowRect(hWnd, &rect);
cxBorder = rect.right - rect.left - rt.right;
cyBorder = rect.bottom - rect.top - rt.bottom;
// 修改按钮消息处理过程
OldButton = (WNDPROC)SetWindowLong(hWndButton, GWL_WNDPROC, (LONG)Button);
break;
// 修改窗口最小宽度和高度,避免出现按钮显示不全的现象。
case WM_GETMINMAXINFO:
PMINMAXINFO mmInfo;
mmInfo = (PMINMAXINFO)lParam;
mmInfo->ptMinTrackSize.x = cxButton + cxBorder;
mmInfo->ptMinTrackSize.y = cyButton + cyBorder;
break;
/* case WM_SIZING:
PRECT rect;
rect = (PRECT)lParam;
if ((rect->right - rect->left) < cxButton || (rect->bottom - rect->top) < cyButton)
{
rect->right = max(cxButton, rect->right - rect->left) + rect->left;
rect->bottom = max(cyButton, rect->bottom - rect->top) + rect->top;
}
break;*/
case WM_SIZE:
// 如果按钮为隐藏状态,将按钮显示
if (!isVisible)
{
ShowWindow(hWndButton, SW_SHOW);
isVisible = TRUE;
}
// 随即移动按钮位置,位置最小值为1,以免出现除数为0的情况。
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
MoveWindow(hWndButton, rand() % max(cxClient - cxButton, 1),
rand() % max(cyClient - cyButton, 1), cxButton,
cyButton, TRUE);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
/* case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
EndPaint(hWnd, &ps);
break;*/
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
// 按钮消息处理过程
LRESULT CALLBACK Button(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
// 如果被按到了,就隐藏
case WM_LBUTTONDOWN:
ShowWindow(hWnd, SW_HIDE);
isVisible = FALSE;
break;
// 鼠标移动到按钮上时,按钮随即移动
case WM_MOUSEMOVE:
MoveWindow(hWnd, rand() % max(cxClient - cxButton, 1),
rand() % max(cyClient - cyButton, 1), cxButton,
cyButton, TRUE);
break;
default:
break;
}
return CallWindowProc(OldButton, hWnd, message, wParam, lParam);
}
复制代码
作者:
rogan
时间:
2012-7-31 20:17
围观
作者:
稚悟
时间:
2012-8-8 19:11
本帖最后由 sky_yx 于 2015-12-30 14:18 编辑
感觉太繁琐
作者:
kuanger
时间:
2012-8-9 13:33
本帖最后由 sky_yx 于 2015-12-30 14:18 编辑
感觉太繁琐[/quote]
其实,大部分代码还是vs帮忙生成的!
作者:
流氓
时间:
2012-8-9 23:38
本帖最后由 sky_yx 于 2015-12-30 14:18 编辑
表示看不懂
欢迎光临 SKY外语计算机学习 (http://skywj.com/)
Powered by Discuz! X2.5