⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gridandcirle.cpp

📁 c++从入门到精通
💻 CPP
字号:
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(	HINSTANCE hInstance,
				   HINSTANCE hPrevInst,
				   LPSTR chCmdLine,
				   int nCmdShow)
{
	HWND hwnd ;
	MSG Msg ;
	WNDCLASS wndclass ;
    char lpszClassName[] = "网格与圆"; 
    char lpszTitle[]= "网格与圆小程序";
    wndclass.style = 0; 
	wndclass.lpfnWndProc = WndProc ;
	wndclass.cbClsExtra	= 0 ;
	wndclass.cbWndExtra	= 0 ;
	wndclass.hInstance = hInstance ;
	wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION) ;
	wndclass.hCursor = LoadCursor( NULL, IDC_ARROW) ;
	wndclass.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH) ;
	wndclass.lpszMenuName = NULL ;
	wndclass.lpszClassName = lpszClassName ;
	
	if( !RegisterClass( &wndclass))
	{
		MessageBeep(0) ;
		return FALSE ;
	}

	hwnd = CreateWindow(	lpszClassName,
							lpszTitle,
							WS_OVERLAPPEDWINDOW,
							CW_USEDEFAULT,
							CW_USEDEFAULT,
							310,
							330,
							NULL,
							NULL,
							hInstance,
							NULL) ;

	ShowWindow( hwnd, nCmdShow) ;//显示窗口
	UpdateWindow(hwnd);	//绘制用户区
	while( GetMessage(&Msg, NULL, 0, 0))//消息循环
	{
		TranslateMessage( &Msg) ;
		DispatchMessage( &Msg) ;
	}

	return Msg.wParam;//消息循环结束时将信息返回系统
}

//窗口函数
LRESULT CALLBACK WndProc(HWND hwnd,
						 UINT message,
						 WPARAM  wParam,
						 LPARAM  lParam)
{
	HDC hdc;
	static PAINTSTRUCT ps;
	static HBRUSH hB1,hB2;
	static HPEN hP1;
	static int nMode;
	static int flags=0;
	static int flag[10][10];
	static int nlbuttonx=0,nlbuttony=0;
	int i=0;
	int j=0;
	switch(message){
	//窗口创建时设置映像模式为MM_TEXT
	case WM_CREATE:
		flags=0;
		for(i=0;i<10;i++)
			for(j=0;j<10;j++)
				flag[i][j]=0;
	//	hB1=(HBRUSH)GetStockObject(BLACK_BRUSH);
		break;
	//单击鼠标左键更改映像模式为MM_ISOTROPIC
	case WM_LBUTTONDOWN:
		flags=1;
		nlbuttonx=LOWORD(lParam);
		nlbuttony=HIWORD(lParam);
		hB1=(HBRUSH)GetStockObject(BLACK_BRUSH);
		InvalidateRect(hwnd,NULL,0);
		break;
	//单击鼠标右键更改显示模式为MM_ANISOTROPIC
	case WM_RBUTTONDOWN:
		flags=2;
		nlbuttonx=LOWORD(lParam);
		nlbuttony=HIWORD(lParam);
		hB2=CreateSolidBrush(RGB(0,255,0));
		InvalidateRect(hwnd,NULL,0);
		break;
	case WM_PAINT:
		hdc=BeginPaint(hwnd,&ps);					
		SetWindowExtEx(hdc,300,300,NULL); 
		SetWindowOrgEx(hdc,0,0,NULL);
		SetViewportExtEx(hdc,300,300,NULL);			
		hP1=CreatePen(PS_INSIDEFRAME,2,RGB(125,125,125));//创建灰色画笔
		
		SelectObject(hdc,hP1);//将灰色画笔选入设备环境。
		if(flags&&(nlbuttonx/30<10)&&(nlbuttony/30<10))
		{			
		    if(flags==1)
			{
				flag[nlbuttonx/30][nlbuttony/30]=1;
				SelectObject(hdc,hB1);
			}	
			if(flags==2)
			{
				flag[nlbuttonx/30][nlbuttony/30]=2;
				SelectObject(hdc,hB2);
			}
			Ellipse(
				hdc,
				(nlbuttonx/30)*30,
				(nlbuttony/30)*30,
				(nlbuttonx/30)*30+30,
				(nlbuttony/30)*30+30);
			
		}
		for(i=0;i<10;i++)
		{
			for(j=0;j<10;j++)
				if(flag[i][j]==1)
				{
					SelectObject(hdc,hB1);
					Ellipse(hdc,
					i*30,
					j*30,
				    i*30+30,
					j*30+30);
				}
				else if(flag[i][j]==2)
				{
					SelectObject(hdc,hB2);
					Ellipse(hdc,
					i*30,
					j*30,
				    i*30+30,
					j*30+30);
				}
			

		}
		for(i=30;i<=300;)
		{
			MoveToEx(hdc,i,0,NULL);
			LineTo(hdc,i,300);
			i=i+30;
		}
		for(i=30;i<=300;)
		{
			MoveToEx(hdc,0,i,NULL);
			LineTo(hdc,300,i);
			i=i+30;
		}
	EndPaint(hwnd,&ps);	//释放设备环境句柄
	break;
case WM_DESTROY:
		DeleteObject(hB1);	
		DeleteObject(hB2);
		DeleteObject(hP1);
	 	PostQuitMessage(0);
		break;   
default:
		return  DefWindowProc(hwnd,message,wParam,lParam);
		}
return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -