API(乱七八糟)
windows程序設(shè)計
API:
創(chuàng)建窗口:CreateWindow()
函數(shù)功能:該函數(shù)創(chuàng)建一個重疊式窗口、彈出式窗口或子窗口。它指定窗口類,窗口標(biāo)題,窗口風(fēng)格,以及窗口的初始位置及大小(可選的)。該函數(shù)也指定該窗口的父窗口或所屬窗口(如果存在的話),及窗口的菜單。若要使用除CreateWindow函數(shù)支持的風(fēng)格外的擴展風(fēng)格,則使用CreateWindowEx函數(shù)代替CreateWindow函數(shù)。
函數(shù)原型:HWND CreateWindow(LPCTSTR lpClassName,LPCTSTR lpWindowName,DWORD dwStyle,int x,int y,int nWidth,int nHeight,HWND hWndParent,HMENU hMenu,HANDLE hlnstance,LPVOID lpParam);
hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPED | \
WS_CAPTION | \
WS_SYSMENU | \
WS_MINIMIZEBOX, // window style
x_position, // initial x position
y_position, // initial y position
x_size, // initial x size
y_size, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
API:讀取屏幕大小(不包括任務(wù)欄)
int width = GetSystemMetrics(SM_CXFULLSCREEN;
int heigh = GetSystemMetrics(SM_CYFULLSCREEN);
//設(shè)置窗口位置和大小
void set_position_size(int * p_x_position, int * p_y_position, int * p_x_size, int * p_y_size)
{
int width = GetSystemMetrics(SM_CXFULLSCREEN);//讀取屏幕大小
int heigh = GetSystemMetrics(SM_CYFULLSCREEN);
* p_x_position = (int)width/3;
* p_y_position =(int)heigh/5;
* p_x_size = (int)width/3;
* p_y_size = (int)width/3;
}
API:GetSystemMetrics()
int frame_width = GetSystemMetrics(SM_CXSIZEFRAME); //邊框?qū)挾?/span>
int caption_width = GetSystemMetrics(SM_CYCAPTION); //標(biāo)題欄寬度
int menu_high = GetSystemMetrics(SM_CYMENU); //菜單高度
畫矩形:Rectangle(hdc, x1, y1, x2, y2);
畫直線:
MoveToEx是用來移動當(dāng)前畫筆的位置,LineTo是用來畫直線的函數(shù),其實在計算機圖形里的直線顯示是使用光柵圖形學(xué)里的原理。
函數(shù)MoveToEx和LineTo聲明如下:
WINGDIAPI BOOL WINAPI MoveToEx( __in HDC hdc, __in int x, __in int y, __out_opt LPPOINT lppt);
hdc是當(dāng)前設(shè)備的句柄。
x是X軸的位置,水平方向,一般原點是在屏幕左上角的位置。
y是Y軸的位置,垂直方向。
lppt是移動前的坐標(biāo)位置。
WINGDIAPI BOOL WINAPI LineTo( __in HDC hdc, __in int x, __in int y);
hdc是當(dāng)前設(shè)備的句柄。
x是X軸的位置,水平方向,一般原點是在屏幕左上角的位置。
y是Y軸的位置,垂直方向。
WINGDIAPI HPEN WINAPI CreatePen( __in int iStyle, __in int cWidth, __in
COLORREF color);
iStyle 是畫筆的類型,比如是實線,還是虛線等等。
cWidth 是線的寬度。
color 是線的顏色。
//備份:
//Main.cpp
/*------------------------------------------------------------
HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
(c) Charles Petzold, 1998
------------------------------------------------------------*/
#include <windows.h>
#include <stdio.h>
#include "NumMines.h"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
WINGDIAPI BOOL WINAPI MoveToEx(HDC hdc, int x, int y, LPPOINT lppt);//移動當(dāng)前畫筆的位置
WINGDIAPI BOOL WINAPI LineTo(HDC hdc, int x, int y);//用來畫直線的函數(shù)
WINGDIAPI HPEN WINAPI CreatePen(int iStyle, int cWidth, COLORREF color);
//自定義函數(shù)
void paint_map(HWND hwnd);
BOOL DrawGrid(HDC hdc, int x1, int y1, int x2, int y2);
//設(shè)置窗口位置和大小
void set_position_size(int * p_x_position, int * p_y_position, int * p_x_size, int * p_y_size);
void left_key(HWND hwnd);
void DrawRec(HWND hwnd, HDC hdc, int i, int j);
//全局變量
char map[MAX_X][MAX_Y];
int m = 10, n = 10;
int Global_x[MAX_X], Global_y[MAX_Y];
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
int x_position, y_position, x_size, y_size;
set_position_size(&x_position, &y_position, &x_size, &y_size);
init();//畫完地圖,再初始化數(shù)組(地雷分布)
static TCHAR szAppName[] = TEXT ("MainWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
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 (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
/*創(chuàng)建默認(rèn)窗口
hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
*/
hwnd = CreateWindow (szAppName, // window class name
TEXT ("掃雷游戲——The ClearMines Game"), // window caption
WS_OVERLAPPED | \
WS_CAPTION | \
WS_SYSMENU | \
WS_MINIMIZEBOX, // window style
x_position, // initial x position
y_position, // initial y position
x_size, // initial x size
y_size, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
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)
{
switch (message)
{
case WM_PAINT:
paint_map(hwnd);
return 0 ;
case WM_LBUTTONDOWN: //單擊鼠標(biāo)
left_key(hwnd);
break;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
//設(shè)置窗口位置和大小
void set_position_size(int * p_x_position, int * p_y_position, int * p_x_size, int * p_y_size)
{
int width = GetSystemMetrics(SM_CXFULLSCREEN);//讀取屏幕大小
int heigh = GetSystemMetrics(SM_CYFULLSCREEN);
* p_x_position = (int)width/3;
* p_y_position =(int)heigh/5;
* p_x_size = (int)width/3;
* p_y_size = (int)width/3;
}
void paint_map(HWND hwnd)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
int x1,y1,x2,y2;
int x_position, y_position, x_size, y_size;
set_position_size(&x_position, &y_position, &x_size, &y_size);
int frame_width = GetSystemMetrics(SM_CXSIZEFRAME); //邊框?qū)挾?/span>
int caption_width = GetSystemMetrics(SM_CYCAPTION); //標(biāo)題欄寬度
int menu_high = GetSystemMetrics(SM_CYMENU); //菜單高度
x1 = caption_width;
y1 = caption_width;
x2 = x_size - caption_width - frame_width;
y2 = y_size - 2*caption_width - frame_width;
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
DrawGrid(hdc, x1, y1, x2, y2);
EndPaint (hwnd, &ps) ;
}
//畫格子
BOOL DrawGrid(HDC hdc, int x1, int y1, int x2, int y2)
{
int i, j;
POINT ptLeftTop;
BOOL tmp1, tmp2;
int tmp_x1;
int tmp_y1;
int tmp_x2;
int tmp_y2;
//畫橫線
tmp_x1 = x1;
tmp_y1 = y1;
tmp_x2 = tmp_x1+((x2-x1)/m)*m;
for (i = 0; i <= m; i++)
{
if (i != m)
{
Global_y[i] = tmp_y1;
}
ptLeftTop.x = tmp_x1;
ptLeftTop.y = tmp_y1;
tmp1 = MoveToEx(hdc,ptLeftTop.x,ptLeftTop.y,NULL);
ptLeftTop.x = tmp_x2;
ptLeftTop.y = tmp_y1;
tmp2 = LineTo(hdc,ptLeftTop.x,ptLeftTop.y);
tmp_y1 += (y2-y1)/n;
}
//畫豎線
tmp_x1 = x1;
tmp_y1 = y1;
tmp_y2 = tmp_y1+((y2-y1)/m)*m;
for (j = 0; j <= n; j++)
{
if (j != n)
{
Global_x[j] = tmp_x1;
}
ptLeftTop.x = tmp_x1;
ptLeftTop.y = tmp_y1;
tmp1 = MoveToEx(hdc,ptLeftTop.x,ptLeftTop.y,NULL);
ptLeftTop.x = tmp_x1;
ptLeftTop.y = tmp_y2;
tmp2 = LineTo(hdc,ptLeftTop.x,ptLeftTop.y);
tmp_x1 += (x2-x1)/m;
}
if (tmp1 == FALSE || tmp2 == FALSE)
{
return FALSE;
}
return TRUE;
}
//接口
void left_key(HWND hwnd)
{
int tmp_x,tmp_y,i,j;
POINT lpPoint;
LPARAM lParam;
GetCursorPos(&lpPoint);
PAINTSTRUCT ps ;
HDC hdc ;
RECT rect ;
tmp_x = lpPoint.x;
tmp_y = lpPoint.y;
for (i = 0; i < m; i++)
{
if (tmp_x > Global_x[i] && tmp_x < Global_x[i+1]) break;
}
for (j = 0; j < n; j++)
{
if (tmp_y > Global_y[j] && tmp_y < Global_y[j+1]) break;
}
i--;
j--;
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
if (map[i][j] == '*')
{
DrawText (hdc, TEXT ("GAME OVER!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
}
else if (map[i][j] == 0)
{
DrawRec(hwnd, hdc, i, j);
}
else
{
TCHAR str_tmp[10];
sprintf(str_tmp,"%d",map[i][j]);
DrawText (hdc, TEXT (str_tmp), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
}
}
//畫小矩形,表示該處為0個地雷
void DrawRec(HWND hwnd, HDC hdc, int i, int j)
{
HPEN hPen = CreatePen(PS_SOLID, 10, RGB(0, 255, 0));
//設(shè)置當(dāng)前設(shè)備的畫筆.
HGDIOBJ hOldPen = SelectObject(hdc,hPen);
Rectangle(hdc, Global_x[i], Global_y[j], Global_x[i+1], Global_y[j+1]);
}
//NumMines.h
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "time.h"
#include "stdlib.h"
#define MAX_X 100 //行坐標(biāo)最大值
#define MAX_Y 100 //縱坐標(biāo)最大值
#define MINES -1 //地雷
extern int m, n;
extern int map[MAX_X][MAX_Y];
void set_mines(int num_mines);
int round_num_mines(int i,int j);
BOOL init(void);
//NumMines.cpp
#include "NumMines.h"
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "time.h"
#include "stdlib.h"
/*******************************************************************
初始化地雷分布位置和個數(shù)
函數(shù)功能:根據(jù)設(shè)置的地雷個數(shù)和分布地圖(map,數(shù)組)給出分布好了地雷的數(shù)組
函數(shù)原型:void set_mines( int num_mines)
參數(shù):(in)—— int num_mines
*********************************************************************/
void set_mines(int num_mines)
{
int num = 0;
int i,j;
while (num <= num_mines)
{
srand(time(0));
//rand()%n 取(0,n-1)的隨機數(shù)
i = rand() % m;
j = rand() % n;
//如果出現(xiàn)相同的情況呢?,沒事,再循環(huán)幾次,直到有了足夠的地雷為止
if (i<0 || i>m || j<0 || j>n || map[i][j] == MINES)
{
continue;
}
map[i][j] = MINES;
num++;//判斷地雷個數(shù)
}
}
/****************************************************************************
返回周圍地雷個數(shù)的函數(shù)
函數(shù)原型: int round_num_mines(int i,int j);
參 數(shù): int i, int j為當(dāng)前的坐標(biāo)
返回值類型: int 返回該坐標(biāo)處周圍的地雷數(shù)
返回值情況:(1)返回1-8代表周圍有1-8個地雷;
(2)返回0代表周圍沒有地雷;
(3)返回*代表此坐標(biāo)時地雷;
******************************************************************************/
int round_num_mines(int i,int j)
{
if (map[i][j] == MINES)
{
return MINES;
}
int dir[8][2] = {{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};
int k = 0, num_mines = 0;
for (k = 0; k < 8; k++)
{
if (map[i+dir[k][0]][j+dir[k][1]] == '*')
{
num_mines++;
}
}
return num_mines;
}
BOOL init(void)
{
memset(map, 0, sizeof(map));
int i, j;
int flag = 0;
set_mines((m*n)/10);//55個地雷
for (i=0; i<m; i++)
{
for (j=0; j<=n; j++)
{
map[i][j] = round_num_mines(i, j);
flag = 1;
}
}
if (1 == flag)
{
return TRUE;
}
return FALSE;
}
//
---------------------------
Microsoft Visual C++
---------------------------
Unhandled exception in ClearMines.exe: 0xC0000094: Integer Divide by Zero.
---------------------------
確定
---------------------------
API:
創(chuàng)建窗口:CreateWindow()
函數(shù)功能:該函數(shù)創(chuàng)建一個重疊式窗口、彈出式窗口或子窗口。它指定窗口類,窗口標(biāo)題,窗口風(fēng)格,以及窗口的初始位置及大小(可選的)。該函數(shù)也指定該窗口的父窗口或所屬窗口(如果存在的話),及窗口的菜單。若要使用除CreateWindow函數(shù)支持的風(fēng)格外的擴展風(fēng)格,則使用CreateWindowEx函數(shù)代替CreateWindow函數(shù)。
函數(shù)原型:HWND CreateWindow(LPCTSTR lpClassName,LPCTSTR lpWindowName,DWORD dwStyle,int x,int y,int nWidth,int nHeight,HWND hWndParent,HMENU hMenu,HANDLE hlnstance,LPVOID lpParam);
hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPED | \
WS_CAPTION | \
WS_SYSMENU | \
WS_MINIMIZEBOX, // window style
x_position, // initial x position
y_position, // initial y position
x_size, // initial x size
y_size, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
API:讀取屏幕大小(不包括任務(wù)欄)
int width = GetSystemMetrics(SM_CXFULLSCREEN;
int heigh = GetSystemMetrics(SM_CYFULLSCREEN);
//設(shè)置窗口位置和大小
void set_position_size(int * p_x_position, int * p_y_position, int * p_x_size, int * p_y_size)
{
int width = GetSystemMetrics(SM_CXFULLSCREEN);//讀取屏幕大小
int heigh = GetSystemMetrics(SM_CYFULLSCREEN);
* p_x_position = (int)width/3;
* p_y_position =(int)heigh/5;
* p_x_size = (int)width/3;
* p_y_size = (int)width/3;
}
API:GetSystemMetrics()
int frame_width = GetSystemMetrics(SM_CXSIZEFRAME); //邊框?qū)挾?/span>
int caption_width = GetSystemMetrics(SM_CYCAPTION); //標(biāo)題欄寬度
int menu_high = GetSystemMetrics(SM_CYMENU); //菜單高度
畫矩形:Rectangle(hdc, x1, y1, x2, y2);
畫直線:
MoveToEx是用來移動當(dāng)前畫筆的位置,LineTo是用來畫直線的函數(shù),其實在計算機圖形里的直線顯示是使用光柵圖形學(xué)里的原理。
函數(shù)MoveToEx和LineTo聲明如下:
WINGDIAPI BOOL WINAPI MoveToEx( __in HDC hdc, __in int x, __in int y, __out_opt LPPOINT lppt);
hdc是當(dāng)前設(shè)備的句柄。
x是X軸的位置,水平方向,一般原點是在屏幕左上角的位置。
y是Y軸的位置,垂直方向。
lppt是移動前的坐標(biāo)位置。
WINGDIAPI BOOL WINAPI LineTo( __in HDC hdc, __in int x, __in int y);
hdc是當(dāng)前設(shè)備的句柄。
x是X軸的位置,水平方向,一般原點是在屏幕左上角的位置。
y是Y軸的位置,垂直方向。
WINGDIAPI HPEN WINAPI CreatePen( __in int iStyle, __in int cWidth, __in
COLORREF color);
iStyle 是畫筆的類型,比如是實線,還是虛線等等。
cWidth 是線的寬度。
color 是線的顏色。
//備份:
//Main.cpp
/*------------------------------------------------------------
HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
(c) Charles Petzold, 1998
------------------------------------------------------------*/
#include <windows.h>
#include <stdio.h>
#include "NumMines.h"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
WINGDIAPI BOOL WINAPI MoveToEx(HDC hdc, int x, int y, LPPOINT lppt);//移動當(dāng)前畫筆的位置
WINGDIAPI BOOL WINAPI LineTo(HDC hdc, int x, int y);//用來畫直線的函數(shù)
WINGDIAPI HPEN WINAPI CreatePen(int iStyle, int cWidth, COLORREF color);
//自定義函數(shù)
void paint_map(HWND hwnd);
BOOL DrawGrid(HDC hdc, int x1, int y1, int x2, int y2);
//設(shè)置窗口位置和大小
void set_position_size(int * p_x_position, int * p_y_position, int * p_x_size, int * p_y_size);
void left_key(HWND hwnd);
void DrawRec(HWND hwnd, HDC hdc, int i, int j);
//全局變量
char map[MAX_X][MAX_Y];
int m = 10, n = 10;
int Global_x[MAX_X], Global_y[MAX_Y];
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
int x_position, y_position, x_size, y_size;
set_position_size(&x_position, &y_position, &x_size, &y_size);
init();//畫完地圖,再初始化數(shù)組(地雷分布)
static TCHAR szAppName[] = TEXT ("MainWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
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 (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
/*創(chuàng)建默認(rèn)窗口
hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
*/
hwnd = CreateWindow (szAppName, // window class name
TEXT ("掃雷游戲——The ClearMines Game"), // window caption
WS_OVERLAPPED | \
WS_CAPTION | \
WS_SYSMENU | \
WS_MINIMIZEBOX, // window style
x_position, // initial x position
y_position, // initial y position
x_size, // initial x size
y_size, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
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)
{
switch (message)
{
case WM_PAINT:
paint_map(hwnd);
return 0 ;
case WM_LBUTTONDOWN: //單擊鼠標(biāo)
left_key(hwnd);
break;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
//設(shè)置窗口位置和大小
void set_position_size(int * p_x_position, int * p_y_position, int * p_x_size, int * p_y_size)
{
int width = GetSystemMetrics(SM_CXFULLSCREEN);//讀取屏幕大小
int heigh = GetSystemMetrics(SM_CYFULLSCREEN);
* p_x_position = (int)width/3;
* p_y_position =(int)heigh/5;
* p_x_size = (int)width/3;
* p_y_size = (int)width/3;
}
void paint_map(HWND hwnd)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
int x1,y1,x2,y2;
int x_position, y_position, x_size, y_size;
set_position_size(&x_position, &y_position, &x_size, &y_size);
int frame_width = GetSystemMetrics(SM_CXSIZEFRAME); //邊框?qū)挾?/span>
int caption_width = GetSystemMetrics(SM_CYCAPTION); //標(biāo)題欄寬度
int menu_high = GetSystemMetrics(SM_CYMENU); //菜單高度
x1 = caption_width;
y1 = caption_width;
x2 = x_size - caption_width - frame_width;
y2 = y_size - 2*caption_width - frame_width;
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
DrawGrid(hdc, x1, y1, x2, y2);
EndPaint (hwnd, &ps) ;
}
//畫格子
BOOL DrawGrid(HDC hdc, int x1, int y1, int x2, int y2)
{
int i, j;
POINT ptLeftTop;
BOOL tmp1, tmp2;
int tmp_x1;
int tmp_y1;
int tmp_x2;
int tmp_y2;
//畫橫線
tmp_x1 = x1;
tmp_y1 = y1;
tmp_x2 = tmp_x1+((x2-x1)/m)*m;
for (i = 0; i <= m; i++)
{
if (i != m)
{
Global_y[i] = tmp_y1;
}
ptLeftTop.x = tmp_x1;
ptLeftTop.y = tmp_y1;
tmp1 = MoveToEx(hdc,ptLeftTop.x,ptLeftTop.y,NULL);
ptLeftTop.x = tmp_x2;
ptLeftTop.y = tmp_y1;
tmp2 = LineTo(hdc,ptLeftTop.x,ptLeftTop.y);
tmp_y1 += (y2-y1)/n;
}
//畫豎線
tmp_x1 = x1;
tmp_y1 = y1;
tmp_y2 = tmp_y1+((y2-y1)/m)*m;
for (j = 0; j <= n; j++)
{
if (j != n)
{
Global_x[j] = tmp_x1;
}
ptLeftTop.x = tmp_x1;
ptLeftTop.y = tmp_y1;
tmp1 = MoveToEx(hdc,ptLeftTop.x,ptLeftTop.y,NULL);
ptLeftTop.x = tmp_x1;
ptLeftTop.y = tmp_y2;
tmp2 = LineTo(hdc,ptLeftTop.x,ptLeftTop.y);
tmp_x1 += (x2-x1)/m;
}
if (tmp1 == FALSE || tmp2 == FALSE)
{
return FALSE;
}
return TRUE;
}
//接口
void left_key(HWND hwnd)
{
int tmp_x,tmp_y,i,j;
POINT lpPoint;
LPARAM lParam;
GetCursorPos(&lpPoint);
PAINTSTRUCT ps ;
HDC hdc ;
RECT rect ;
tmp_x = lpPoint.x;
tmp_y = lpPoint.y;
for (i = 0; i < m; i++)
{
if (tmp_x > Global_x[i] && tmp_x < Global_x[i+1]) break;
}
for (j = 0; j < n; j++)
{
if (tmp_y > Global_y[j] && tmp_y < Global_y[j+1]) break;
}
i--;
j--;
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
if (map[i][j] == '*')
{
DrawText (hdc, TEXT ("GAME OVER!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
}
else if (map[i][j] == 0)
{
DrawRec(hwnd, hdc, i, j);
}
else
{
TCHAR str_tmp[10];
sprintf(str_tmp,"%d",map[i][j]);
DrawText (hdc, TEXT (str_tmp), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
}
}
//畫小矩形,表示該處為0個地雷
void DrawRec(HWND hwnd, HDC hdc, int i, int j)
{
HPEN hPen = CreatePen(PS_SOLID, 10, RGB(0, 255, 0));
//設(shè)置當(dāng)前設(shè)備的畫筆.
HGDIOBJ hOldPen = SelectObject(hdc,hPen);
Rectangle(hdc, Global_x[i], Global_y[j], Global_x[i+1], Global_y[j+1]);
}
//NumMines.h
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "time.h"
#include "stdlib.h"
#define MAX_X 100 //行坐標(biāo)最大值
#define MAX_Y 100 //縱坐標(biāo)最大值
#define MINES -1 //地雷
extern int m, n;
extern int map[MAX_X][MAX_Y];
void set_mines(int num_mines);
int round_num_mines(int i,int j);
BOOL init(void);
//NumMines.cpp
#include "NumMines.h"
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "time.h"
#include "stdlib.h"
/*******************************************************************
初始化地雷分布位置和個數(shù)
函數(shù)功能:根據(jù)設(shè)置的地雷個數(shù)和分布地圖(map,數(shù)組)給出分布好了地雷的數(shù)組
函數(shù)原型:void set_mines( int num_mines)
參數(shù):(in)—— int num_mines
*********************************************************************/
void set_mines(int num_mines)
{
int num = 0;
int i,j;
while (num <= num_mines)
{
srand(time(0));
//rand()%n 取(0,n-1)的隨機數(shù)
i = rand() % m;
j = rand() % n;
//如果出現(xiàn)相同的情況呢?,沒事,再循環(huán)幾次,直到有了足夠的地雷為止
if (i<0 || i>m || j<0 || j>n || map[i][j] == MINES)
{
continue;
}
map[i][j] = MINES;
num++;//判斷地雷個數(shù)
}
}
/****************************************************************************
返回周圍地雷個數(shù)的函數(shù)
函數(shù)原型: int round_num_mines(int i,int j);
參 數(shù): int i, int j為當(dāng)前的坐標(biāo)
返回值類型: int 返回該坐標(biāo)處周圍的地雷數(shù)
返回值情況:(1)返回1-8代表周圍有1-8個地雷;
(2)返回0代表周圍沒有地雷;
(3)返回*代表此坐標(biāo)時地雷;
******************************************************************************/
int round_num_mines(int i,int j)
{
if (map[i][j] == MINES)
{
return MINES;
}
int dir[8][2] = {{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};
int k = 0, num_mines = 0;
for (k = 0; k < 8; k++)
{
if (map[i+dir[k][0]][j+dir[k][1]] == '*')
{
num_mines++;
}
}
return num_mines;
}
BOOL init(void)
{
memset(map, 0, sizeof(map));
int i, j;
int flag = 0;
set_mines((m*n)/10);//55個地雷
for (i=0; i<m; i++)
{
for (j=0; j<=n; j++)
{
map[i][j] = round_num_mines(i, j);
flag = 1;
}
}
if (1 == flag)
{
return TRUE;
}
return FALSE;
}
//
---------------------------
Microsoft Visual C++
---------------------------
Unhandled exception in ClearMines.exe: 0xC0000094: Integer Divide by Zero.
---------------------------
確定
---------------------------
轉(zhuǎn)載于:https://www.cnblogs.com/hanxi/archive/2011/03/31/2000978.html
《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
- 上一篇: shell 常用命令
- 下一篇: Torchlight(火炬之光)人物骨骼