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

📄 winmain.cpp

📁 用VC模拟数字通信系统
💻 CPP
字号:

#include "stdafx.h"
#include "stdio.h"
#include "DCS.H"

#define TIMES 1000
#define NUMBER_OF_PIXEL 500
HINSTANCE hInst;
HPEN black_Pen,red_Pen,green_Pen,blue_Pen;
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
void				Paint(HDC hdc);
void				Reflesh(HDC hdc);
void				DrawAxis(int x0,int y0,int lx,int ly,HDC hdc);
int	pixel1=0;
int pixel2=0;
int pixel3=0;
int state=0;
double k=0;
int y_position1[NUMBER_OF_PIXEL];
int y_position2[NUMBER_OF_PIXEL];
int y_position3[NUMBER_OF_PIXEL];

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     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		= NULL;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= "DCS";
	wcex.hIconSm		= NULL;

	return RegisterClassEx(&wcex);
}


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

	hInst = hInstance;

	hWnd = CreateWindow("DCS", "DCS" , WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

	if (!hWnd)
	{
		return FALSE;
	}
	srand((unsigned   int)time(NULL)); 
	MoveWindow(hWnd,100,100,660,450,true);
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);


	black_Pen = CreatePen(0,1,RGB(0,0,0));
	red_Pen = CreatePen(0,1,RGB(200,50,50));
	green_Pen = CreatePen(0,1,RGB(50,200,50));
	blue_Pen = CreatePen(0,1,RGB(50,50,200));
	hdc = GetDC(hWnd);
	DrawAxis(40,400,530,350,hdc);
	Paint(hdc);
	ReleaseDC(hWnd,hdc);
	return TRUE;
}


