📄 main.cpp
字号:
#include <windows.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "gaTSP.h"
#include "define.h"
//using namespace std;
char* szApplicationName = "Chapter4 - The TSP";
char* szWindowClassName = "TSPclass";
CgaTSP* g_pTSP;
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
HDC hdc;
PAINTSTRUCT ps;
//these hold the dimensions of the client window area
static int cxClient, cyClient;
//used to create the back buffer
static HDC hdcBackBuffer;
static HBITMAP hBitmap;
static HBITMAP hOldBitmap;
switch( msg )
{
case WM_CREATE:
{
//显示每一代
srand((unsigned) time(NULL));
RECT rect;
GetClientRect(hwnd, &rect);
cxClient = rect.right;
cyClient = rect.bottom;
//create a surface for us to render to(backbuffer)
hdcBackBuffer = CreateCompatibleDC(NULL);
HDC hdc = GetDC(hwnd);
hBitmap = CreateCompatibleBitmap(hdc,
cxClient,
cyClient);
ReleaseDC(hwnd, hdc);
hOldBitmap = (HBITMAP)SelectObject(hdcBackBuffer, hBitmap);
g_pTSP = new CgaTSP( MUTATION_RATE,
CROSSOVER_RATE,
POP_SIZE,
NUM_CITIES,
WINDOW_WIDTH,
WINDOW_HEIGHT );
}
break;
case WM_KEYUP:
{
switch( wparam )
{
case VK_RETURN:
{
g_pTSP->Run( hwnd );
}
break;
case VK_ESCAPE:
{
PostQuitMessage( 0 );
}
break;
case VK_SPACE:
{
g_pTSP->Stop();
}
break;
}// end switch
}
break;
case WM_SIZE:
{
cxClient = LOWORD(lparam);
cyClient = HIWORD(lparam);
g_pTSP->Resize(cxClient, cyClient);
SelectObject(hdcBackBuffer, hOldBitmap);
HDC hdc = GetDC(hwnd);
hBitmap = CreateCompatibleBitmap(hdc,
cxClient,
cyClient);
ReleaseDC(hwnd, hdc);
hOldBitmap = (HBITMAP)SelectObject(hdcBackBuffer, hBitmap);
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
BitBlt(hdcBackBuffer,
0, 0,
cxClient,
cyClient,
NULL,
NULL,
NULL,
WHITENESS);
g_pTSP->Render( hdcBackBuffer, cxClient, cyClient );
BitBlt(ps.hdc, 0, 0, cxClient, cyClient, hdcBackBuffer, 0, 0, SRCCOPY);
ReleaseDC(hwnd, hdc);
EndPaint(hwnd, &ps);
}
break;
case WM_DESTROY:
{
SelectObject(hdcBackBuffer, hOldBitmap);
DeleteDC(hdcBackBuffer);
DeleteObject(hBitmap);
PostQuitMessage(0);
}
break;
}//end switch
return (DefWindowProc(hwnd, msg, wparam, lparam));
}//end WinProc
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX winclass;
HWND hwnd;
MSG msg;
// first fill in the window class stucture
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground= NULL; //(HBRUSH)GetStockObject(WHITE_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName= szWindowClassName;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// register the window class
if (!RegisterClassEx(&winclass))
return 0;
// create the window
if (!(hwnd = CreateWindowEx(NULL,
szWindowClassName,
szApplicationName,
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
GetSystemMetrics(SM_CXSCREEN)/2 - WINDOW_WIDTH/2,
GetSystemMetrics(SM_CYSCREEN)/2 - WINDOW_HEIGHT/2,
WINDOW_WIDTH,WINDOW_HEIGHT,
NULL,
NULL,
hinstance,
NULL)))
return 0;
ShowWindow(hwnd, ncmdshow);
UpdateWindow(hwnd);
bool bDone = false;
while( !bDone )
{
while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
if( msg.message == WM_QUIT )
{
bDone = true;
}
else
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
if( g_pTSP->Started() )
{
g_pTSP->Epoch();
InvalidateRect( hwnd, NULL, TRUE );
UpdateWindow( hwnd );
}
}//end while
UnregisterClass( szWindowClassName, winclass.hInstance );
if (g_pTSP)
{
delete g_pTSP;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -