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

📄 main.cpp

📁 一个遗传算法的VC版本
💻 CPP
字号:
// DEMO2_5.CPP - Creates two windows based on the same 
// window class
// INCLUDES ///////////////////////////////////////////////
//#define WIN32_LEAN_AND_MEAN  // just say no to MFC

#include "control.h"
// DEFINES ////////////////////////////////////////////////

// defines for windows 
#define WINDOW_CLASS_NAME "WINCLASS1"

// GLOBALS ////////////////////////////////////////////////
bool         bdrawline=1;                    ///表示是否画直线1未画
HINSTANCE     g_hinstance;						// 当前实例
char c_buffer[4][5];                         //char数组类型临时变量
RECT          rect; 

Control ControlObject;

// FUNCTIONS //////////////////////////////////////////////
float StringToInt(string str,bool bflg )
{
	int size=str.size( );
	float result=0;
	for(int n=0;n<size;n++)
	{ 
		result +=str[n]-48;
		result *=10 ;
	}
	result/=10;
	if( bflg==0)                    //这一段处理字符串为 大于1的
	{ return result;	}
	while( size-- )
	{ result/=10;}                 //获得 1到 0
	return result;
}

//int iLength=GetWindowTextLength(hwndButton[0]);  //取得按钮文本长度
//	GetWindowText(hwndButton[0],c_buffer,iLength+1);   //取得按钮当前文本	
//	s_buffer=c_buffer;
LRESULT CALLBACK AboutGASet(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_INITDIALOG:                                         //初始化对话框 
		SetDlgItemInt (hDlg, IDC_EDIT2, ControlObject.Getpcross( )*100000  , TRUE );
		SetDlgItemInt (hDlg, IDC_EDIT3, ControlObject.Getpmutation( )*100000 , TRUE );
		SetDlgItemInt (hDlg, IDC_EDIT4, ControlObject.Getpopsize( ) , TRUE );
		SetDlgItemInt (hDlg, IDC_EDIT6, ControlObject.Getmaxgen( ) , TRUE );

		CheckRadioButton(hDlg,IDC_RADIO1,IDC_RADIO2,1019+ControlObject.GetPower( ) );
		return TRUE;
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{	
		case IDC_RADIO1:
			{
				ControlObject.SetPower( 0);
				break;
			}
			case IDC_RADIO2:
			{
				ControlObject.SetPower( 1);
				break;
			}
		case IDOK:
			{
				GetDlgItemText(hDlg, IDC_EDIT2,c_buffer[0],5);
				GetDlgItemText(hDlg, IDC_EDIT3,c_buffer[1],5);
				GetDlgItemText(hDlg, IDC_EDIT4,c_buffer[2],5);
				GetDlgItemText(hDlg, IDC_EDIT6,c_buffer[3],5);
              ControlObject.SetGaInformation(StringToInt(c_buffer[0],1),StringToInt(c_buffer[1],1),
				               StringToInt(c_buffer[2],0),StringToInt(c_buffer[3],0) );
	          EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
		case IDCANCEL:
			{		
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}	
		case IDDefault:                        //恢复默认值 
			{
				ControlObject.SetGaInformation(0.6,0.2,300,300);
				ControlObject.SetPower(1);
               SendMessage(hDlg,WM_INITDIALOG,0,0);
						return TRUE;
			}
		break;
		}break;/*
	case EM_GETSEL :
		{
			//rect.bottom=100;
		//	rect.bottom=100;//EditWordBreakProcEx( IDC_EDIT2,0, 0,  0,WB_LEFTBREAK);
			//GetRoleTextW(,n,1);
		
		int iLength=GetWindowTextLength(hDlg );  //取得按钮文本长度
		GetWindowText(hDlg ,c_buffer,iLength+1);   //取得按钮当前文本	
		}break;
		case IDC_EDIT2 :
		{
			//rect.bottom=100;
		//	rect.bottom=100;//EditWordBreakProcEx( IDC_EDIT2,0, 0,  0,WB_LEFTBREAK);
			//GetRoleTextW(,n,1);
		int iLength=GetWindowTextLength( hDlg );  //取得按钮文本长度
		GetWindowText( hDlg ,c_buffer,iLength+1);   //取得按钮当前文本	
		}break;*/
		
	}
	return FALSE;
}