void DrawAxis(int x0,int y0,int lx,int ly,HDC hdc)
{
		SelectObject(hdc,black_Pen);
		MoveToEx(hdc,x0,y0,NULL);	 
		LineTo(hdc,x0,y0-ly);
		LineTo(hdc,x0+10,y0-ly+10);
		MoveToEx(hdc,x0,y0-ly,NULL);
		LineTo(hdc,x0-10,y0-ly+10);
		MoveToEx(hdc,x0,y0,NULL);
		LineTo(hdc,x0+lx,y0);
		LineTo(hdc,x0+lx-10,y0-10);
		MoveToEx(hdc,x0+lx,y0,NULL);
		LineTo(hdc,x0+lx-10,y0+10);
		int axis;
		int x_marker=0;
		int y_marker=6;
		char str[20];
		SetTextColor(hdc,RGB(0,0,255));
		for(axis=x0;axis<x0+lx;axis=axis+50)
		{
			MoveToEx(hdc,axis,y0,NULL);
			LineTo(hdc,axis,y0-10);
			sprintf(str,"%d",x_marker);
			TextOut(hdc,axis,y0+5,str,strlen(str));
			x_marker=x_marker+4;
		}
		for(axis=y0;axis>y0-ly;axis=axis-50)
		{
			MoveToEx(hdc,x0,axis,NULL);
			LineTo(hdc,x0+10,axis);
			sprintf(str,"10");
			TextOut(hdc,x0-35,axis,str,strlen(str));
			sprintf(str,"-%d",y_marker);
			TextOut(hdc,x0-15,axis-5,str,strlen(str));
			y_marker=y_marker-1;
		}
		SetTextColor(hdc,RGB(255,0,0));
		sprintf(str,"E0/N0(dB)");
		TextOut(hdc,x0+lx+5,y0,str,strlen(str));
		sprintf(str,"PB");
		TextOut(hdc,x0-5,y0-ly-20,str,strlen(str));
}
void Paint(HDC hdc)
{
	double pb;
	char str[30];
	int  percent;
	SelectObject(hdc,red_Pen);
	MoveToEx(hdc,40,100,NULL);
	SetTextColor(hdc,RGB(200,50,50));
	for(;pixel1<NUMBER_OF_PIXEL;pixel1++)
	{
		MSG msg;
		if( msg.message==WM_QUIT )
		{
			DeleteObject(black_Pen);
			DeleteObject(red_Pen);
			PostQuitMessage(0);
			return;
		}
        if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
		percent=pixel1/double(NUMBER_OF_PIXEL/100);
		sprintf(str,"16PSK+Code:%d%%",percent);
		TextOut(hdc,500,20,str,strlen(str));
		k=sqrt(0.5*pow(10.0,double(pixel1)/125.0));
		pb=double(DCS_Code(k,TIMES))/double(16)/double(TIMES);
		if(pb==0)
			pb=0.000001;
		pb=-10*log10(pb);
		y_position1[pixel1]=100+int(pb*5);
		LineTo(hdc,pixel1+40,y_position1[pixel1]);
	}
	sprintf(str,"16PSK+Code complete  ");
	TextOut(hdc,500,20,str,strlen(str));
	state=1;
	SelectObject(hdc,green_Pen);
	MoveToEx(hdc,40,100,NULL);
	SetTextColor(hdc,RGB(50,200,50));
	for(;pixel2<NUMBER_OF_PIXEL;pixel2++)
	{
		MSG msg;
		if( msg.message==WM_QUIT )
		{
			DeleteObject(black_Pen);
			DeleteObject(red_Pen);
			PostQuitMessage(0);
			return;
		}
        if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
		percent=pixel2/double(NUMBER_OF_PIXEL/100);
		sprintf(str,"16PSK :%d%%",percent);
		TextOut(hdc,500,40,str,strlen(str));
		k=sqrt(0.5*pow(10.0,double(pixel2)/125.0));
		pb=double(DCS_Uncode(k,TIMES))/double(16)/double(TIMES);
		if(pb==0)
			pb=0.000001;
		pb=-10*log10(pb);
		y_position2[pixel2]=100+int(pb*5);
		LineTo(hdc,pixel2+40,y_position2[pixel2]);
	}
	state=2;
	sprintf(str,"16PSK complete  ");
	TextOut(hdc,500,40,str,strlen(str));
	SelectObject(hdc,blue_Pen);
	MoveToEx(hdc,40,100,NULL);
	SetTextColor(hdc,RGB(50,50,200));
	for(;pixel3<NUMBER_OF_PIXEL;pixel3++)
	{
		MSG msg;
		if( msg.message==WM_QUIT )
		{
			DeleteObject(black_Pen);
			DeleteObject(red_Pen);
			PostQuitMessage(0);
			return;
		}
        if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
		percent=pixel3/double(NUMBER_OF_PIXEL/100);
		sprintf(str,"QPSK+Code :%d%%",percent);
		TextOut(hdc,500,60,str,strlen(str));
		k=sqrt(0.5*pow(10.0,double(pixel3)/125.0));
		pb=double(DCS_QPSK(k,TIMES))/double(16)/double(TIMES);
		if(pb==0)
			pb=0.000001;
		pb=-10*log10(pb);
		y_position3[pixel3]=100+int(pb*5);
		LineTo(hdc,pixel3+40,y_position3[pixel3]);
	}
	state=3;
	sprintf(str,"QPSK+Code complete  ");
	TextOut(hdc,500,60,str,strlen(str));
}
void Reflesh(HDC hdc)
{
	char str[30];
	DrawAxis(40,400,530,350,hdc);
	SetTextColor(hdc,RGB(128,128,255));
	MoveToEx(hdc,40,100,NULL);
	SelectObject(hdc,red_Pen);
	int loop;
	for(loop=0;loop<pixel1;loop++)
	{
		LineTo(hdc,loop+40,y_position1[loop]);
	}
	if(state>=1)
	{
		MoveToEx(hdc,40,100,NULL);
		SelectObject(hdc,green_Pen);
		for(loop=0;loop<pixel2;loop++)
		{
			LineTo(hdc,loop+40,y_position2[loop]);
		}
		SetTextColor(hdc,RGB(200,50,50));
		sprintf(str,"16PSK+code complete  ");
		TextOut(hdc,500,20,str,strlen(str));
	}
	if(state>=2)
	{
		MoveToEx(hdc,40,100,NULL);
		SelectObject(hdc,blue_Pen);
		for(loop=0;loop<pixel3;loop++)
		{
			LineTo(hdc,loop+40,y_position3[loop]);
		}
		SetTextColor(hdc,RGB(50,200,50));
		sprintf(str,"16PSK complete  ");
		TextOut(hdc,500,40,str,strlen(str));
	}
	if(state>=3)
	{
		SetTextColor(hdc,RGB(50,50,200));
		sprintf(str,"QPSK+Code complete  ");
		TextOut(hdc,500,60,str,strlen(str));
	}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message) 
	{
		case WM_PAINT:						
			hdc = BeginPaint(hWnd, &ps);
			Reflesh(hdc);
			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:				
			DeleteObject(black_Pen);
			DeleteObject(red_Pen);
			PostQuitMessage(0);
			break;
		default:						
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

⌨️ 快捷键说明

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