EasyX庫簡單中文手冊
作者:
時間: 2021/2/2
第一個例程
#include <graphics.h> // 圖像相關庫 #include <conio.h> // 按鍵獲取相關庫 int main() { initgraph(640, 480); // 創建一個圖像畫板 circle(200, 200, 100); // 以(200,200)為圓心畫一個r100的圓 _getch(); // 獲取一個按鍵值 closegraph(); // 關閉畫板 return 0; }
//conio是Console Input / Output(控制台輸入輸出)的簡寫,其中定義了通過控制台進行數據輸入和數據輸出的函數,主要是一些用戶通過按鍵盤產生的對應操作,比如getch()函數等等。
基本元素——顏色
Setlinecolor,有四種格式 setlinecolor(0xff0000); setlinecolor(BLUE); setlinecolor(RGB(0, 0, 255)); setlinecolor(HSLtoRGB(240, 1, 0.5));
Constant |
Value |
Color description |
BLACK |
0 |
Black |
BLUE |
0xAA0000 |
Blue |
GREEN |
0x00AA00 |
Green |
CYAN |
0xAAAA00 |
Cyan |
RED |
0x0000AA |
Red |
MAGENTA |
0xAA00AA |
Magenta |
BROWN |
0x0055AA |
Brown |
LIGHTGRAY |
0xAAAAAA |
Light Gray |
DARKGRAY |
0x555555 |
Dark Gray |
LIGHTBLUE |
0xFF5555 |
Bright Blue |
LIGHTGREEN |
0x55FF55 |
Bright Green |
LIGHTCYAN |
0xFFFF55 |
Bright Cyan |
LIGHTRED |
0x5555FF |
Bright Red |
LIGHTMAGENTA |
0xFF55FF |
Bright Magenta |
YELLOW |
0x55FFFF |
Yellow |
WHITE |
0xFFFFFF |
White |
基本元素——圓
void circle(int x, int y, int radius); void fillcircle(int x, int y, int radius); setfillcolor(BLUE);
無邊界實現圓
void solidcircle( int x, int y, int radius );
基本元素——直線
void line( int x1, int y1, int x2, int y2 );
基本元素——長方形
void rectangle( int left, int top, int right, int bottom );
Solidrectangle 無邊框填充長方形
Fillrectangle 填充長方形
基本元素——文字
setbkmode(TRANSPARENT); // 文字除了字體外透明 settextstyle(40, 0, L"Verdana"); //設置字體高,寬(0自適應),字體 wchar_t s[] = L"游戲開始"; outtextxy(10, 20, s);
圖片顯示
// 從圖片文件獲取圖像(bmp/jpg/gif/emf/wmf/ico)
void loadimage( IMAGE* pDstImg, // 保存圖像的 IMAGE 對象指針 LPCTSTR pImgFile, // 圖片文件名 int nWidth = 0, // 圖片的拉伸寬度 int nHeight = 0, // 圖片的拉伸高度 bool bResize = false // 是否調整 IMAGE 的大小以適應圖片 );
pDstImg:保存圖像的 IMAGE 對象指針。如果為 NULL,表示圖片將讀取至繪圖窗口。
pImgFile:圖片文件名。支持 bmp / jpg / gif / emf / wmf / ico 類型的圖片。gif 類型的圖片僅加載第一幀,不支持透明。
nWidth:圖片的拉伸寬度。加載圖片后,會拉伸至該寬度。如果為 0,表示使用原圖的寬度。
nHeight:圖片的拉伸高度。加載圖片后,會拉伸至該高度。如果為 0,表示使用原圖的高度。
bResize:是否調整 IMAGE 的大小以適應圖片。
#
include <iostream> #include <graphics.h> // 引用圖形庫頭文件 #include <conio.h> int main() { initgraph(640, 480); // 創建繪圖窗口,大小為 640x480 像素 IMAGE img; //創建IMAGE對象 loadimage(&img, L"01.png");//絕對地址載入圖片 putimage(100, 0, &img); _getch(); // 按任意鍵繼續 closegraph(); // 關閉繪圖窗口 }
鼠標的使用
struct MOUSEMSG { UINT uMsg; // Current mouse message. bool mkCtrl; // The CTRL key is down. bool mkShift; // The SHIFT key is down. bool mkLButton; // The left mouse button is down. bool mkMButton; // The middle mouse button is down. bool mkRButton; // The right mouse button is down. int x; // The x-coordinate of the cursor. (physical coordinates) int y; // The y-coordinate of the cursor. (physical coordinates) int wheel; // The mouse wheel scroll value.};
Value |
Description |
WM_MOUSEMOVE |
The message of the mouse movement. |
WM_MOUSEWHEEL |
The message of the mouse wheel scroll. |
WM_LBUTTONDOWN |
The message of the left mouse button down. |
WM_LBUTTONUP |
The message of the left mouse button up. |
WM_LBUTTONDBLCLK |
The message of the left mouse button double click. |
WM_MBUTTONDOWN |
The message of the middle mouse button down. |
WM_MBUTTONUP |
The message of the middle mouse button up. |
WM_MBUTTONDBLCLK |
The message of the middle mouse button double click. |
WM_RBUTTONDOWN |
The message of the right mouse button down. |
WM_RBUTTONUP |
The message of the right mouse button up. |
WM_RBUTTONDBLCLK |
The message of the right mouse button double click. |
#include<graphics.h> #include<conio.h> #include<stdio.h> //鼠標左鍵點擊 int main() { initgraph(800, 600); setbkcolor(WHITE); cleardevice(); while (1) { MOUSEMSG m; if (MouseHit()) { m = GetMouseMsg(); if (m.uMsg == WM_LBUTTONDOWN) { setfillcolor(GREEN); fillcircle(m.x, m.y, 30); } } } return 0; }
// //// 鼠標移動 //int main() //{ // initgraph(800, 600); // setbkcolor(WHITE); // cleardevice(); // // while (1) // { // MOUSEMSG m; // if (MouseHit()) // { // m = GetMouseMsg(); // if (m.uMsg == WM_MOUSEMOVE) // { // setfillcolor(GREEN); // fillcircle(m.x, m.y, 3); // // } // // } // // } // return 0; //}
音樂播放
#include <windows.h> #include <mmsystem.h> #include<conio.h> #pragma comment(lib, "winmm.lib") int main() { //mci: media control interface(多媒體控制接口) mciSendStringW(L"play 01.mp3", 0, 0, 0); mciSendString(L"play 01.mp3", 0, 0, 0); _getch(); mciSendString(L"pause 01.mp3", 0, 0, 0); _getch(); mciSendString(L"resume 01.mp3", 0, 0, 0); system("pause"); return 0; }
//open 打開設備
//close 關閉設備
//play 開始設備播放
//stop 停止設備的播放或記錄
//record 開始記錄
//save 保存設備內容
//pause 暫停設備的播放或記錄
//resume 恢復暫停播放或記錄的設備
//seek 改變媒體的當前位置
//capacility 查詢設備能力
//info 查詢設備的信息
//status 查詢設備狀態信息
批量繪圖
BeginBatchDraw 這個函數用於開始批量繪圖。執行后,任何繪圖操作都將暫時不輸出到屏幕上,直到執行 FlushBatchDraw 或 EndBatchDraw 才將之前的繪圖輸出。
隨機數
rand() 參數隨機數,但是每次都是一個相同序列
srand(time(0)); time(0)一般只用於重新運行時要產生不同隨機數的情況,否則在這一秒內產生的隨機數將會是一樣的。
按鍵
_getchar();