///////////////////        WindowProc    ///////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
{
// this is the main message handler of the system
PAINTSTRUCT		ps;		// used in WM_PAINT
HDC				hdc;	// handle to a device context

static POINT point;        ///临时点

rect.bottom=100;///// 无效区域 (网格以外的区域)
rect.left=0;
rect.right=48;
rect.top=0;
// what is the message 
  
switch(msg)
{	
    case WM_CREATE: 
		{
		// do initialization stuff here
			ControlObject.welcome( hwnd );
      //     return success		
		return(0);
		} break;
		
	case WM_PAINT: 
		{
			
		// simply validate the window
		hdc = BeginPaint(hwnd,&ps);	 
		// you would do all your painting here
       
		//画地图	
		ControlObject.DrawMap(hwnd ,hdc);
		ControlObject.DisPlay(hwnd,point,bdrawline);
		ControlObject.DrawAllPoint(hwnd );

		if( bdrawline==0)                //已画直线但未消除直线     
		{ ControlObject.DrawLine(hwnd );}
		// return success
   		} break;

     case WM_MOUSEMOVE :
		 {
			point.x=(int)LOWORD(lparam);
			point.y=(int)HIWORD(lparam);
			
			InvalidateRect (hwnd, &rect, 1) ;//刷新消息
		 }break;
	 
	case WM_LBUTTONDOWN:
		{		
			ControlObject.DrawTruePoint(hwnd,point );
			ControlObject.DrawAllPoint(hwnd );			
			if( bdrawline==0)
			{ InvalidateRect (hwnd,NULL, 1) ;} //刷新消息
			InvalidateRect (hwnd,&rect, 1) ;  //刷新消息
			bdrawline=1;                    //标志画直线后已清除直线
		}break;

	case WM_RBUTTONDOWN:
		{		
			ControlObject.DrawFalsePoint(hwnd,point );
			ControlObject.DrawAllPoint(hwnd );
			InvalidateRect (hwnd, NULL, 1) ;//刷新消息
			bdrawline=1;
		}break;

	case WM_RBUTTONDBLCLK:
		{	      
			SendMessage(hwnd,WM_COMMAND,ID_CLEAN_ALL,0);
		}break;
	case WM_KEYDOWN:
		{
			switch(LOWORD(wparam))
			{
			case VK_RETURN:
				SendMessage(hwnd,WM_COMMAND,IDOK,0);				
				break;
			}
		}break;
	case WM_COMMAND:
		{
			switch(LOWORD(wparam))
			{
			case IDOK:
				{ 
					HDC hdc;
					hdc=GetDC(hwnd);
					SetTextColor(hdc,RGB(128,0,128));
					TextOut(hdc,160,0," Please Wait",12);
					ReleaseDC (hwnd, hdc);

					ControlObject.DrawLineWait( );
					ControlObject.DrawLine( hwnd);
					bdrawline=0;
					InvalidateRect (hwnd, NULL, 1) ;//刷新消息
				} break;
			case ID_DefaultMap :
			case ID_RoundMap :
				{
					if(ControlObject.GetMapStyle( )==LOWORD(wparam) )  //如果选择类型不变,不清空点
					{ break;}
					ControlObject.CleanAllUpDate( );               //清空当前所有点
					ControlObject.SetMapStyle( hwnd,wparam);
					InvalidateRect (hwnd, NULL, 1) ;
					bdrawline=1;
				}break;
			case  ID_CLEAN_ALL:
				{
					ControlObject.CleanAllUpDate( );
					InvalidateRect (hwnd, NULL, 1) ;
					bdrawline=1;
				}break;
			case ID_GA_SET:
				{	DialogBox(g_hinstance, (LPCTSTR)IDD_GA_BOX, hwnd, (DLGPROC)AboutGASet);	}
				break;
			case ID_HELP_BOX:
				{ 	DialogBox(g_hinstance, (LPCTSTR)IDD_HELP_BOX, hwnd, (DLGPROC)AboutGASet); }      //帮助对话框 	
				break;
			case ID_END:
				{ SendMessage(hwnd,WM_COMMAND,ID_CLEAN_ALL,0);}

			default:break;
			}
		}break;

	case WM_DESTROY: 
		{
		// kill the application, this sends a WM_QUIT message 
		PostQuitMessage(0);
        // return success
		return(0);
		} break;

	default:break;

    } // end switch
// process any messages that we didn't take care of 
return (DefWindowProc(hwnd, msg, wparam, lparam));

} // end WinProc

// WINMAIN ////////////////////////////////////////////////
int WINAPI WinMain(	HINSTANCE hinstance,
					HINSTANCE hprevinstance,
					LPSTR lpcmdline,
					int ncmdshow)
{

WNDCLASSEX winclass; // this will hold the class we create
HWND	   hwnd;	 // generic window handle
MSG		   msg;		 // generic message

// first fill in the window class stucture
winclass.cbSize         = sizeof(WNDCLASSEX);
winclass.style			= CS_SAVEBITS|CS_DBLCLKS|CS_HREDRAW | CS_VREDRAW ; //CS_OWNDC | ;
winclass.lpfnWndProc	= WindowProc;
winclass.cbClsExtra		= 0;
winclass.cbWndExtra		= 0;
winclass.hInstance		= hinstance;
winclass.hIcon			=LoadIcon(hinstance, MAKEINTRESOURCE(IDI_ICON1) );
winclass.hCursor		= LoadCursor (NULL, IDC_ARROW) ;
winclass.hbrBackground	= (HBRUSH)GetStockObject(WHITE_BRUSH);
winclass.lpszMenuName	= MAKEINTRESOURCE(101);
winclass.lpszClassName	= WINDOW_CLASS_NAME;
winclass.hIconSm        = LoadIcon(hinstance, MAKEINTRESOURCE(IDI_ICON1) );

// register the window class
if (!RegisterClassEx(&winclass))
	return(0);
// create the first window
if (!(hwnd = CreateWindowEx(NULL,                  // extended style
                            WINDOW_CLASS_NAME,     // class
						    "遗传算法解决TSP问题演示", // title
						    WS_OVERLAPPED|WS_CAPTION|WS_MINIMIZEBOX| WS_VISIBLE|WS_SYSMENU,
					 	    CW_USEDEFAULT,CW_USEDEFAULT,	    // initial x,y
						    CW_USEDEFAULT,CW_USEDEFAULT,  // initial width, height
						    NULL,	    // handle to parent 
						    NULL,	    // handle to menu
						    hinstance,// instance of this application
						    NULL)))	// extra creation parms
return(0);
// enter main event loop, but this time we use PeekMessage()
// instead of GetMessage() to retrieve messages

 hinstance=g_hinstance;

while(TRUE)
	{
    // test if there is a message in queue, if so get it
	if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
	   { 		   
	   // test if this is a quit
       if (msg.message == WM_QUIT)
           break;	
	   // translate any accelerator keys
	   TranslateMessage(&msg);

	   // send the message to the window proc
	   DispatchMessage(&msg);
	   } // end if    
    // main game processing goes here	
	} // end while

// return to Windows like this
return(msg.wParam);

} // end WinMain

///////////////////////////////////////////////////////////

⌨️ 快捷键说明

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