windows编程中画笔与画刷

#include <windows.h>
#include <iostream.h>
//声明窗口函数原型
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
BOOL Initinstance(HINSTANCE hInstance,int nCmdShow);
void MyPaint(HDC hdc);
ATOM MyRegisterClass(HINSTANCE hInstance);

HINSTANCE hInst;
HPEN hPen[7];
HBRUSH hBru[7];
int sPen[7]={PS_SOLID,PS_DASH,PS_DOT,PS_DASHDOT,PS_DASHDOTDOT,PS_NULL,PS_INSIDEFRAME};
int sBru[7]={HS_VERTICAL,HS_HORIZONTAL,HS_CROSS,HS_DIAGCROSS,HS_FDIAGONAL,HS_BDIAGONAL};

//--------------------------------------------
//主函数
int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE PreInstance,
				   LPSTR lpCmdLine,
				   int nCmdShow)
{							//定义窗口句柄
	MSG msg;											//定义一个用来存储消息的变量

	MyRegisterClass(hInstance);

	if(!Initinstance(hInstance,nCmdShow)) 
		return false;

	while(GetMessage(&msg,NULL,0,0))					//消息循环
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;

}
//-------------------------------------
//处理消息的窗口函数
ATOM MyRegisterClass(HINSTANCE hInstance){
	WNDCLASSEX wcex;
	
	wcex.cbSize = sizeof(WNDCLASSEX);  
	
	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = (WNDPROC)WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = NULL;
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = "canvas";
	wcex.hIconSm = NULL;
	
	return RegisterClassEx(&wcex);
}

BOOL Initinstance(HINSTANCE hInstance,int nCmdShow){
	HWND hWnd;
	HDC hdc;
	int i;

	hInst = hInstance;

	hWnd = CreateWindow("canvas","绘图窗口",WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

	if(!hWnd) return FALSE;
	MoveWindow(hWnd,10,10,650,350,true);
	ShowWindow(hWnd,nCmdShow);
	UpdateWindow(hWnd);

	for(i=0;i<7;i++){
		hPen[i] = CreatePen(sPen[i],1,RGB(255,0,0));
		if(i==6)
			hBru[i] = CreateSolidBrush(RGB(0,255,0));
		else
			hBru[i] = CreateHatchBrush(sBru[i],RGB(0,255,0));
	}

	hdc = GetDC(hWnd);
	MyPaint(hdc);
	ReleaseDC(hWnd,hdc);

	return TRUE;
}

void MyPaint(HDC hdc){
	int i,x1,x2,y;

	for(i=0;i<7;i++){
		y = (i+1) * 30;

		SelectObject(hdc,hPen[i]);
		MoveToEx(hdc,30,y,NULL);
		LineTo(hdc,100,y);
	}
	
	x1 = 120;
	x2 = 180;

	for(i=0;i<7;i++){
		SelectObject(hdc,hBru[i]);
		Rectangle(hdc,x1,30,x2,y);
		x1 += 70;
		x2 += 70;
	}
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam){
	PAINTSTRUCT ps;
	HDC hdc;
	int i;

	switch(message){
		case WM_PAINT:
			{
				hdc = BeginPaint(hWnd,&ps);
				MyPaint(hdc);
				EndPaint(hWnd,&ps);
			}
		break;
		case WM_DESTROY:
			{
				for(i = 0;i < 7 ;i++){
					DeleteObject(hPen[i]);
					DeleteObject(hBru[i]);
				}
				PostQuitMessage(0);
			}
		break;
		default:
			return DefWindowProc(hWnd,message,wParam,lParam);
	}
	return 0;
}


 

你可能感兴趣的:(编程,windows,null,callback,include,winapi)