21xrx.com
2024-11-22 06:08:36 Friday
登录
文章检索 我的文章 写文章
玩转VC++:有趣的代码示例
2023-07-04 20:00:08 深夜i     --     --
VC++ 代码示例 程序设计 学习 计算机编程

VC++(Visual C++)是一种基于Windows操作系统的C++编程语言,并且拥有强大的Windows应用程序开发能力。在编程中,我们经常需要在实现功能的同时,保证代码的简洁易懂和易于维护性,让代码更加有趣也是一种挑战。下面就为大家带来了一些有趣的VC++代码示例,让我们一起玩转VC++。

1. 使用VC++实现的简单画图工具:

这是一个简单的画图工具,它可以在一个窗口中随意绘制各种形状,如直线、矩形、圆形等等。使用VC++实现该功能的代码如下:


#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {

  static TCHAR szAppName[] = TEXT("HelloWin");

  HWND hwnd; MSG msg; WNDCLASS wndclass;

  wndclass.style = CS_HREDRAW | CS_VREDRAW;

  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 = szAppName;

  if (!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR); return 0;}

  hwnd = CreateWindow(szAppName, TEXT("The Hello Program"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, iCmdShow); 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) {

  static HDC hdc; static int cxClient, cyClient; POINT ptEndPoint; switch (message) {

  case WM_SIZE: cxClient = LOWORD(lParam); cyClient = HIWORD(lParam); break;

  case WM_LBUTTONDOWN: SetCapture(hwnd); hdc = GetDC(hwnd); MoveToEx(hdc, LOWORD(lParam), HIWORD(lParam), NULL); break;

  case WM_MOUSEMOVE: if (wParam & MK_LBUTTON) { LineTo(hdc, LOWORD(lParam), HIWORD(lParam));} break;

  case WM_LBUTTONUP: ptEndPoint.x = LOWORD(lParam); ptEndPoint.y = HIWORD(lParam); ReleaseCapture(); if (wParam & MK_CONTROL) Rectangle(hdc, LOWORD(lParam)-50, HIWORD(lParam)-50, LOWORD(lParam)+50, HIWORD(lParam)+50); else Ellipse(hdc, LOWORD(lParam)-50, HIWORD(lParam)-50, LOWORD(lParam)+50, HIWORD(lParam)+50); ReleaseDC(hwnd, hdc); break;

  case WM_DESTROY: PostQuitMessage(0); break;

  default: return DefWindowProc(hwnd, message, wParam, lParam);

  } return 0;

}

2. 模拟计算器程序:

计算器是大家经常使用的小工具,本程序是使用VC++实现的一个简易模拟计算器程序,功能包括加、减、乘、除、取余、次方、开方等等。代码如下:


#include <math.h>

#include <windows.h>

#include <commctrl.h>

#pragma comment(lib,"Comctl32.lib")

LRESULT CALLBACK WndProc(HWND,HUINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow) {

  static TCHAR szAppName[] = TEXT("WINCALC");HWND hwnd;MSG msg;

  INITCOMMONCONTROLSEX icex;icex.dwSize = sizeof(icex);icex.dwICC = ICC_WIN95_CLASSES|ICC_DATE_CLASSES;InitCommonControlsEx(&icex);

  WNDCLASS wndclass;wndclass.style = CS_HREDRAW|CS_VREDRAW;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 = szAppName;

  if (!RegisterClass(&wndclass)) {MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);return 0;}

  hwnd = CreateWindow(szAppName,TEXT("Calculator"),WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN,0,0,0,0,NULL,NULL,hInstance,NULL);

  ShowWindow(hwnd,iCmdShow); 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) {

  static HWND hwndOutput;TCHAR szBuffer[16];static double dbNumber = 0.0;static char szOperand = '=';BOOL fFirstDigit = TRUE,fDecimalPoint = FALSE;

  HDC hdc;LONG lStyle;int id; LONG lValue; HWND hUpDownctrl;double temp;

  switch (message) {

  case WM_CREATE: hwndOutput = CreateWindow(TEXT("edit"),NULL,WS_CHILD|WS_VISIBLE|ES_RIGHT|ES_READONLY|ES_AUTOHSCROLL|ES_NUMBER,0,0,0,0,hwnd,(HMENU)1,NULL,NULL);

  temp = log10(30)+0.5; lStyle = GetWindowLong(hwndOutput,GWL_STYLE); SetWindowLong(hwndOutput,GWL_STYLE,(lStyle&~ES_NUMBER)|ES_LEFT);SendMessage(hwndOutput,EM_LIMITTEXT,15,0);

  hUpDownctrl=CreateWindow(UPDOWN_CLASS, "", WS_CHILD | WS_VISIBLE | UDS_SETBUDDYINT | UDS_ALIGNRIGHT | UDS_NOTHOUSANDS | UDS_ARROWKEYS |

    UDS_AUTOBUDDY, 0, 0, 0, 0, hwnd, (HMENU)2, NULL, NULL);

  SendMessage(hUpDownctrl, UDM_SETBUDDY, (WPARAM)hwndOutput, 0);

  SendMessage(hUpDownctrl, UDM_SETRANGE, 0, MAKELPARAM(100, -100));

  SendMessage(hUpDownctrl, UDM_SETPOS, 0, MAKELPARAM((int)(dbNumber), 0));

  hwndOutput = CreateWindowEx(WS_EX_ACCEPTFILES,TEXT("edit"),NULL,WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOHSCROLL|ES_AUTOVSCROLL|

    WS_VSCROLL|ES_LEFT,0,0,0,0,hwnd,(HMENU)1,NULL,NULL);

  SendMessage(hwndOutput,EM_SETREADONLY,TRUE,0);return 0;

  case WM_SETFOCUS: SetFocus(hwndOutput);return 0;

  case WM_SIZE:  MoveWindow(hwndOutput,10,10,LOWORD(lParam)-20,48,TRUE); MoveWindow(hUpDownctrl,LOWORD(lParam)-28,10,16,48,TRUE);

    MoveWindow(hwndOutput,10,10,LOWORD(lParam)-20,48,TRUE); MoveWindow(hUpDownctrl,LOWORD(lParam)-28,10,16,48,TRUE);

    return 0;

  case WM_COMMAND: id = LOWORD(wParam);

      if (id >= IDC_0 && id <= IDC_9) { if (fFirstDigit) {SetWindowText(hwndOutput,szBuffer);dbNumber = 0.0;}} else {GetWindowText(hwndOutput,szBuffer,16);dbNumber = atof(szBuffer);}

      if (id >= IDC_0 && id <= IDC_DECIMAL) { if (fFirstDigit) {SetWindowText(hwndOutput,szBuffer);szBuffer[0] = 0;fFirstDigit = FALSE;} if (id != IDC_DECIMAL || !fDecimalPoint) {szBuffer[lstrlen(szBuffer)] = (TCHAR)(id-IDC_0+(int)'0'); szBuffer[lstrlen(szBuffer)] = 0;

        SetWindowText(hwndOutput,szBuffer); if (id == IDC_DECIMAL) {fDecimalPoint = TRUE;}}} else {fFirstDigit = TRUE;fDecimalPoint = FALSE; switch (szOperand) {

          case '+': dbNumber += atof(szBuffer); break;

          case '-': dbNumber -= atof(szBuffer); break;

          case '*': dbNumber *= atof(szBuffer); break;

          case '/': dbNumber /= atof(szBuffer); break;

          case '%': dbNumber = fmod(dbNumber,atof(szBuffer)); break;

          case '^': dbNumber=pow(dbNumber,atof(szBuffer)); break;

          case 'V': dbNumber=sqrt(dbNumber); break;

          case '=': dbNumber = atof(szBuffer); break;}

        szBuffer[0] = 0;SetWindowText(hwndOutput,_gcvt(dbNumber,10,szBuffer));szOperand = (CHAR)(id-IDC_PLUS+'=');

      } return 0;

  case WM_DESTROY: PostQuitMessage(0); return 0;

  default: return DefWindowProc(hwnd,message,wParam,lParam);

  } return 0;

}

3. 类似于Windows资源管理器的文件浏览器:

文件浏览器是操作系统中常用的一个工具,可以帮助用户管理文件并方便的进行操作。本程序使用VC++实现了一个类似于Windows资源管理器的文件浏览器,可以进行文件列表浏览、删除、复制、重命名等常见操作,其中使用了Windows API中的一些常用函数和控件。代码如下:


#include <windows.h>

#include <tchar.h>

#include <shlobj.h>

#include <shellapi.h>

#pragma comment(lib, "Shell32.lib")

HINSTANCE g_hinst;

#define ID_LISTBOX1 200

