日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

API(乱七八糟)

發布時間:2025/3/21 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.
---------------------------
確定
---------------------------

轉載于:https://www.cnblogs.com/hanxi/archive/2011/03/31/2000978.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的API(乱七八糟)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。