代码改变世界

位图和位块传输(4)位图画刷与画画

2012-08-10 20:42  java线程例子  阅读(264)  评论(0编辑  收藏  举报

位图同样可以用做画刷,例如博客位图和位块传输(3)中的墙,可以使用位图画刷来完成

//在WinMain函数中完成
HBITMAP hBitmap;
HBRUSH hBrush;
hBitmap=LoadBitmap(hInstance,MAKEINTRESOURCE(BITMAPID));
hBrush=CreatePatternBrush(hBitmap);
DeleteObject(hBitmap);
wndclass.hbrBackground=hBrush;


同样可以在位图上画画。

#include<windows.h>
#include"resource.h"

LRESULT CALLBACK WindowProc(
							HWND hwnd,      // handle to window
							UINT uMsg,      // message identifier
							WPARAM wParam,  // first message parameter
							LPARAM lParam   // second message parameter
							);

int WINAPI WinMain(
				   HINSTANCE hInstance,      // handle to current instance
				   HINSTANCE hPrevInstance,  // handle to previous instance
				   LPSTR lpCmdLine,          // command line
				   int nCmdShow              // show state
				   )
{
	static TCHAR szAppName[]=TEXT("leidemingzi");
	MSG msg;
	HWND hwnd;
	WNDCLASS wndclass;

	wndclass.cbClsExtra=0;
	wndclass.cbWndExtra=0;
	wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
	wndclass.hInstance=hInstance;
	wndclass.lpfnWndProc=WindowProc;
	wndclass.lpszClassName=szAppName;
	wndclass.lpszMenuName=NULL;
	wndclass.style=CS_HREDRAW|CS_VREDRAW;

	if(!RegisterClass(&wndclass))
	{
		MessageBox(NULL,TEXT("the program require the window nt"),TEXT("tips"),MB_ICONERROR);
		return 0;
	}

	hwnd=CreateWindow(
		szAppName,  // registered class name
		TEXT("this is title"), // window name
		WS_OVERLAPPEDWINDOW,        // window style
		CW_USEDEFAULT,                // horizontal position of window
		CW_USEDEFAULT,                // vertical position of window
		CW_USEDEFAULT,           // window width
		CW_USEDEFAULT,          // window height
		NULL,      // handle to parent or owner window
		NULL,          // menu handle or child identifier
		hInstance,  // handle to application instance
		NULL		// window-creation data
		);

	ShowWindow(hwnd,nCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	
	return msg.wParam;
}

LRESULT CALLBACK WindowProc(
							HWND hwnd,      // handle to window
							UINT uMsg,      // message identifier
							WPARAM wParam,  // first message parameter
							LPARAM lParam   // second message parameter
							)
{
	static HBITMAP hBtimap;
	static HDC hdcMem;
	static int cxBitmap,cyBitmap,cxClient,cyClient,iSize=LARGE;
	HDC  hdc;
	static int zhuangtai;
	int x,y;
	PAINTSTRUCT ps;
	static SIZE size;
	static TCHAR *szText=TEXT(" Hello, world!! ");


	switch(uMsg)
	{
	case WM_CREATE:
		hdc=GetDC(hwnd);
		hdcMem=CreateCompatibleDC(hdc);
		GetTextExtentPoint32(hdc,szText,lstrlen(szText),&size);
		cxBitmap=size.cx;
		cyBitmap=size.cy;
		hBtimap = CreateCompatibleBitmap(hdc,cxBitmap,cyBitmap);

		ReleaseDC(hwnd,hdc);
		SelectObject(hdcMem,hBtimap);
		TextOut(hdcMem,0,0,szText,lstrlen(szText));
		
		return 0;

	case WM_SIZE:
		cxClient=LOWORD(lParam);
		cyClient=HIWORD(lParam);
		return 0;

	case WM_LBUTTONDOWN:
		zhuangtai=LARGE;
		return 0;

	case WM_RBUTTONDOWN:
		zhuangtai=SMALL;
		return 0;

	case WM_PAINT:
		hdc=BeginPaint(hwnd,&ps);
		switch(zhuangtai)
		{
		case LARGE:
			StretchBlt(hdc,0,0,cxClient,cyClient,hdcMem,0,0,cxBitmap,cyBitmap,SRCCOPY);
			break;

		case SMALL:
			for(y=0;y<cyClient;y+=cyBitmap)
				for(x=0;x<cxClient;x+=cxBitmap)
					BitBlt(hdc,x,y,cxBitmap,cyBitmap,hdcMem,0,0,SRCCOPY);
			break;

		}
		InvalidateRect(hwnd,NULL,FALSE);
		EndPaint(hwnd,&ps);
		return 0;

	case WM_DESTROY:
		DeleteDC(hdcMem);
		DeleteObject(hBtimap);

		PostQuitMessage(0);
		return 0;
	}

	return DefWindowProc(hwnd,uMsg,wParam,lParam);
}



按右键左键可以切换: