/*--------------------------------------------------------------------------
BRICKS1.C -- LoadBitmap Demonstration
(c) Charles Petzold, 1998
----------------------------------------------------------------------------*/
#include <windows.h>
#include <atlimage.h>
#define ID_TIMER 1
#define GET_X_LPARAM(lParam) ((int)(short)LOWORD(lParam))
#define GET_Y_LPARAM(lParam) ((int)(short)HIWORD(lParam))
CImage myImage;
char* szPicPath = ".//temp//1765.jpg";
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Bricks1") ;
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 ;
}
//获得屏幕的宽和高
int scrWidth = ::GetSystemMetrics(SM_CXSCREEN);
int scrHeight = ::GetSystemMetrics(SM_CYSCREEN);
HRESULT hResult;
hResult = myImage.Load(szPicPath);
if(FAILED(hResult))
{
char tmpstr[128] = {0};
sprintf(tmpstr, "load %s failed!", szPicPath);
::MessageBox(NULL, tmpstr, "error", MB_OK);
return 0;
}
//得到图像的宽高
int picWidth = myImage.GetWidth();
int picHeight = myImage.GetHeight();
hwnd = CreateWindow( szAppName, TEXT("LoadBitmap Demo"),
WS_POPUP,
(scrWidth - picWidth)/2, (scrHeight - picHeight)/2,
picWidth, picHeight,
NULL, NULL, hInstance, NULL) ;
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)
{
static HBITMAP hBmp_closeDef, hBmp_closeDown ;
static int cxClient, cyClient, cx_closeDef, cy_closeDef, cx_closeDown, cy_closeDown;
BITMAP bmp_closeDef, bmp_closeDown;
HDC hdc, hdcMem ;
HINSTANCE hInstance ;
PAINTSTRUCT ps ;
// static CImage myImage;
// HRESULT hResult;
char tmpstr[128] = {0};
int xPos, yPos;
//设置鼠标拖拽标志
static bool bCloseDown;
switch (message)
{
case WM_CREATE:
hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
bCloseDown = false;
//加载两个不同的close图标
hBmp_closeDef = LoadBitmap(hInstance, TEXT ("CLOSE_DEF"));
hBmp_closeDown = LoadBitmap(hInstance, TEXT("CLOSE_DOWN"));
GetObject(hBmp_closeDef, sizeof(BITMAP), &bmp_closeDef);
GetObject(hBmp_closeDown, sizeof(BITMAP), &bmp_closeDown);
cx_closeDef = bmp_closeDef.bmWidth;
cy_closeDef = bmp_closeDef.bmHeight;
cx_closeDown = bmp_closeDown.bmWidth;
cy_closeDown = bmp_closeDown.bmHeight;
//使用CImage类
// hResult = myImage.Load("D://Bricks.bmp");
/* hResult = myImage.Load("D://1765.jpg");
if(FAILED(hResult))
{
MessageBox(hwnd, "load picture failed!", "error", MB_OK);
//退出程序
DestroyWindow(hwnd);
}*/
//设置定时器
SetTimer(hwnd, ID_TIMER, 30000, NULL);
return 0 ;
case WM_TIMER:
// DestroyWindow(hwnd);
PostMessage(hwnd, WM_DESTROY, 0, 0);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps) ;
myImage.StretchBlt(hdc, 0, 0, myImage.GetWidth(), myImage.GetHeight(), SRCCOPY);
hdcMem = CreateCompatibleDC (hdc) ;
//处理close图标鼠标拖拽
if(bCloseDown)
{
SelectObject(hdcMem, hBmp_closeDown);
BitBlt(hdc, 0, 0, cx_closeDown, cy_closeDown, hdcMem, 0, 0, SRCCOPY);
}
else
{
SelectObject(hdcMem, hBmp_closeDef) ;
BitBlt(hdc, 0, 0, cx_closeDef, cy_closeDef, hdcMem, 0, 0, SRCCOPY);
}
DeleteDC(hdcMem);
EndPaint(hwnd, &ps);
return 0 ;
case WM_MOUSEMOVE:
// if(wParam == MK_LBUTTON)
// {
// ::MessageBox(NULL, "dd", "error", MB_OK);
// ::MessageBox(hwnd, "dd", "error", MB_OK);
// }
xPos = GET_X_LPARAM(lParam);
yPos = GET_Y_LPARAM(lParam);
if(bCloseDown)
{
if(xPos > 16 || yPos > 16)
{
bCloseDown = false;
InvalidateRect(hwnd, NULL, TRUE);
}
}
return 0;
case WM_LBUTTONDOWN:
xPos = GET_X_LPARAM(lParam);
yPos = GET_Y_LPARAM(lParam);
if(xPos < 16 && yPos < 16)
{
bCloseDown = true;
InvalidateRect(hwnd, NULL, TRUE);
}
return 0;
case WM_LBUTTONUP:
xPos = GET_X_LPARAM(lParam);
yPos = GET_Y_LPARAM(lParam);
// sprintf(tmpstr, "x: %d, y: %d", xPos, yPos);
// MessageBox(hwnd, tmpstr, "456", MB_OK);
if(xPos < 16 && yPos < 16 && bCloseDown)
{
PostMessage(hwnd, WM_DESTROY, 0, 0);
}
return 0;
case WM_DESTROY:
KillTimer(hwnd, ID_TIMER); //程序有问题,不知是否是没有此句的原因
DeleteObject(hBmp_closeDef);
DeleteObject(hBmp_closeDown);
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc(hwnd, message, wParam, lParam) ;
}