#define ID_LISTBOX2 201

#define ID_FILE_EXIT 400

  

TCHAR szDir[MAX_PATH], szFilter[] = TEXT("All Files(*.*)\0*.*\0\0");

OPENFILENAME ofn;

HWND g_hwndLB1 = NULL, g_hwndLB2 = NULL;

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

{

  static HWND hwndButton;

  static TCHAR szText[] = TEXT("View");

  HDC hdc;

  PAINTSTRUCT ps;

  RECT rect;

  switch (uMsg)

  {

  case WM_CREATE:

    g_hwndLB1 = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("listbox"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | LBS_NOTIFY | WS_VSCROLL, 20, 20, 200, 200, hwnd, (HMENU)ID_LISTBOX1, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);

    g_hwndLB2 = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("listbox"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | LBS_NOTIFY | WS_VSCROLL, 230, 20, 200, 200, hwnd, (HMENU)ID_LISTBOX2, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);

    hwndButton=CreateWindow(TEXT("button"), szText,WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,440,150,60,20,hwnd,(HMENU)1,(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);

    return 0;

  case WM_SIZE:

    MoveWindow(g_hwndLB1, 20, 20, LOWORD(lParam) / 2 - 25, HIWORD(lParam) - 40, TRUE);

    MoveWindow(g_hwndLB2, LOWORD(lParam) / 2 + 5, 20, LOWORD(lParam) / 2 - 25, HIWORD(lParam) - 40, TRUE);

    MoveWindow(hwndButton, LOWORD(lParam) / 2 + 25, 150, 80, 25, TRUE);

    return 0;

  case WM_PAINT:

    hdc = BeginPaint(hwnd, &ps);

    GetClientRect(hwnd, &rect);

    DrawText(hdc, TEXT(" Left Files"), -1, &rect, DT_CENTER | DT_SINGLELINE | DT_VCENTER);

    rect.left = LOWORD(rect.right) / 2 + 5;

    rect.right = rect.right - 15;

    DrawText(hdc, TEXT(" Right Files"), -1, &rect, DT_CENTER | DT_SINGLELINE | DT_VCENTER);

    EndPaint(hwnd, &ps);

    return 0;

  case WM_COMMAND:

    switch (LOWORD(wParam))

    {

    case 1:

      int lbItem = SendMessage(g_hwndLB1, LB_GETCURSEL, 0, 0);

      int len = SendMessage(g_hwndLB1, LB_GETTEXTLEN, lbItem, 0);

      TCHAR* name = new TCHAR[len + 1];

      SendMessage(g_hwndLB1, LB_GETTEXT, lbItem, (LPARAM)name);

      SendMessage(g_hwndLB2, LB_ADDSTRING, 0, (LPARAM)name);

      return 0;

    default:

      return DefWindowProc(hwnd, uMsg, wParam, lParam);

    }

  case WM_DESTROY:

    PostQuitMessage(0);

    return 0;

  }

  return DefWindowProc(hwnd, uMsg, wParam, lParam);

}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)

{

  static TCHAR szAppName[] = TEXT("DirDialogApp");

  WNDCLASS wndclass;

  setlocale(LC_ALL, "chs");

  wndclass.style = CS_HREDRAW | CS_VREDRAW;

  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 = szAppName;

  if (!RegisterClass(&wndclass))

  {

    MessageBox(NULL, TEXT("程序需要Windows 98或以上版本的操作系统支持"), szAppName, MB_ICONERROR);

    return 0;

  }

  HWND hwnd = CreateWindow(szAppName, TEXT("文件浏览器"), WS_OVERLAPPEDWINDOW,

    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

    NULL, NULL, hInstance, NULL);

  ShowWindow(hwnd, iCmdShow);

  UpdateWindow(hwnd);

  MSG msg;

  HACCEL hAccelTable = LoadAccelerators(hInstance, szAppName);

  while (GetMessage(&msg, NULL, 0, 0))

  {

    if (!TranslateAccelerator(hwnd, hAccelTable, &msg))

    {

      TranslateMessage(&msg);

      DispatchMessage(&msg);

    }

  }

  return msg.wParam;

}

以上代码示例展示了VC++的无穷魅力,在编程实现功能的同时,更能体现出程序员的编码能力和技巧。本文介绍的VC++代码示例只是冰山一角,还有很多很多不同的应用和实现方式,让我们继续学习和研究,玩转VC++!

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复