C++类的基本概念演示Win32版

演示C++基本的类的概念,使用Win32;做一个简单的类;

#include 
#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 作为类的成员,才能输出;
运行如下;

C++类的基本概念演示Win32版_第1张图片

C++类的基本概念演示Win32版_第2张图片

工程;

C++类的基本概念演示Win32版_第3张图片

C++类的基本概念演示Win32版_第4张图片

资源和头文件;

#include "resource.h"
#include 

/////////////////////////////////////////////////////////////////////////////
//
// Menu
//

IDC_CLASSDEMO MENU 
BEGIN
    POPUP "&File"
    BEGIN
    	MENUITEM "class Demo",                	IDM_class
        MENUITEM "E&xit",                	IDM_EXIT
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&About ...",           	IDM_ABOUT
    END
END
#define 	IDM_EXIT		10001
#define 	IDM_ABOUT		10002

#define 	IDC_CLASSDEMO		10101
#define 	IDD_ABOUTBOX	10102
#define     IDM_class     40001

 

你可能感兴趣的:(VC++)