/*******************************************************************
程序:Bitmap
文件:Bitmap.c
功能:位图演示程序
********************************************************************/
#include <windows.h>
#include <windowsx.h>
#include"resource.h"
#include "抓屏程序.h"
BOOL InitWindow (HINSTANCE hInstance, int nCmdShow);
BOOL Initshow(HINSTANCE hInstance, int nCmdShow);
LRESULT CALLBACK WinProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK Winshow (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
HWND hchild,hfather;
static BOOL show,zhua,save;
static TCHAR FileName[128]="";
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow)
{
MSG msg;
if (!InitWindow (hInstance, iCmdShow))
return FALSE;
if (!Initshow (hInstance, iCmdShow))
return FALSE;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
static BOOL InitWindow (HINSTANCE hInstance, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Bitmap");
HWND hwnd;
WNDCLASS wc;
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.lpfnWndProc = WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (hInstance, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_CROSS);
wc.hbrBackground = (HBRUSH)GetStockObject (HOLLOW_BRUSH);
wc.lpszMenuName = szAppName;
wc.lpszClassName = szAppName;
if (!RegisterClass (&wc))
{
MessageBox (NULL, TEXT ("注册窗口类失败!"), szAppName,
MB_ICONERROR);
return 0;
}
hwnd = CreateWindow (szAppName, TEXT ("全屏"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if( !hwnd ) return FALSE;
//ShowWindow( hwnd, iCmdShow );
UpdateWindow( hwnd );
return TRUE;
}
static BOOL Initshow (HINSTANCE hInstance, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Bitmapcopy");
HWND hwnd;
WNDCLASS wc;
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.lpfnWndProc = Winshow;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (hInstance,MAKEINTRESOURCE(IDI_ICON1));
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
if (!RegisterClass (&wc))
{
MessageBox (NULL, TEXT ("注册窗口类失败!"), szAppName,
MB_ICONERROR);
return 0;
}
hwnd = CreateWindow (szAppName, TEXT ("抓屏程序"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
500,400,
NULL, NULL, hInstance, NULL);
if( !hwnd ) return FALSE;
ShowWindow( hwnd, iCmdShow );
UpdateWindow( hwnd );
return TRUE;
}
LRESULT CALLBACK WinProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc, hdcDest, hdc2;
RECT rect;
static int iShowMode ;
static DWORD iROP ;
static int iSelectedBitmap ;
static HBITMAP hOldBitmap;
BITMAP bm;
static HDC hdcMem;
static HBITMAP hSelectBitmap=NULL;
static HBITMAP hBitmap;
HBRUSH hPatternBrush=NULL, hOldBrush=NULL;
static BOOL bDrawing;
static int cxBeginPoint, cyBeginPoint,cxCurrPoint, cyCurrPoint;
static int chang,kuan;
static int OldRop;
static HPEN hRedDashdotPen,hOldPen;
chang=cxCurrPoint-cxBeginPoint;//长等于坐标只差,可以为负值
kuan=cyCurrPoint-cyBeginPoint;
hfather=hWnd;//////////////////////
switch (message)
{
case WM_CREATE: //创建内存设备描述表
hdc = GetDC (hWnd);
hdcMem = CreateCompatibleDC (hdc);
hRedDashdotPen = CreatePen (PS_DASHDOT, 1, RGB (255, 0, 0));
ReleaseDC (hWnd, hdc);
return 0;
case WM_LBUTTONDOWN:
if (!bDrawing)
SetCapture (hWnd);
//设置绘图的起始、终止点
cxBeginPoint = LOWORD (lParam);
cyBeginPoint = HIWORD (lParam);
cxCurrPoint = LOWORD (lParam);
cyCurrPoint = HIWORD (lParam);
bDrawing = TRUE;
return 0;
case WM_LBUTTONUP:
if (bDrawing)
{
SetCapture (NULL);
bDrawing = FALSE ;
hdc = GetDC (hWnd);
OldRop=SetROP2 (hdc,R2_XORPEN);
hOldPen = (HPEN)SelectObject (hdc, (HPEN)hRedDashdotPen);
MoveToEx(hdc,cxBeginPoint,cyBeginPoint,NULL);
LineTo(hdc,cxCurrPoint,cyBeginPoint);
LineTo(hdc,cxCurrPoint,cyCurrPoint);
LineTo(hdc,cxBeginPoint,cyCurrPoint);
LineTo(hdc,cxBeginPoint,cyBeginPoint);
SelectObject (hdc, hOldPen);
SetROP2 (hdc,OldRop);
if (hSelectBitmap!=NULL)
DeleteObject(hSelectBitmap);
hSelectBitmap = CreateCompatibleBitmap (hdc, abs(cxCurrPoint-cxBeginPoint),
abs(cyCurrPoint-cyBeginPoint));
hOldBitmap = (HBITMAP)SelectObject (hdcMem, hSelectBitmap);
GetObject (hSelectBitmap, sizeof (BITMAP), &bm);
StretchBlt(hdcMem,0,0,bm.bmWidth,bm.bmHeight,hdc,cxBeginPoint,cyBeginPoint,chang,kuan,SRCCOPY);
SelectObject (hdcMem, hOldBitmap);
SendMessage(hWnd,WM_COMMAND,0,0);
ShowWindow( hWnd, SW_HIDE);
ShowWindow(hchild, SW_SHOWDEFAULT);
show=FALSE;zhua=FALSE;
ReleaseDC (hWnd, hdc);
}
return 0 ;
case WM_MOUSEMOVE:
if (!bDrawing)
return 0 ;
hdc = GetDC (hWnd);
OldRop=SetROP2 (hdc,R2_XORPEN);
hOldPen = (HPEN)SelectObject (hdc, (HPEN)hRedDashdotPen);
MoveToEx(hdc,cxBeginPoint,cyBeginPoint,NULL);
LineTo(hdc,cxCurrPoint,cyBeginPoint);
LineTo(hdc,cxCurrPoint,cyCurrPoint);
LineTo(hdc,cxBeginPoint,cyCurrPoint);
LineTo(hdc,cxBeginPoint,cyBeginPoint);
cxCurrPoint = LOWORD (lParam);
cyCurrPoint = HIWORD (lParam);
MoveToEx(hdc,cxBeginPoint,cyBeginPoint,NULL);
LineTo(hdc,cxCurrPoint,cyBeginPoint);
LineTo(hdc,cxCurrPoint,cyCurrPoint);
LineTo(hdc,cxBeginPoint,cyCurrPoint);
LineTo(hdc,cxBeginPoint,cyBeginPoint);
SelectObject (hdc, hOldPen);
//SetROP2 (hdc,OldRop);
if(show){
char szShow[32];
RECT rect;
rect.top=cyBeginPoint-15;rect.left=cxBeginPoint+10;
rect.right=cxBeginPoint+310;rect.bottom=cyBeginPoint;
SetTextColor (hdc,RGB(255,0,0));
wsprintf (szShow, "像素大小%d * %d =%d (约%d K) ", chang, kuan,chang*kuan,chang*kuan/1024);
DrawText (hdc, szShow , -1, &rect, DT_LEFT);
}
ReleaseDC (hWnd, hdc);
return 0;
case WM_PAINT:
if(zhua==TRUE){
hdc2=CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL);
hdc=GetDC(hchild);
GetClientRect(hchild, &rect);
StretchBlt(hdc, 0, 31,rect.right-rect.left,rect.bottom-rect.top-31,
hdc2,0 ,0 ,1024,768,SRCCOPY);
}
return 0;
case WM_COMMAND:
if (hSelectBitmap==NULL)
return 0;
hdcDest = GetDC (hchild);
//GetClientRect(hchild,&rect);
hOldBitmap = (HBITMAP)SelectObject (hdcMem, hSelectBitmap);
GetObject (hSelectBitmap, sizeof (BITMAP), &bm);
//StretchBlt(hdcDest,0,31,rect.right ,rect.bottom,hdcMem,0,0,bm.bmWidth ,bm.bmHeight ,SRCCOPY);
BitBlt(hdcDest,0,31,bm.bmWidth,bm.bmHeight,hdcMem,0,0,SRCCOPY);
SelectObject (hdcMem, hOldBitmap);
if(save){
WriteBmp(hSelectBitmap,FileName);
}
ReleaseDC (hchild,hdcDest);
return 0;
case WM_DESTROY:
DeleteDC (hdcMem);
DeleteObject (hRedDashdotPen);
PostQuitMessage (0);
return 0;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}
LRESULT CALLBACK Winshow(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{ RECT rect;
HDC hdc;
HWND hbutton1,hbutton2,hbutton3;
PA