#include <Windows.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
PBITMAPINFO CreateBitmapInfoStruct(HWND hwnd, HBITMAP hBmp);
void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi,
HBITMAP hBMP, HDC hDC) ;
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
void translate(HWND hwnd,LPCTSTR lpszName,int x,int y)
{
HANDLE h0=LoadImage(NULL,lpszName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
BITMAP bmp0;
GetObject(h0,sizeof(BITMAP),&bmp0);
HDC dc0=CreateCompatibleDC(GetDC(hwnd));
SelectObject(dc0,h0);
HANDLE h;
HDC hdc,dc;
BITMAP bmp;
h=LoadImage(NULL,lpszName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);//打开图片C:/a.bmp并得到句柄
GetObject(h,sizeof(BITMAP),&bmp);//得到图片的宽,高等信息,储存在bmp对象
hdc=GetDC(hwnd);//得到窗口的DC(hWnd是窗口句柄)
dc=CreateCompatibleDC(hdc);//得到与窗口DC兼容的DC
SelectObject(dc,h);//把得到的DC与图片句柄关联起来
//BitBlt(hdc,0,0,bmp.bmWidth,bmp.bmHeight,dc,0,0,SRCCOPY);//把图片画在窗体上
for(int i=0;i<bmp.bmWidth;i++)
{
for(int j=0;j<bmp.bmHeight;j++)
{
COLORREF cr=GetPixel(dc0,i-x,j-y);
SetPixel(dc,i,j,cr);
}
}
PBITMAPINFO pbmi=CreateBitmapInfoStruct(hwnd,(HBITMAP)h);
CreateBMPFile(hwnd, _T(“out.bmp”), pbmi, HBITMAP( h), dc);
DeleteDC(dc);//删除CreateCompatibleDC得到的图片DC
ReleaseDC(hwnd,hdc);//释放GetDC得到的DC
DeleteObject(h);//删除内存中的位图
}
void rotate(HWND hwnd,LPCTSTR lpszName,int x,int y,float theta)
{
HANDLE h0=LoadImage(NULL,lpszName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
BITMAP bmp0;
GetObject(h0,sizeof(BITMAP),&bmp0);
HDC dc0=CreateCompatibleDC(GetDC(hwnd));
SelectObject(dc0,h0);
HANDLE h;
HDC hdc,dc;
BITMAP bmp;
int max=bmp0.bmHeight;
if(bmp0.bmHeight>max)
{
max=bmp0.bmHeight;
}
max*=3;
h=LoadImage(NULL,lpszName,IMAGE_BITMAP,max,max,LR_LOADFROMFILE);//打开图片C:/a.bmp并得到句柄
GetObject(h,sizeof(BITMAP),&bmp);//得到图片的宽,高等信息,储存在bmp对象
hdc=GetDC(hwnd);//得到窗口的DC(hWnd是窗口句柄)
dc=CreateCompatibleDC(hdc);//得到与窗口DC兼容的DC
SelectObject(dc,h);//把得到的DC与图片句柄关联起来
//BitBlt(hdc,0,0,bmp.bmWidth,bmp.bmHeight,dc,0,0,SRCCOPY);//把图片画在窗体上
for(int i=0;i<bmp.bmWidth;i++)
{
for(int j=0;j<bmp.bmHeight;j++)
{
int m=(i-x)*cos(theta)-(j-y)*sin(theta)+x;
int n=(i-x)*sin(theta)+(j-y)*cos(theta)+y;
COLORREF cr=GetPixel(dc0,m,n);
SetPixel(dc,i,j,cr);
}
}
PBITMAPINFO pbmi=CreateBitmapInfoStruct(hwnd,(HBITMAP)h);
CreateBMPFile(hwnd, _T(“out.bmp”), pbmi, HBITMAP( h), dc);
DeleteDC(dc);//删除CreateCompatibleDC得到的图片DC
ReleaseDC(hwnd,hdc);//释放GetDC得到的DC
DeleteObject(h);//删除内存中的位图
}
void scale()
{
}
void show(HWND hwnd,LPCTSTR lpszName)
{
HANDLE h;
HDC hdc,dc;
BITMAP bmp;
h=LoadImage(NULL,lpszName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);//打开图片C:/a.bmp并得到句柄
GetObject(h,sizeof(BITMAP),&bmp);//得到图片的宽,高等信息,储存在bmp对象
hdc=GetDC(hwnd);//得到窗口的DC(hWnd是窗口句柄)
dc=CreateCompatibleDC(hdc);//得到与窗口DC兼容的DC
SelectObject(dc,h);//把得到的DC与图片句柄关联起来
BitBlt(hdc,0,0,bmp.bmWidth,bmp.bmHeight,dc,0,0,SRCCOPY);//把图片画在窗体上
DeleteDC(dc);//删除CreateCompatibleDC得到的图片DC
ReleaseDC(hwnd,hdc);//释放GetDC得到的DC
DeleteObject(h);//删除内存中的位图
}
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WindowProc;
wndcls.lpszClassName=_T(“MyWndClass”);
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd=CreateWindow(_T(“MyWndClass”),_T(“MyWnd”),WS_OVERLAPPEDWINDOW,0,0,700,700,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
//translate(hwnd,_T(“lena.bmp”),100,100);
rotate(hwnd,_T(“lena.bmp”),100,100,2);
show(hwnd,_T(“out.bmp”));
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
HDC hdc;
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,”char is %d”,wParam);
MessageBoxA(hwnd,szChar,”MyMessageBox”,0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,_T(“mouse clicked”),_T(“MyMessageBox”),0);
hdc=GetDC(hwnd);
TextOut(hdc,0,50,_T(“mouse clicked”),strlen((“mouse clicked”)));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
PAINTSTRUCT ps;
hdc=BeginPaint(hwnd,&ps);
TextOut(hdc,0,0,_T(“mouse clicked”),strlen((“mouse clicked”)));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,_T(“exit or not?”),_T(“MyMessageBox”),MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
PBITMAPINFO CreateBitmapInfoStruct(HWND hwnd, HBITMAP hBmp)
{
BITMAP bmp;
PBITMAPINFO pbmi;
WORD cClrBits;
// Retrieve the bitmap’s color format, width, and height.
if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp)) ;
//errhandler(“GetObject”, hwnd);
// Convert the color format to a count of bits.
cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
if (cClrBits == 1)
cClrBits = 1;
else if (cClrBits <= 4)
cClrBits = 4;
else if (cClrBits <= 8)
cClrBits = 8;
else if (cClrBits <= 16)
cClrBits = 16;
else if (cClrBits <= 24)
cClrBits = 24;
else cClrBits = 32;
// Allocate memory for the BITMAPINFO structure. (This structure
// contains a BITMAPINFOHEADER structure and an array of RGBQUAD
// data structures.)
if (cClrBits != 24)
pbmi = (PBITMAPINFO) LocalAlloc(LPTR,
sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD) * (1<< cClrBits));
// There is no RGBQUAD array for the 24-bit-per-pixel format.
else
pbmi = (PBITMAPINFO) LocalAlloc(LPTR,
sizeof(BITMAPINFOHEADER));
// Initialize the fields in the BITMAPINFO structure.
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = bmp.bmWidth;
pbmi->bmiHeader.biHeight = bmp.bmHeight;
pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
if (cClrBits < 24)
pbmi->bmiHeader.biClrUsed = (1<<cClrBits);
// If the bitmap is not compressed, set the BI_RGB flag.
pbmi->bmiHeader.biCompression = BI_RGB;
// Compute the number of bytes in the array of color
// indices and store the result in biSizeImage.
// For Windows NT/2000, the width must be DWORD aligned unless
// the bitmap is RLE compressed. This example shows this.
// For Windows 95/98, the width must be WORD aligned unless the
// bitmap is RLE compressed.
pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits +31) & ~31) /8
* pbmi->bmiHeader.biHeight;
// Set biClrImportant to 0, indicating that all of the
// device colors are important.
pbmi->bmiHeader.biClrImportant = 0;
return pbmi;
}
void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi,
HBITMAP hBMP, HDC hDC)
{
HANDLE hf; // file handle
BITMAPFILEHEADER hdr; // bitmap file-header
PBITMAPINFOHEADER pbih; // bitmap info-header
LPBYTE lpBits; // memory pointer
DWORD dwTotal; // total count of bytes
DWORD cb; // incremental count of bytes
BYTE *hp; // byte pointer
DWORD dwTmp;
pbih = (PBITMAPINFOHEADER) pbi;
lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
if (!lpBits) ;
//errhandler(“GlobalAlloc”, hwnd);
// Retrieve the color table (RGBQUAD array) and the bits
// (array of palette indices) from the DIB.
if (!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi,
DIB_RGB_COLORS))
{
//errhandler(“GetDIBits”, hwnd);
}
// Create the .BMP file.
hf = CreateFile(pszFile,
GENERIC_READ | GENERIC_WRITE,
(DWORD) 0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
if (hf == INVALID_HANDLE_VALUE) ;
// errhandler(“CreateFile”, hwnd);
hdr.bfType = 0x4d42; // 0x42 = “B” 0x4d = “M”
// Compute the size of the entire file.
hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed
* sizeof(RGBQUAD) + pbih->biSizeImage);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
// Compute the offset to the array of color indices.
hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed
* sizeof (RGBQUAD);
// Copy the BITMAPFILEHEADER into the .BMP file.
if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER),
(LPDWORD) &dwTmp, NULL))
{
// errhandler(“WriteFile”, hwnd);
}
// Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
// Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) + pbih->biClrUsed * sizeof (RGBQUAD), (LPDWORD) &dwTmp, ( NULL) ));
// errhandler(“WriteFile”, hwnd);
// Copy the array of color indices into the .BMP file.
dwTotal = cb = pbih->biSizeImage;
hp = lpBits;
if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL))
printf(“WriteFile”, hwnd);
// Close the .BMP file.
if (!CloseHandle(hf)) ;
// errhandler(“CloseHandle”, hwnd);
// Free memory.
GlobalFree((HGLOBAL)lpBits);
}