1.游戏的目的是在一个10*10的方框内找一个有“ok"的按钮
2.如果所选不是目标按钮(ok),则选中的按钮指示"ok"按钮的相对位置(左右上下)
3.每次开始随机产生目标按钮
源代码:
#include "time.h"
#include <windows.h>
#include "resource.h"
#include "stdlib.h"
#include "math.h"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK AboutProc(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK HelpProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE hInst;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("game") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
hInst=hInstance;
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 (LTGRAY_BRUSH) ;
wndclass.lpszMenuName = TEXT("MYMENU") ;
wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
} hwnd = CreateWindow (szAppName, TEXT ("捉迷藏"),
WS_CAPTION|WS_SYSMENU,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0,
NULL, NULL, hInstance, NULL) ;
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)
{
int aimx,aimy; //目标按钮位置
int id,idx,idy; //id为子窗口ID,idy为id个位,idx为十位
int i,j;
static HWND hwndButton[10][10]; //子窗口的句炳
int cxunit,cyunit; //子窗口大小
RECT rect;
switch(message)
{
case WM_CREATE:
GetClientRect(hwnd,&rect); //得到客户区大小
cxunit=rect.right/10; //将客户区大小分为10份作为子窗口按钮的
cyunit=rect.bottom/10; //长(cxunit)和高(cyunit)
for(i=0;i<10;i++)
for(j=1;j<10;j++)
hwndButton[i][j]=CreateWindow(TEXT("button"),TEXT(" "),
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,i*cxunit,j*cyunit,cxunit,cyunit,hwnd,(HMENU)(i*10+j),hInst,NULL); //创建子窗口作为按钮
return 0;
case WM_COMMAND:
switch(lParam)
{
case 0: //处理菜单
switch(LOWORD(wParam))
{
case IDM_START:
srand( (unsigned)time( NULL ) );//随机数发生
aimx=rand()%10; //随机得到目标子窗口id的十位
aimy=rand()%10; //随机得到目标子窗口id的个位
for(i=0;i<10;i++)
for(j=0;j<10;j++)
SetWindowText(hwndButton[i][j],TEXT(" "));//清空按钮字
break;
case IDM_EXIT:
PostQuitMessage(0);
break;
case IDM_HELP:
DialogBox(hInst,"help",hwnd,HelpProc);//说明性对话框
break;
case IDM_ABOUT:
DialogBox(hInst,"about",hwnd,AboutProc);//对话框 break;
default:
break;
}
default:
if(BN_CLICKED==HIWORD(wParam))
{
id=LOWORD(wParam);//得到子窗口id
idy=id%10; //得到子窗口id的个位
idx=(id-idy)/10; //得到子窗口id的十位
if(fabs(aimy-idy)>fabs(aimx-idx)&&(aimy-idy>0)) //判断所
SetWindowText((HWND)lParam,TEXT("右")); //选按钮与目标
if(fabs(aimy-idy)>fabs(aimx-idx)&&(aimy-idy<0)) //按钮
SetWindowText((HWND)lParam,TEXT("左")); //的位置情况
if(fabs(aimy-idy)<fabs(aimx-idx)&&(aimx-idx>0))
SetWindowText((HWND)lParam,TEXT("下"));
if(fabs(aimy-idy)<fabs(aimx-idx)&&(aimx-idx<0))
SetWindowText((HWND)lParam,TEXT("上"));
if(aimy==idy&&aimx==idx) //得到目标按钮 {SetWindowText((HWND)lParam,TEXT("OK")); MessageBeep(MB_ICONEXCLAMATION);}
}
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
BOOL CALLBACK AboutProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM
lParam)
{
switch(message)
{
case WM_INITDIALOG:
return 0;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
EndDialog(hDlg,0);
return 0;
}
break;
}
return FALSE;
}
BOOL CALLBACK HelpProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM
lParam)
{
switch(message)
{
case WM_INITDIALOG:
return 0;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
EndDialog(hDlg,0);
return 0;
}
break;
}
return FALSE;
}
对话框是一些说明性的东西,无关紧要。请大家按源代码直接编译,就知道问题了,把问题和解决的方法发表出来,谢谢。
2.如果所选不是目标按钮(ok),则选中的按钮指示"ok"按钮的相对位置(左右上下)
3.每次开始随机产生目标按钮
源代码:
#include "time.h"
#include <windows.h>
#include "resource.h"
#include "stdlib.h"
#include "math.h"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK AboutProc(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK HelpProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE hInst;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("game") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
hInst=hInstance;
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 (LTGRAY_BRUSH) ;
wndclass.lpszMenuName = TEXT("MYMENU") ;
wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
} hwnd = CreateWindow (szAppName, TEXT ("捉迷藏"),
WS_CAPTION|WS_SYSMENU,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0,
NULL, NULL, hInstance, NULL) ;
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)
{
int aimx,aimy; //目标按钮位置
int id,idx,idy; //id为子窗口ID,idy为id个位,idx为十位
int i,j;
static HWND hwndButton[10][10]; //子窗口的句炳
int cxunit,cyunit; //子窗口大小
RECT rect;
switch(message)
{
case WM_CREATE:
GetClientRect(hwnd,&rect); //得到客户区大小
cxunit=rect.right/10; //将客户区大小分为10份作为子窗口按钮的
cyunit=rect.bottom/10; //长(cxunit)和高(cyunit)
for(i=0;i<10;i++)
for(j=1;j<10;j++)
hwndButton[i][j]=CreateWindow(TEXT("button"),TEXT(" "),
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,i*cxunit,j*cyunit,cxunit,cyunit,hwnd,(HMENU)(i*10+j),hInst,NULL); //创建子窗口作为按钮
return 0;
case WM_COMMAND:
switch(lParam)
{
case 0: //处理菜单
switch(LOWORD(wParam))
{
case IDM_START:
srand( (unsigned)time( NULL ) );//随机数发生
aimx=rand()%10; //随机得到目标子窗口id的十位
aimy=rand()%10; //随机得到目标子窗口id的个位
for(i=0;i<10;i++)
for(j=0;j<10;j++)
SetWindowText(hwndButton[i][j],TEXT(" "));//清空按钮字
break;
case IDM_EXIT:
PostQuitMessage(0);
break;
case IDM_HELP:
DialogBox(hInst,"help",hwnd,HelpProc);//说明性对话框
break;
case IDM_ABOUT:
DialogBox(hInst,"about",hwnd,AboutProc);//对话框 break;
default:
break;
}
default:
if(BN_CLICKED==HIWORD(wParam))
{
id=LOWORD(wParam);//得到子窗口id
idy=id%10; //得到子窗口id的个位
idx=(id-idy)/10; //得到子窗口id的十位
if(fabs(aimy-idy)>fabs(aimx-idx)&&(aimy-idy>0)) //判断所
SetWindowText((HWND)lParam,TEXT("右")); //选按钮与目标
if(fabs(aimy-idy)>fabs(aimx-idx)&&(aimy-idy<0)) //按钮
SetWindowText((HWND)lParam,TEXT("左")); //的位置情况
if(fabs(aimy-idy)<fabs(aimx-idx)&&(aimx-idx>0))
SetWindowText((HWND)lParam,TEXT("下"));
if(fabs(aimy-idy)<fabs(aimx-idx)&&(aimx-idx<0))
SetWindowText((HWND)lParam,TEXT("上"));
if(aimy==idy&&aimx==idx) //得到目标按钮 {SetWindowText((HWND)lParam,TEXT("OK")); MessageBeep(MB_ICONEXCLAMATION);}
}
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
BOOL CALLBACK AboutProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM
lParam)
{
switch(message)
{
case WM_INITDIALOG:
return 0;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
EndDialog(hDlg,0);
return 0;
}
break;
}
return FALSE;
}
BOOL CALLBACK HelpProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM
lParam)
{
switch(message)
{
case WM_INITDIALOG:
return 0;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
EndDialog(hDlg,0);
return 0;
}
break;
}
return FALSE;
}
对话框是一些说明性的东西,无关紧要。请大家按源代码直接编译,就知道问题了,把问题和解决的方法发表出来,谢谢。
解决方案 »
- 如何将电脑屏幕保存为一张图片?
- socket超级疑难?
- 如何在程序中调用其它的可执行文件?
- 请达人指教,什么叫协议栈
- 讨论:IBM笔记本上有个一键恢复功能,谈谈算法
- 请问:为什么可以给一般对话框上的按钮添加位图,在CDialogBar上却不行?如何在CDialogBar上的按钮上显示位图?谢谢回复。
- 这个问题都问到我没有分了
- PhotoShop 中的图像变形算法?
- 救命呀!这段代码有什么问题,为什么说XXXXXX不能访问00000000的内存,附原码!!!
- WinMain函数居然在dll里
- 为什么ASSERT(m_pFindDialog == NULL);运行时会报错??
- 为什么在XP下改编辑框的属性有时成功,有时不成功!
2.如果所选不是目标按钮(ok),则选中的按钮指示"ok"按钮的相对位置(左右上下)
3.每次开始随机产生目标按钮
源代码:
#include "time.h"
#include <windows.h>
#include "resource.h"
#include "stdlib.h"
#include "math.h"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK AboutProc(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK HelpProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE hInst;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("game") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
hInst=hInstance;
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 (LTGRAY_BRUSH) ;
wndclass.lpszMenuName = TEXT("MYMENU") ;
wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
} hwnd = CreateWindow (szAppName, TEXT ("捉迷藏"),
WS_CAPTION|WS_SYSMENU,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0,
NULL, NULL, hInstance, NULL) ;
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)
{
int aimx,aimy; //目标按钮位置
int id,idx,idy; //id为子窗口ID,idy为id个位,idx为十位
int i,j;
static HWND hwndButton[10][10]; //子窗口的句炳
int cxunit,cyunit; //子窗口大小
RECT rect;
switch(message)
{
case WM_CREATE:
GetClientRect(hwnd,&rect); //得到客户区大小
cxunit=rect.right/10; //将客户区大小分为10份作为子窗口按钮的
cyunit=rect.bottom/10; //长(cxunit)和高(cyunit)
for(i=0;i<10;i++)
for(j=1;j<10;j++)
hwndButton[i][j]=CreateWindow(TEXT("button"),TEXT(" "),
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,i*cxunit,j*cyunit,cxunit,cyunit,hwnd,(HMENU)(i*10+j),hInst,NULL); //创建子窗口作为按钮
return 0;
case WM_COMMAND:
switch(lParam)
{
case 0: //处理菜单
switch(LOWORD(wParam))
{
case IDM_START:
srand( (unsigned)time( NULL ) );//随机数发生
aimx=rand()%10; //随机得到目标子窗口id的十位
aimy=rand()%10; //随机得到目标子窗口id的个位
for(i=0;i<10;i++)
for(j=0;j<10;j++)
SetWindowText(hwndButton[i][j],TEXT(" "));//清空按钮字
break;
case IDM_EXIT:
PostQuitMessage(0);
break;
case IDM_HELP:
DialogBox(hInst,"help",hwnd,HelpProc);//说明性对话框
break;
case IDM_ABOUT:
DialogBox(hInst,"about",hwnd,AboutProc);//对话框 break;
default:
break;
}
default:
if(BN_CLICKED==HIWORD(wParam))
{
id=LOWORD(wParam);//得到子窗口id
idy=id%10; //得到子窗口id的个位
idx=(id-idy)/10; //得到子窗口id的十位
if(fabs(aimy-idy)>fabs(aimx-idx)&&(aimy-idy>0)) //判断所
SetWindowText((HWND)lParam,TEXT("右")); //选按钮与目标
if(fabs(aimy-idy)>fabs(aimx-idx)&&(aimy-idy<0)) //按钮
SetWindowText((HWND)lParam,TEXT("左")); //的位置情况
if(fabs(aimy-idy)<fabs(aimx-idx)&&(aimx-idx>0))
SetWindowText((HWND)lParam,TEXT("下"));
if(fabs(aimy-idy)<fabs(aimx-idx)&&(aimx-idx<0))
SetWindowText((HWND)lParam,TEXT("上"));
if(aimy==idy&&aimx==idx) //得到目标按钮 {SetWindowText((HWND)lParam,TEXT("OK")); MessageBeep(MB_ICONEXCLAMATION);}
}
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
BOOL CALLBACK AboutProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM
lParam)
{
switch(message)
{
case WM_INITDIALOG:
return 0;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
EndDialog(hDlg,0);
return 0;
}
break;
}
return FALSE;
}
BOOL CALLBACK HelpProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM
lParam)
{
switch(message)
{
case WM_INITDIALOG:
return 0;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
EndDialog(hDlg,0);
return 0;
}
break;
}
return FALSE;
}
对话框是一些说明性的东西,无关紧要。请大家按源代码直接编译,就知道问题了,把问题和解决的方法发表出来,谢谢。
I think it is good.
#define IDM_START 40001
#define IDM_EXIT 40002
#define IDM_HELP 40003
#define IDM_ABOUT 40004// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_value 102
#define _APS_NEXT_COMMAND_value 40005
#define _APS_NEXT_CONTROL_value 1000
#define _APS_NEXT_SYMED_value 101
#endif
#endif
resource.rc/Microsoft Developer Studio generated resource script.
//
#include "resource.h"#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS/////////////////////////////////////////////////////////////////////////////
// Chinese (P.R.C.) resources#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)
#ifdef _WIN32
LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED
#pragma code_page(936)
#endif //_WIN32#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//1 TEXTINCLUDE DISCARDABLE
BEGIN
"resource.h\0"
END2 TEXTINCLUDE DISCARDABLE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END3 TEXTINCLUDE DISCARDABLE
BEGIN
"\r\n"
"\0"
END#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Menu
//MYMENU MENU DISCARDABLE
BEGIN
POPUP "游戏"
BEGIN
MENUITEM "开始", IDM_START
MENUITEM "结束", IDM_EXIT
END
POPUP "说明"
BEGIN
MENUITEM "规则", IDM_HELP
MENUITEM "关于", IDM_ABOUT
END
END#endif // Chinese (P.R.C.) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED