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

📄 lines.cpp

📁 VC源代码大全(精华版)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Lines.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include	<math.h>
#include	<stdio.h>

#define MAX_LOADSTRING 100
#define	TEXT_FORMAT		(DT_SINGLELINE|DT_VCENTER|DT_CENTER)

#define	PI		3.14159
#define	TWOPI		(2 * PI)

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
void DoMouseEvent (HWND hWnd, UINT message, UINT nFlags, POINT & point, COLORREF color);
void DoMouseLine (HDC hdc, POINT & ptLineStart, POINT & ptLineEnd, COLORREF color);
void DoPolyLine (HWND hWnd, HDC hdc, bool bMenuClicked);
void DoPieChart (HWND hWnd, HDC hdc);
void DoArc (HWND hWnd, HDC hdc);
void DoPieChart2 (HWND hWnd, HDC hdc);
void CreateLogFont (HDC hdc, LOGFONT & lf, int nPoints = 10, char *szFace = "Times New Roman Bold");

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_LINES, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LINES));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
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			= LoadIcon(hInstance, (LPCTSTR)IDI_LINES);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_LINES;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int nMode = IDM_COLORS_CLEAR;
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static COLORREF crColor = 0;
static POINT	ptLast = {-1, -1};
static HCURSOR	hOldCursor;

	switch (message) 
	{
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case IDM_EXIT:
				   DestroyWindow(hWnd);
				   break;
				case IDM_COLORS_RED:
					crColor = 0x000000ff;
					break;
				case IDM_COLORS_GREEN:
					crColor = 0x0000ff00;
					break;
				case IDM_COLORS_BLUE:
					crColor = 0x00ff0000;
					break;
				case IDM_COLORS_SELECT:
					break;
				case IDM_COLORS_CLEAR:
				case IDM_LINES_POLYLINE:
				case IDM_LINES_PIECHART:
				case IDM_LINES_ARC:
					nMode = wmId;
					RECT rc;
					GetClientRect(hWnd, &rc);
					InvalidateRect (hWnd, &rc, TRUE);
					break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
//
//	the following commented lines are the first steps in using the right
//	mouse button to draw a line. To us them, simply include the following:
//	#define LINE_IN_WNDPROC
//	at this point or at the top of the file and recompile.
//
#ifdef LINE_IN_WNDPROC
		case WM_MOUSEMOVE:
			if (!(wParam & MK_RBUTTON))
				break;
		case WM_RBUTTONDOWN:
			hdc = GetDC (hWnd);
			HCURSOR hCross;
			hCross = LoadCursor (NULL, MAKEINTRESOURCE(IDC_CROSS));
			hOldCursor = SetCursor (hCross);
			SetPixel (hdc, LOWORD(lParam), HIWORD(lParam), 0);
			if (ptLast.x >= 0)
			{
				HPEN hPen = CreatePen (PS_SOLID, 2, crColor ^ 0x00ffffff);
				int nOldMode = SetROP2 (hdc, R2_XORPEN);// R2_NOT);
				MoveToEx (hdc, LOWORD(lParam), HIWORD(lParam), NULL);
				SelectObject (hdc, hPen);
				LineTo (hdc, ptLast.x, ptLast.y);
				SetROP2 (hdc, nOldMode);
				DeleteObject (hPen);
			}
			ptLast.x = LOWORD(lParam);
			ptLast.y = HIWORD(lParam);
			ReleaseDC(hWnd, hdc);
			break;
		case WM_RBUTTONUP:
			ptLast.x = -1;
			ptLast.y = -1;
			SetCursor (hOldCursor);
			break;
#else
		case WM_RBUTTONDOWN:
			HCURSOR hCross;
			hCross = LoadCursor (NULL, MAKEINTRESOURCE(IDC_CROSS));
			hOldCursor = SetCursor (hCross);
// fall through
		case WM_LBUTTONDOWN:
		case WM_LBUTTONUP:
		case WM_MOUSEMOVE:
			POINT point;
			point.x = LOWORD(lParam);
			point.y = HIWORD(lParam);
			DoMouseEvent (hWnd, message, wParam, point, crColor);
			break;
		case WM_RBUTTONUP:
			SetCursor (hOldCursor);
			DoMouseEvent (hWnd, message, wParam, point, crColor);
			break;
#endif
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			switch (nMode)
			{
				default:
				case IDM_COLORS_CLEAR:
					break;
				case IDM_LINES_POLYLINE2:
					DoPolyLine (hWnd, hdc, false);
					break;
				case IDM_LINES_POLYLINE:
					DoPolyLine (hWnd, hdc, true);
					nMode = IDM_LINES_POLYLINE2;
					break;
				case IDM_LINES_PIECHART:
					DoPieChart (hWnd, hdc);
					break;
				case IDM_LINES_ARC:
					DoArc (hWnd, hdc);
					break;
			}
			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}

void DoMouseEvent (HWND hWnd, UINT message, UINT nFlags, POINT & point, COLORREF color)
{
static bool	bTracking = false;
static POINT	ptLineStart = {0, 0};
static POINT	ptLineEnd = {0, 0};
static POINT	ptLast = {-1, -1};
HDC hdc;

	switch (message)
	{
		case WM_LBUTTONDOWN:
			ptLineStart = point;
			break;
		case WM_LBUTTONUP:
			bTracking = false;
			ptLineStart.x = 0;
			ptLineStart.y = 0;
			break;
		case WM_RBUTTONUP:
			ptLast.x = -1;
			ptLast.y = -1;
			break;
		case WM_RBUTTONDOWN:
			hdc = GetDC (hWnd);
			SetPixel (hdc, point.x, point.y, color);

⌨️ 快捷键说明

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