不知道为什么运行的时候窗口一闪就没了,这个程序是做的图片的半透明处理,按7:3的比例处理两张图片像素,bg.bmp图片占70% , girl.bmp图片占30%,求各位大神给俺指导指导
下面这张是bg.bmp
下面这张是girl.bmp
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND , UINT , WPARAM , LPARAM);
ATOM MyRegisterClass(HINSTANCE);
BOOL InitInstance(HINSTANCE , int);
void MsgDeal();
void MyPaint(HDC);
HBITMAP bg , girl;
HDC mdc;
const int xstart = 50;
const int ystart = 20;
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
MyRegisterClass(hInstance);
InitInstance(hInstance , nCmdShow);
MsgDeal();
return 0;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbClsExtra = 0;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.cbWndExtra = 0;
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.hCursor = LoadCursor(NULL , IDC_ARROW);
wcex.hIcon = NULL;
wcex.hIconSm = NULL;
wcex.hInstance = hInstance;
wcex.lpfnWndProc = WindowProc;
wcex.lpszClassName = "canvas_bitmap1";
wcex.lpszMenuName = NULL;
wcex.style = CS_HREDRAW | CS_VREDRAW;
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance , int nCmdShow)
{
HWND hWnd;
HDC hdc;
hWnd = CreateWindow("canvas_bitmap1" , "图片的半透明处理" , WS_OVERLAPPEDWINDOW , 0 , 0 ,
CW_USEDEFAULT , CW_USEDEFAULT , NULL , NULL , hInstance , NULL);
if(!hWnd)
{
return FALSE;
}
MoveWindow(hWnd , 10 , 10 , 600 , 450 , TRUE);
ShowWindow(hWnd , nCmdShow);
UpdateWindow(hWnd);
hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
BITMAP bm_bg , bm_girl;
unsigned char *px_bg , *px_girl;
//处理背景图
bg = (HBITMAP)LoadImage(NULL , "bg.bmp" , IMAGE_BITMAP , 500 , 262 , LR_LOADFROMFILE);
GetObject(bg , sizeof(BITMAP) , &bm_bg);
if(bm_bg.bmBitsPixel != 32 && bm_bg.bmBitsPixel != 24)
{
MessageBox(NULL , "此程序只能在32bit或24bit显示模式中运行" , "警告" , 0);
return FALSE;
}
px_bg = new unsigned char[bm_bg.bmHeight * bm_bg.bmWidthBytes];
GetBitmapBits(bg , bm_bg.bmHeight * bm_bg.bmWidthBytes , px_bg);
//处理前景图
girl = (HBITMAP)LoadImage(NULL , "girl.bmp" , IMAGE_BITMAP , 250 , 140 , LR_LOADFROMFILE);
GetObject(girl , sizeof(BITMAP) , &bm_girl);
px_girl = new unsigned char[bm_girl.bmHeight * bm_girl.bmWidthBytes];
GetBitmapBits(girl , bm_girl.bmHeight * bm_girl.bmWidthBytes , px_girl);
int xend , yend;
int x , y , i; //循环变量
int rgb_b;
int PxBytes = bm_bg.bmBitsPixel / 8;
xend = xstart +250;
yend = ystart+ 140;
//处理背景图像素颜色
for(x = xstart; x < xend; x ++)
{
for(y = ystart; y < yend; y ++)
{
rgb_b = y * bm_bg.bmWidthBytes + x * PxBytes;
px_bg[rgb_b] = px_bg[rgb_b] * 0.7;
px_bg[rgb_b+1] = px_bg[rgb_b+2] * 0.7;
px_bg[rgb_b+2] = px_bg[rgb_b+2] * 0.7;
}
}
//处理前景图像素颜色
for(x = 0; x < bm_girl.bmWidth; x ++)
{
for(y = 0; y < bm_girl.bmWidth; y ++)
{
rgb_b = y * bm_girl.bmWidthBytes + x * PxBytes;
i = (ystart+y) * bm_girl.bmWidthBytes + (xstart+x) * PxBytes;
px_girl[rgb_b] = px_girl[rgb_b] * 0.3 + px_bg[i];
px_girl[rgb_b+1] = px_girl[rgb_b+1] * 0.3 + px_bg[i+1];
px_girl[rgb_b+2] = px_girl[rgb_b+2] * 0.3 + px_bg[i+2];
}
}
SetBitmapBits(girl , bm_girl.bmHeight * bm_girl.bmWidthBytes , px_girl);
MyPaint(hdc);
ReleaseDC(hWnd , hdc);
delete []px_bg;
delete []px_girl;
return TRUE;
}
void MsgDeal()
{
MSG msg;
while(GetMessage(&msg , NULL , 0 , 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
HDC hdc;
PAINTSTRUCT ps;
switch(uMsg)
{
case WM_PAINT:
hdc = BeginPaint(hwnd , &ps);
EndPaint(hwnd , &ps);
break;
case WM_DESTROY:
DeleteObject(mdc);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd , uMsg , wParam , lParam);
}
return 0;
}
void MyPaint(HDC hdc)
{
//贴上背景图
SelectObject(mdc , bg);
BitBlt(hdc , 0 , 0 , 500 , 262 , mdc , 0 , 0 ,SRCCOPY);
//贴上处理后的半透明图
SelectObject(mdc , girl);
BitBlt(hdc , xstart , ystart , 250 , 140 , mdc , 0 , 0 , SRCCOPY);
}
终于找到错误原因了,处理前景图像素颜色的时候,应该是y<bm_girl.bmHeight,还有i应该=(y+ystart) * bm_bg.bmWidthBytes + (x + xstart) * PxBytes