API(乱七八糟)

windows程序设计
API:
创建窗口:CreateWindow()
函数功能:该函数创建一个重叠式窗口、弹出式窗口或子窗口。它指定窗口类,窗口标题,窗口风格,以及窗口的初始位置及大小(可选的)。该函数也指定该窗口的父窗口或所属窗口(如果存在的话),及窗口的菜单。若要使用除CreateWindow函数支持的风格外的扩展风格,则使用CreateWindowEx函数代替CreateWindow函数。
函数原型: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:读取屏幕大小(不包括任务栏)
int width = GetSystemMetrics(SM_CXFULLSCREEN;
int heigh = GetSystemMetrics(SM_CYFULLSCREEN);
//设置窗口位置和大小
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); //边框宽度
int caption_width = GetSystemMetrics(SM_CYCAPTION); //标题栏宽度
int menu_high = GetSystemMetrics(SM_CYMENU); //菜单高度

画矩形:Rectangle(hdc, x1, y1, x2, y2);

画直线:
MoveToEx是用来移动当前画笔的位置,LineTo是用来画直线的函数,其实在计算机图形里的直线显示是使用光栅图形学里的原理。

函数MoveToEx和LineTo声明如下:
WINGDIAPI BOOL WINAPI MoveToEx( __in HDC hdc, __in
int x, __in int y, __out_opt LPPOINT lppt);
hdc是当前设备的句柄。
x是X轴的位置,水平方向,一般原点是在屏幕左上角的位置。
y是Y轴的位置,垂直方向。
lppt是移动前的坐标位置。

WINGDIAPI BOOL WINAPI LineTo( __in HDC hdc, __in
int x, __in int y);
hdc是当前设备的句柄。
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);//移动当前画笔的位置
WINGDIAPI BOOL WINAPI LineTo(HDC hdc, int x, int y);//用来画直线的函数
WINGDIAPI HPEN WINAPI CreatePen(int iStyle, int cWidth, COLORREF color);

//自定义函数
void paint_map(HWND hwnd);
BOOL DrawGrid(HDC hdc,
int x1, int y1, int x2, int y2);
//设置窗口位置和大小
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();
//画完地图,再初始化数组(地雷分布)

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 ;
}

/*创建默认窗口
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: //单击鼠标
left_key(hwnd);
break;

case WM_DESTROY:
PostQuitMessage (
0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

//设置窗口位置和大小
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); //边框宽度
int caption_width = GetSystemMetrics(SM_CYCAPTION); //标题栏宽度
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));
//设置当前设备的画笔.
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 //行坐标最大值
#define MAX_Y 100 //纵坐标最大值
#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"

/*******************************************************************
初始化地雷分布位置和个数
函数功能:根据设置的地雷个数和分布地图(map,数组)给出分布好了地雷的数组
函数原型:void set_mines( int num_mines)
参数:(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)的随机数
i = rand() % m;
j
= rand() % n;
//如果出现相同的情况呢?,没事,再循环几次,直到有了足够的地雷为止
if (i<0 || i>m || j<0 || j>n || map[i][j] == MINES)
{
continue;
}
map[i][j]
= MINES;
num
++;//判断地雷个数
}
}

/****************************************************************************
返回周围地雷个数的函数
函数原型: int round_num_mines(int i,int j);
参 数: int i, int j为当前的坐标
返回值类型: int 返回该坐标处周围的地雷数
返回值情况:(1)返回1-8代表周围有1-8个地雷;
(2)返回0代表周围没有地雷;
(3)返回*代表此坐标时地雷;
*****************************************************************************
*/
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.
---------------------------
确定
---------------------------

posted @ 2011-03-31 14:17  涵曦  阅读(646)  评论(0编辑  收藏  举报