C++控制臺程式,原陳述句loadimage(&fm, "./fire/flower.jpg", 3120, 240);現已將flower.jpg轉化為bmp檔案匯入到resource資源中,值為IDB_BITMAP1 改寫成loadimage(&fm, LPCTSTR(IDB_BITMAP1), 3120, 240);程式出錯,例外中斷,求助該如何寫?謝謝!!
uj5u.com熱心網友回復:
LoadImage(hInst, MAKEINTRESOURCE(IDB_BMP1), IMAGE_BITMAP, 0,0, 0)uj5u.com熱心網友回復:
LoadImageThe LoadImage function loads an icon, cursor, or bitmap.
HANDLE LoadImage(
HINSTANCE hinst, // handle of the instance containing the image
LPCTSTR lpszName, // name or identifier of image
UINT uType, // type of image
int cxDesired, // desired width
int cyDesired, // desired height
UINT fuLoad // load flags
);
Parameters
hinst
Handle to an instance of the module that contains the image to be loaded. To load an OEM image, set this parameter to zero.
lpszName
Handle to the image to load.
If the hinst parameter is non-NULL and the fuLoad parameter does not include LR_LOADFROMFILE, lpszName is a pointer to a null-terminated string that contains the name of the image resource in the hinst module.
If hinst is NULL and LR_LOADFROMFILE is not specified, the low-order word of this parameter must be the identifier of the OEM image to load. The OEM image identifiers are defined in WINUSER.H and have the following prefixes: Prefix Meaning
OBM_ OEM bitmaps
OIC_ OEM icons
OCR_ OEM cursors
If the fuLoad parameter includes the LR_LOADFROMFILE value, lpszName is the name of the file that contains the image.
uType
Specifies the type of image to be loaded. This parameter can be one of the following values: Value Meaning
IMAGE_BITMAP Loads a bitmap.
IMAGE_CURSOR Loads a cursor.
IMAGE_ICON Loads an icon.
cxDesired
Specifies the width, in pixels, of the icon or cursor. If this parameter is zero and the fuLoad parameter is LR_DEFAULTSIZE, the function uses the SM_CXICON or SM_CXCURSOR system metric value to set the width. If this parameter is zero and LR_DEFAULTSIZE is not used, the function uses the actual resource width.
cyDesired
Specifies the height, in pixels, of the icon or cursor. If this parameter is zero and the fuLoad parameter is LR_DEFAULTSIZE, the function uses the SM_CYICON or SM_CYCURSOR system metric value to set the height. If this parameter is zero and LR_DEFAULTSIZE is not used, the function uses the actual resource height.
fuLoad
Specifies a combination of the following values: Value Meaning
LR_DEFAULTCOLOR The default flag; it does nothing. All it means is "not LR_MONOCHROME".
LR_CREATEDIBSECTION When the uType parameter specifies IMAGE_BITMAP, causes the function to return a DIB section bitmap rather than a compatible bitmap. This flag is useful for loading a bitmap without mapping it to the colors of the display device.
LR_DEFAULTSIZE Uses the width or height specified by the system metric values for cursors or icons, if the cxDesired or cyDesired values are set to zero. If this flag is not specified and cxDesired and cyDesired are set to zero, the function uses the actual resource size. If the resource contains multiple images, the function uses the size of the first image.
LR_LOADFROMFILE Loads the image from the file specified by the lpszName parameter. If this flag is not specified, lpszName is the name of the resource.
LR_LOADMAP3DCOLORS Searches the color table for the image and replaces the following shades of gray with the corresponding 3D color:
Color Replaced with
Dk Gray,
RGB(128,128,128) COLOR_3DSHADOW
Gray,
RGB(192,192,192) COLOR_3DFACE
Lt Gray,
RGB(223,223,223) COLOR_3DLIGHT
LR_LOADTRANSPARENT Retrieves the color value of the first pixel in the image and replaces the corresponding entry in the color table with the default window color (COLOR_WINDOW). All pixels in the image that use that entry become the default window color. This value applies only to images that have corresponding color tables.
If fuLoad includes both the LR_LOADTRANSPARENT and LR_LOADMAP3DCOLORS values, LRLOADTRANSPARENT takes precedence. However, the color table entry is replaced with COLOR_3DFACE rather than COLOR_WINDOW.
LR_MONOCHROME Loads the image in black and white.
LR_SHARED Shares the image handle if the image is loaded multiple times. If LR_SHARED is not set, a second call to LoadImage for the same resource will load the image again and return a different handle.
Do not use LR_SHARED for images that have non-standard sizes, that may change after loading, or that are loaded from a file.
Windows 95 and Windows 98: The function finds the first image with the requested resource name in the cache, regardless of the size requested.
LR_VGACOLOR Uses true VGA colors.
Return Values
If the function succeeds, the return value is the handle of the newly loaded image.
If the function fails, the return value is NULL. To get extended error information, callGetLastError.
Remarks
When you are finished using the bitmap, cursor, or icon, you can release its associated memory by calling one of the functions in the following table.
Resource Release function
Bitmap DeleteObject
Cursor DestroyCursor
Icon DestroyIcon
The system automatically deletes these resources when the process that created them terminates, however, calling the appropriate function saves memory and decreases the size of the process's working set.
Windows CE: The cxDesired and cyDesired parameters must be zero for IMAGE_BITMAP.
Windows CE does not support stretching and shrinking of icons.
The fuLoad parameter must be zero (==LR_DEFAULTCOLOR).
If you are targeting a platform that does not support mouse cursors, you cannot specify the SM_CXCURSOR and SM_CYCURSOR values in the cxDesired and cyDesired parameters, and you cannot specify IMAGE_CURSOR for the uType parameter.
If you are targeting a platform that supports mouse cursors, you can specify SM_CXCURSOR and SM_CYCURSOR in the cxDesired and cyDesired parameters, and IMAGE_CURSOR in the uType parameter.
uj5u.com熱心網友回復:
LoadBitmap()uj5u.com熱心網友回復:
以上回復答案都不能用呀
uj5u.com熱心網友回復:
不可能的,你沒有讀透uj5u.com熱心網友回復:
#include <stdio.h>#include <graphics.h> //圖形界面的頭檔案
#include <math.h>
#include <time.h>
#include <mmsystem.h> //多媒體設備介面
#include "resource.h"
#include <Windows.h>
#pragma comment(lib,"winmm.lib")
#define NUM 13 //十三個煙花
struct FIRE
{
int r;
int max_r;
int x, y;
int cen_x, cen_y;
int width, height;
int xy[240][240];
bool isShow;
bool isDraw;
DWORD t1, t2, dt;
}Fire[NUM];
struct JET
{
int x, y; //煙花彈的坐標
int hx, hy; //煙花彈的最高點的坐標
int height; //當前煙花彈的坐標與最高點坐標的差
bool isShoot;
DWORD t1, t2, dt; //發射的時間間隔
IMAGE img[2]; //2張 暗色的 亮色
byte n : 1; //2張圖片的下標 位段 byte n 位 1 int 32 -21億---21億
}Jet[NUM];
void Welcome()
{
//播放背景音樂
//MP3格式用下面陳述句
//mciSendString("open ./fire/小幸運.mp3 alias bk", 0, 0, 0);
//mciSendString("play bk repeat", 0, 0, 0);
//WAV格式用下面陳述句
PlaySound((LPCTSTR) IDR_WAVE1, NULL, SND_RESOURCE | SND_LOOP | SND_ASYNC);
setcolor(YELLOW); //讓文字動起來 x y 學習這些函式 C語言9大控制結構
for (int i = 0; i < 50; i++) //i這個控制變數得到x y
{
//600 200 需要得到的圓的圓心
//180 圓的半徑
//60 是圓軌跡上的60個點
int x = 600 + int(180 * sin(3.1415926548 * 2 * i / 60));
int y = 200 + int(180 * cos(3.1415926548 * 2 * i / 60));
cleardevice(); //每一次回圈都把螢屏清除掉
settextstyle(i, 0, "楷體");
outtextxy(x, y, "雙十一,不剁手");
outtextxy(x - 100, y + 80, "寫段代碼");
Sleep(30);
}
getchar(); //等待鍵盤的輸入
cleardevice();
settextstyle(25, 0, "楷體");
outtextxy(400, 200, "XXXXXXXXXXXXXX");
outtextxy(400, 250, "XXXXXXXXX");
outtextxy(400, 300, "XXXXXXXXXXXXXX");
outtextxy(400, 350, "XXXXXXXXXXXXXXXX");
outtextxy(400, 400, "XXXXXXXXX");
outtextxy(400, 450, "XXXXXXXXXXXXX");
outtextxy(400, 500, "XXXXXXXXXXXXXXX");
outtextxy(600, 550, "----");
getchar();
}
//加載圖片
void Load()
{
//加載煙花
IMAGE fm, gm;
loadimage(&fm, L"./images/flower.jpg");
for (int i = 0; i < NUM; i++)
{
SetWorkingImage(&fm); //作業---物件是一張圖片
getimage(&gm, i * 240, 0, 240, 240);
SetWorkingImage(&gm); //作業---物件是一張圖片
for (int a = 0; a < 240; a++)
{
for (int b = 0; b < 240; b++)
{
fire[i].xy[a][b] = getpixel(a, b);
}
}
}
就是這里面的一句如何替換?LoadImage(hInst, MAKEINTRESOURCE(IDB_BMP1), IMAGE_BITMAP, 0,0, 0)直接替換不行,很多未定義的識別符號,差什么陳述句或者頭檔案呀
uj5u.com熱心網友回復:
你用的是 easyX loadimage 而不是 Windows API LoadImage。網上查閱,easyX 的 loadimage 有以下多載:void loadimage(所以,格式不需要轉換,即使轉換了,也沒法通過 Windows 的 LoadImage 得到 easyX 的 IMAGE 物件。
IMAGE *lpDestImage,
LPCTSTR lpImageFile,
int nWidth = 0,
int nHeight = 0,
bool bResize = false
);
void loadimage(
IMAGE *lpDestImage,
LPCTSTR lpResType,
LPCTSTR lpResName,
int nWidth = 0,
int nHeight = 0,
bool bResize = false
);
正確的做法是:
loadimage(&fm, MAKEINTRESOURCE(RT_IMAGE), MAKEINTRESOURCE(IDB_BMP1));這里的 RT_IMAGE 和 IDB_BMP1 由你的資源型別和名字決定。如果你確定添加的是 bmp 資源,則格式如下:
loadimage(&fm, RT_BITMAP, MAKEINTRESOURCE(IDB_BMP1));RT_BITMAP 是 Windows 已定義的資源型別。如果是其他的系統資源型別,都是 RT_ 開頭的。如果是自定義的型別,在頭檔案定義為數值,如 #define RT_IMAGE 100,代碼中就是 MAKEINTRESOURCE(RT_IMAGE),沒有定義宏的,都被認為是字串,則傳 TEXT("RT_IMAGE") 即可。
uj5u.com熱心網友回復:
感謝,你的回復是最詳細,說得最透徹的uj5u.com熱心網友回復:
為何將圖片裝載在資源中編譯沒任何錯誤,程式也可基本運行,資源中的圖片不作業#include <stdio.h> //標準的輸入輸出頭檔案
#include <math.h>
#include <graphics.h> //圖形庫頭檔案
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
#include "resource.h"
#include <conio.h>
#define NUM 13
#define PI 3.1415926548
//煙花彈結構體
struct Jet
{
int x; //坐標
int y;
int hx; //最高的坐標 將賦值給煙花
int hy;
int height; //高度
bool shoot; //是否可以發射
DWORD t1, t2, dt;//煙花彈的上升速度
IMAGE img[2]; //一亮一暗兩張圖片
byte n : 1; //圖片下標 0 1 //位段 一個二進制位 int 32 float 32 short 16
}jet[13];
//煙花結構體
struct Fire
{
int x, y; //坐標
int cen_x, cen_y; //中心坐標
int width,height; //圖片的寬和高
int r; //半徑 從小變大
int max_r; //最大的半徑
int xy[240][240]; //像素點的集合
bool show;
bool draw; //輸出像素點
DWORD t1, t2, dt;
}fire[NUM];
void Init(int i);
void welcome();
void Load();
void Chose(DWORD& t1);
void shoot();
void Show(DWORD* pMem);
//C 語言 條件選擇 函式的使用 回圈 資料型別
// 庫函式 邏輯思維 了解庫函式的時候 組裝成一個程式。
int main() //在使用一些標準庫函式的時候 需要添加頭檔案
{
initgraph(1200, 800); //打開一個界面
welcome();
DWORD t1 = timeGetTime();
DWORD *pMem = GetImageBuffer();
//回圈初始化13次
for (int i = 0; i < NUM; i++)
{
Init(i);
}
Load();
BeginBatchDraw();
while (!_kbhit())
{
Sleep(10);
for (int clr = 0; clr < 1000; clr++)
{
for (int j = 0; j < 2; j++)
{
int px1 = rand() % 1200;
int py1 = rand() % 800;
if (py1 < 799)
{
pMem[py1 * 1200 + px1] = pMem[py1 * 1200 + px1 + 1] = BLACK;
}
}
}
Chose(t1);
shoot();
Show(pMem);
FlushBatchDraw(); //吧顯存展示出來
}
getchar();
closegraph(); //關閉界面
return 0;
}
void welcome()
{
//wav
//PlaySound((LPCTSTR)IDR_WAVE1, NULL, SND_RESOURCE | SND_LOOP | SND_ASYNC);
//MP3
mciSendString(L"open ./music/你還要我怎樣.mp3 alias BGM", 0, 0, 0);
mciSendString(L"play BGM repeat", 0, 0, 0);
settextcolor(YELLOW);
//怎樣得到一個圓的軌跡上的n個坐標。
for (int i = 0; i < 50; i++)
{
int x = 600 + int(180 * sin(PI * 2 * i / 60));
int y = 200 + int(180 * cos(PI * 2 * i / 60));
cleardevice(); //重繪 沒有指定顏色 默認為黑色
settextstyle(i, 0, L"楷體");
outtextxy(x - 200, y, L"給:");
outtextxy(x + 50, y + 100, L"某某");
Sleep(25);
}
getchar(); //卡住
cleardevice();
settextstyle(25, 0, L"楷體");
outtextxy(400,150,L"XXXXXXXXXXXXXXXXXXX");
outtextxy(400, 150, L"XXXXXXXXXX");
outtextxy(400, 200, L"XXXXXXXXX");
outtextxy(400, 250, L"XXXXXXXXXXXXXXXXXX");
outtextxy(400, 300, L"XXXXXXXXXXXXXXXXXXXXXXXXXXX");
outtextxy(400, 350, L"XXXXXXXXXXXXXXXXXX");
outtextxy(400, 400, L"XXXXXXXXX");
outtextxy(400, 450, L"XXXXXXXXXXXXXXXXXXXXXXXXXXX");
outtextxy(400, 500, L"XXXXXXXXXXXXXXXXXX");
getchar();
}
void Init(int i)
{
int r[13] = { 120, 120, 155, 123, 130, 147, 138, 138, 130, 135, 140, 132, 155 };
int x[13] = { 120, 120, 110, 117, 110, 93, 102, 102, 110, 105, 100, 108, 110 };
int y[13] = { 120, 120, 85, 118, 120, 103, 105, 110, 110, 120, 120, 104, 85 };
//初始化煙花
fire[i].x = 0;
fire[i].y = 0;
fire[i].width = 240;
fire[i].height = 240;
fire[i].max_r = r[i];
fire[i].cen_x = x[i];
fire[i].cen_y = y[i];
fire[i].show = false;
fire[i].dt = 5;
fire[i].t1 = timeGetTime();
fire[i].r = 0;
//初始化煙花彈
jet[i].x = 0;
jet[i].y = 0;
jet[i].hx = 0;
jet[i].hy = 0;
jet[i].height = 0;
jet[i].t1 = timeGetTime();
jet[i].dt = rand() % 10;
jet[i].n = 0;
jet[i].shoot = false;
}
void Load()
{
//加載煙花
IMAGE fm,gm;
//loadimage(&fm, L"./images/bluexc/flower.jpg");//用這一句,使用檔案中的圖片可以顯示
loadimage(&fm, RT_BITMAP, MAKEINTRESOURCE(IDB_BITMAP2));//換這一句,資源中的圖片無法顯示,請幫忙修改能運行,我需要將圖片裝載在資源中,說明程式運行需要裝EASYX運行圖新界面庫
for (int i = 0; i < NUM; i++)
{
SetWorkingImage(&fm); //作業---物件是一張圖片
getimage(&gm, i * 240, 0, 240, 240);
SetWorkingImage(&gm); //作業---物件是一張圖片
for (int a = 0; a < 240; a++)
{
for (int b = 0; b < 240; b++)
{
fire[i].xy[a][b] = getpixel(a, b);
}
}
}
//加載煙花彈
IMAGE jm;
loadimage(&jm, L"./images/shoot.jpg");
//jm 里面保存了一整張圖片 分隔開來
for (int i = 0; i < NUM; i++)
{
SetWorkingImage(&jm); //作業---物件是一張圖片
int n = rand() % 5; //0-4
getimage(&jet[i].img[0], n * 20, 0, 20, 50);
getimage(&jet[i].img[1], (n + 5) * 20, 0, 20, 50);
}
SetWorkingImage(NULL); //放棄掉剛才的作業區域。
//零基礎 條件選擇 回圈。
}
//選出13個
void Chose(DWORD& t1)
{
DWORD t2 = timeGetTime();
if (t2 - t1 > 100)
{
int n = rand() % 20;
if (n < NUM&&jet[n].shoot == false&&fire[n].show==false)
{
jet[n].x = rand() % 1200;
jet[n].y = rand() % 100 + 600; //600-699
jet[n].hx = jet[n].x;
jet[n].hy = rand() % 400;
jet[n].height = jet[n].y - jet[n].hy;
jet[n].shoot = true;
putimage(jet[n].x, jet[n].y, &jet[n].img[jet[n].n], SRCINVERT);
}
t1 = t2;
}
}
void shoot()
{
for (int i = 0; i < NUM; i++)
{
jet[i].t2 = timeGetTime();
if (jet[i].t2 - jet[i].t1>jet[i].dt&&jet[i].shoot == true)
{
putimage(jet[i].x, jet[i].y, &jet[i].img[jet[i].n], SRCINVERT);
if (jet[i].y > jet[i].hy)
{
jet[i].n++; //明暗交替的效果
jet[i].y -= 5;
}
putimage(jet[i].x, jet[i].y, &jet[i].img[jet[i].n], SRCINVERT);
//
if (jet[i].y <= jet[i].hy)
{
putimage(jet[i].x, jet[i].y, &jet[i].img[jet[i].n], SRCINVERT);
fire[i].x = jet[i].hx + 10;
fire[i].y = jet[i].hy;
fire[i].show = true;
jet[i].shoot = false;
}
jet[i].t1 = jet[i].t2;
}
}
}
void Show(DWORD* pMem) //顯存指標 像素 這里面所有的像素點的集合
{
//3D游戲 地形 天空 顯存來做
int drt[16] = { 5, 5, 5, 5, 5, 5, 25, 25, 25, 25, 55, 55, 55, 55, 55, 55 };
for (int i = 0; i < NUM; i++)
{
fire[i].t2 = timeGetTime();
if (fire[i].t2 - fire[i].t1>fire[i].dt&&fire[i].show == true)
{
if (fire[i].r < fire[i].max_r)
{
fire[i].r++;
fire[i].dt = drt[fire[i].r / 10];
fire[i].draw = true;
}
if (fire[i].r >= fire[i].max_r-1)
{
fire[i].draw = false;
Init(i);
}
fire[i].t1 = fire[i].t2;
}
if (fire[i].draw)
{
for (double a = 0; a <= 6.28; a += 0.01)
{
int x1 = (int)(fire[i].cen_x + fire[i].r*cos(a));
int y1 = (int)(fire[i].cen_y + fire[i].r*sin(a));
if (x1 > 0 && x1<fire[i].width&&y1>0 && y1 < fire[i].height)
{
int b = fire[i].xy[x1][y1] & 0xff;
int g = (fire[i].xy[x1][y1] >> 8) & 0xff;
int r = (fire[i].xy[x1][y1] >> 16);
int xx = (int)(fire[i].x + fire[i].r*cos(a));
int yy = (int)(fire[i].y - fire[i].r*sin(a));
if (r>0x20 && g > 0x20 && b > 0x20 && xx > 0 && xx<1200 && yy>0 && yy < 800)
pMem[yy * 1200 + xx] = BGR(fire[i].xy[x1][y1]);
}
}
fire[i].draw = false;
}
}
}
uj5u.com熱心網友回復:
改成是上面的了,雖然程式編譯不報錯了,但是裝載在資源中的圖片運行時也不顯示了,原因何在?uj5u.com熱心網友回復:
之前已經說清楚了,用文本打開 rc 檔案,如果其中的代碼是:RT_BITMAP IDB_BITMAP2 "xxx.bmp"
且 resource.h 中,有 IDB_BITMAP2 的宏定義:
#define IDB_BITMAP2 1000
并且 xxx,bmp 的確是一個 bmp 檔案的話,是可以的,不能只是個 bmp 擴展名的假 bmp。
凡是 resource.h 中沒有定義的宏,VS 編輯器中,型別和名字都被加上了引號。如 "RT_IMAGE","IDB_BITMAP2"。
MAKEINTRESOURCE(IDB_BITMAP2) 的作用和 (LPCTSTR)IDB_BITMAP2 一樣。
如果沒有定義,就要用 TEXT("IDB_BITMAP2") 這樣的字串。
自己排查一下,是否引數使用的問題,如 TEXT("IDB_BITMAP2") 和 (LPCTSTR)IDB_BITMAP2 的區別。
不用按我的來,而是應該按你的實際情況來定,搞清楚資源 id、資源名 id 是數字 (宏定義) 還是字串。
uj5u.com熱心網友回復:
樓主,我用loadimage(&fm, RT_BITMAP, MAKEINTRESOURCE(IDB_BITMAP2));也出現了這種情況,請問一下你最后是怎么解決的?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/54365.html
標籤:界面
下一篇:判斷SSH是否遠程登陸
