windows programming -- learning note 2

Keywords: Windows

SysMets.h

The structure of SysMets.h in the book is

struct
{
	int     iIndex;
	TCHAR * szLabel;
	TCHAR * szDesc;
};

**However, errors are reported during compilation. All TEXT macros in SysMets.h report errors. The errors are
Severity code description project file line suppress status
Error (activity) the value of type E0144 "const wchar ﹐ t *" cannot be used to initialize the entity of type "TCHAR *". WindowsProject1 C:\Users\Dell\source\repos\WindowsProject1\WindowsProject1\SysMets.h 38 * * after a while, I feel that the error is reported due to the unequal type, and then try to add const keyword, and then ok

struct
{
	int     iIndex;
	const TCHAR * szLabel;
	const TCHAR * szDesc;
};

There is also the definition method of structure in SysMets.h, which makes my eyes bright. I haven't defined it like this. I don't even specify the direct definition of structure name. When defining NUMLINES macro, sizeof didn't add parentheses, sizeof sysmetrics directly. I wrote it myself with an unbelievable attitude, which seems to make my C linguistics too shallow

Experimental procedure:

#include<iostream>
#include<cstdio>
using namespace std;
struct
{
    int a;
    char *p;
}
x[]
={1,"hello",2,"world"};

int main(){
    cout << sizeof x << endl;  // All bytes occupied by structure data
    cout << sizeof x / sizeof x[0] << endl;  Number of elements in structure
    cout << x[0].a << " " << x[1].p << endl;
    cout << x[1].a << " " << x[1].p << endl;
    return 0;
}
/*
16
2
1 world
2 world
*/

Here is the main program:

#include<Windows.h>
#include"SysMets.h"
LRESULT CALLBACK WndProc(HWND hInstance, UINT message,
								WPARAM wparam, LPARAM 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.cbClsExtra = NULL;
	wndclass.cbWndExtra = NULL;
	wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hInstance = hInstance;
	wndclass.lpszMenuName = NULL;
	wndclass.lpszClassName = szAppName;
	wndclass.lpfnWndProc = WndProc;
	if (!RegisterClass(&wndclass)) {
		MessageBox(NULL, TEXT("Yes Bug Ah!!!"), 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 int cxChar, cxCaps, cyChar;
	int i;
	HDC hdc;
	PAINTSTRUCT ps;
	RECT rect;
	TEXTMETRIC tm;
	TCHAR szBuffer[10];
	switch (message) {
	case WM_CREATE:
		hdc = GetDC(hwnd);  // Get environment handle
		GetTextMetrics(hdc, &tm);
		cxChar = tm.tmAveCharWidth;  // Average width of lowercase characters
		cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;  Average width of uppercase characters
		cyChar = tm.tmExternalLeading + tm.tmHeight;  // Height of each character
		ReleaseDC(hwnd, hdc);  // Release handle
		return 0;
	case WM_PAINT: 
		hdc = BeginPaint(hwnd, &ps);  // Get device environment handle
		for (i = 0; i < NUMLINES; i++) {  
			TextOut(hdc, 0, cyChar * i, sysmetrics[i].szLabel
				, lstrlen(sysmetrics[i].szLabel));

			TextOut(hdc, 22 * cxCaps, cyChar * i, sysmetrics[i].szDesc,
				lstrlen(sysmetrics[i].szDesc));

			SetTextAlign(hdc, TA_RIGHT | TA_TOP);

			TextOut(hdc, 22 * cxCaps + 40 * cxChar, cyChar * i, szBuffer,
				wsprintf(szBuffer, TEXT("%5d"), GetSystemMetrics(sysmetrics[i].iIndex)));

			SetTextAlign(hdc, TA_LEFT | TA_TOP);
		}
		EndPaint(hwnd, &ps);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd, message, wParam, lparam);
}

Posted by WLW on Wed, 20 Nov 2019 10:02:40 -0800