📄 blitit.cpp
字号:
// BlitIt.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "BlitIt.h"
#include <commctrl.h>
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // The current instance
HWND hwndCB; // The command bar handle
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass (HINSTANCE hInstance, LPTSTR szWindowClass);
BOOL InitInstance (HINSTANCE, int);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
HACCEL hAccelTable;
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_BLITIT);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_BLITIT));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The window class name
hInst = hInstance; // Store instance handle in our global variable
// Initialize global strings
LoadString(hInstance, IDC_BLITIT, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance, szWindowClass);
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
if (hwndCB)
CommandBar_Show(hwndCB, TRUE);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
int wmId, wmEvent;
PAINTSTRUCT ps;
TCHAR szHello[MAX_LOADSTRING];
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_HELP_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_FILE_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
hwndCB = CommandBar_Create(hInst, hWnd, 1);
CommandBar_InsertMenubar(hwndCB, hInst, IDM_MENU, 0);
CommandBar_AddAdornments(hwndCB, 0, 0);
break;
case WM_PAINT:
RECT rt;
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rt);
// <BOOK_ADDON Chapter 10.1.3> *****************************************
DrawBitmapFromFile(hdc,TEXT("\\Windows\\Startup.bmp"),0,rt.left,rt.top,rt.right,rt.bottom,TRUE);
// </BOOK_ADDON Chapter 10.1.3> *****************************************
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
DrawText(hdc, szHello, _tcslen(szHello), &rt,
DT_SINGLELINE | DT_VCENTER | DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CommandBar_Destroy(hwndCB);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for the About box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
RECT rt, rt1;
int DlgWidth, DlgHeight; // dialog width and height in pixel units
int NewPosX, NewPosY;
switch (message)
{
case WM_INITDIALOG:
// trying to center the About dialog
if (GetWindowRect(hDlg, &rt1)) {
GetClientRect(GetParent(hDlg), &rt);
DlgWidth = rt1.right - rt1.left;
DlgHeight = rt1.bottom - rt1.top ;
NewPosX = (rt.right - rt.left - DlgWidth)/2;
NewPosY = (rt.bottom - rt.top - DlgHeight)/2;
// if the About box is larger than the physical screen
if (NewPosX < 0) NewPosX = 0;
if (NewPosY < 0) NewPosY = 0;
SetWindowPos(hDlg, 0, NewPosX, NewPosY,
0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
return TRUE;
case WM_COMMAND:
if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
// <BOOK_ADDON Chapter 10.1.3> *****************************************
void DrawBitmapFromFile(HDC hdc,LPCTSTR FileName,DWORD Offset,int x,
int y, int dx, int dy,BOOL stretch)
{
LPBYTE lpDIB= NULL;
if( (lpDIB=ReadBMPFile(FileName,Offset)) == NULL )
return; // Error - do nothing
BlitDIB(hdc, (LPBITMAPINFO)lpDIB, x,y,dx,dy, stretch );
free( lpDIB );
}
LPBYTE ReadBMPFile( LPCTSTR szFileName, DWORD Offset )
{
HANDLE hFile;
BITMAPFILEHEADER bfh;
DWORD dwBytes;
LPBYTE lpDIB = NULL, lpTemp = NULL;
WORD wPaletteSize = 0;
DWORD dwBitsSize = 0;
// Open the file
if( (hFile=CreateFile( szFileName, GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL))
== INVALID_HANDLE_VALUE )
return NULL;
if( SetFilePointer( hFile, Offset, NULL, FILE_BEGIN ) == 0xffffffff )
{
CloseHandle( hFile );
return NULL;
}
// Read the header
if((! ReadFile(hFile,&bfh,sizeof(BITMAPFILEHEADER),&dwBytes, NULL))
|| ( dwBytes != sizeof( BITMAPFILEHEADER ) ) )
{
CloseHandle( hFile );
return NULL;
}
// Does it look like a BMP file?
if((bfh.bfType != 0x4d42) || (bfh.bfReserved1!=0) || (bfh.bfReserved2!=0) )
{
CloseHandle( hFile );
return NULL;
}
// Allocate memory for DIB
if( (lpDIB = (LPBYTE) malloc( sizeof( BITMAPINFO ))) == NULL )
{
CloseHandle( hFile );
return NULL;
}
// Read in the BITMAPINFOHEADER
if( (!ReadFile(hFile,lpDIB,sizeof(BITMAPINFOHEADER),&dwBytes,NULL))
|| (dwBytes!=sizeof(BITMAPINFOHEADER)) )
{
CloseHandle( hFile );
free( lpDIB );
return NULL;
}
if( ((LPBITMAPINFOHEADER)lpDIB)->biSize !=
sizeof( BITMAPINFOHEADER ) )
{
CloseHandle( hFile );
free( lpDIB );
return NULL;
}
wPaletteSize = PaletteSize((LPSTR)lpDIB);
dwBitsSize = ((LPBITMAPINFOHEADER)lpDIB)->biHeight *
BytesPerLine((LPBITMAPINFOHEADER)lpDIB);
// realloc to account for the total size of the DIB
if( (lpTemp = (LPBYTE)realloc(lpDIB, sizeof( BITMAPINFOHEADER ) +
wPaletteSize + dwBitsSize )) == NULL)
{
CloseHandle( hFile );
free( lpDIB );
return NULL;
}
lpDIB = lpTemp;
// If there is a color table, read it
if( wPaletteSize != 0 )
{
if( (!ReadFile( hFile, ((LPBITMAPINFO)lpDIB)->bmiColors,
wPaletteSize, &dwBytes, NULL ))
|| (dwBytes!=wPaletteSize) )
{
CloseHandle( hFile );
free( lpDIB );
return NULL;
}
}
if( bfh.bfOffBits != 0 )
{
if( SetFilePointer( hFile, bfh.bfOffBits+Offset, NULL,
FILE_BEGIN ) == 0xffffffff )
{
CloseHandle( hFile );
free( lpDIB );
return NULL;
}
}
// Read the image bits
if( (!ReadFile( hFile, FindDIBBits((LPSTR)lpDIB), dwBitsSize,
&dwBytes, NULL )) || (dwBytes!=dwBitsSize) )
{
CloseHandle( hFile );
free( lpDIB );
return NULL;
}
// clean up
CloseHandle( hFile );
return lpDIB;
}
BOOL BlitDIB( HDC JChdc,LPBITMAPINFO lpSrcDIB, int x,int y,int dx,
int dy,BOOL bStretch )
{
LPBITMAPINFO lpbmi = NULL;
LPBYTE lpSourceBits;
HDC hDC = NULL, hSourceDC;
HBITMAP hSourceBitmap, hOldSourceBitmap;
DWORD dwSourceBitsSize;
hDC = GetDC( NULL );
hSourceBitmap = CreateDIBSection( hDC, lpSrcDIB, DIB_RGB_COLORS,
(void **)&lpSourceBits, NULL, 0 );
hSourceDC = CreateCompatibleDC( hDC );
dwSourceBitsSize = lpSrcDIB->bmiHeader.biHeight *
BytesPerLine(&(lpSrcDIB->bmiHeader));
memcpy( lpSourceBits, FindDIBBits((LPSTR)lpSrcDIB), dwSourceBitsSize );
hOldSourceBitmap = (HBITMAP)SelectObject(hSourceDC,hSourceBitmap);
if( bStretch )
StretchBlt( JChdc, x, y, x+dx, y+dy, hSourceDC, 0, 0,
lpSrcDIB->bmiHeader.biWidth,
lpSrcDIB->bmiHeader.biHeight, SRCCOPY );
else
BitBlt( JChdc, x, y, x+dx, y+dy, hSourceDC, 0, 0, SRCCOPY );
// Clean up and delete the DCs
SelectObject( hSourceDC, hOldSourceBitmap );
DeleteDC( hSourceDC );
ReleaseDC( NULL, hDC );
DeleteObject( hSourceBitmap );
free( lpbmi );
return TRUE;
}
LPSTR FindDIBBits( LPSTR lpbi )
{
return ( lpbi + *(LPDWORD)lpbi + PaletteSize( lpbi ) );
}
WORD DIBNumColors( LPSTR lpbi )
{
WORD wBitCount;
DWORD dwClrUsed;
dwClrUsed = ((LPBITMAPINFOHEADER) lpbi)->biClrUsed;
if (dwClrUsed)
return (WORD) dwClrUsed;
wBitCount = ((LPBITMAPINFOHEADER) lpbi)->biBitCount;
switch (wBitCount)
{
case 1: return 2;
case 2: return 4;
case 4: return 16;
case 8: return 256;
default:return 0;
}
return 0;
}
WORD PaletteSize( LPSTR lpbi )
{
return ( DIBNumColors( lpbi ) * sizeof( RGBQUAD ) );
}
DWORD BytesPerLine( LPBITMAPINFOHEADER lpBMIH )
{
return WIDTHBYTES(lpBMIH->biWidth * lpBMIH->biPlanes
* lpBMIH->biBitCount);
}
// </BOOK_ADDON Chapter 10.1.3> *****************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -