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;
}
对话框是一些说明性的东西,无关紧要。请大家按源代码直接编译,就知道问题了,把问题和解决的方法发表出来,谢谢。
解决方案 »
- 自绘的CListBox控件,双击获得某行的内容,怎么全是乱码?
- 知道菜单的句柄,菜单的某一项的caption,怎么得到这一项菜单的ID
- WriteFile("\r\n")无法换行
- 工作线程数据丢失
- 使用孙鑫老师的例子有误(内存泄露)
- recv时,如果不知道要接收的数据长度,这个时候要如何处理啊?
- 如何给CInternetSession的OpenURL方法设置超时?
- IMPLEMENT_DYNAMIC的问题
- 使用ado的recordset打开表,遍历表中内容如何判断是否为NULL?
- 怎样在一个Radio控件上绑定一个变量????
- 为什么会在Release版编译的时候出这种错?
- 怎样用SendMessage函数向窗口发一个组合键消息(如Alt + F)?
源代码:
#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;
}
对话框是一些说明性的东西,无关紧要。请大家按源代码直接编译,就知道问题了,把问题和解决的方法发表出来,谢谢。
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM)
就可以了。