C++类的基本概念演示Win32版
生活随笔
收集整理的這篇文章主要介紹了
C++类的基本概念演示Win32版
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
演示C++基本的類的概念,使用Win32;做一個簡單的類;
#include <windows.h> #include "resource.h"LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);HINSTANCE hInst; TCHAR szClassName[] = TEXT("classDemo"); char szBuffer[100];//通過class關鍵字類定義類 class Student{public://類包含的變量char *name;int age;int score;HDC hdc;//類包含的函數void say(){//printf("%s的年齡是 %d,成績是 %d\n", name, age, score);wsprintf(szBuffer, "%s的年齡是 %d,成績是 %d",name, age, score);TextOut(hdc,10,50,szBuffer,lstrlen(szBuffer));} };int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nFunsterStil) {HWND hwnd;MSG messages;WNDCLASSEX wincl;hInst = hThisInstance;wincl.hInstance = hThisInstance;wincl.lpszClassName = szClassName;wincl.lpfnWndProc = WindowProcedure;wincl.style = CS_DBLCLKS;wincl.cbSize = sizeof (WNDCLASSEX);wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);wincl.hCursor = LoadCursor (NULL, IDC_ARROW);wincl.lpszMenuName = MAKEINTRESOURCE (IDC_CLASSDEMO);wincl.cbClsExtra = 0;wincl.cbWndExtra = 0;wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);if (!RegisterClassEx (&wincl))return 0;hwnd = CreateWindowEx (0,szClassName,TEXT("classDemo"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,300,200,HWND_DESKTOP,NULL,hThisInstance,NULL);ShowWindow (hwnd, nFunsterStil);while (GetMessage (&messages, NULL, 0, 0)){TranslateMessage(&messages);DispatchMessage(&messages);}return messages.wParam; }LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {PAINTSTRUCT ps;HDC hdc;RECT rt; switch (message){case WM_COMMAND:switch (LOWORD(wParam)){case IDM_class: hdc=GetDC(hwnd);//通過類來定義變量,即創建對象class Student stu1; //也可以省略關鍵字class//為類的成員變量賦值stu1.name = "小明";stu1.age = 15;stu1.score = 9999;stu1.hdc=hdc;//調用類的成員函數stu1.say(); break;case IDM_ABOUT:MessageBox (hwnd, TEXT ("classDemo v1.0\nCopyright (C) 2020\n by bo"),TEXT ("classDemo"), MB_OK | MB_ICONINFORMATION);break;case IDM_EXIT:DestroyWindow(hwnd);break;default:return DefWindowProc(hwnd, message, wParam, lParam); }break;case WM_CREATE:break;case WM_PAINT:hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rt); EndPaint(hwnd, &ps);break;case WM_DESTROY:PostQuitMessage (0);break;default:return DefWindowProc (hwnd, message, wParam, lParam);}return 0; }和控制臺有一點不同;需要把 HDC hdc 作為類的成員,才能輸出;
運行如下;
工程;
資源和頭文件;
#include "resource.h" #include <windows.h>/ // // Menu //IDC_CLASSDEMO MENU BEGINPOPUP "&File"BEGINMENUITEM "class Demo", IDM_classMENUITEM "E&xit", IDM_EXITENDPOPUP "&Help"BEGINMENUITEM "&About ...", IDM_ABOUTEND END #define IDM_EXIT 10001 #define IDM_ABOUT 10002#define IDC_CLASSDEMO 10101 #define IDD_ABOUTBOX 10102 #define IDM_class 40001?
總結
以上是生活随笔為你收集整理的C++类的基本概念演示Win32版的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SuperMap iDesktop 8C
- 下一篇: s3c2440移植MQTT