控制台和Win32 API程序输出变量地址值

控制台程序;

#include <stdio.h>
int main(){
    int a = 100;
    char str[20] = "www.daye.com";
    printf("%#X, %#X\n", &a, str);
    return 0;
}

Win32 程序;

/*-------------------------------------------------
bobo, 2020
-------------------------------------------------*/

#include <windows.h>
#include <windowsx.h>

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

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	PSTR  szCmdLine, int iCmdShow)
{
	static TCHAR szAppName[] = TEXT("pointerDemo");
	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("Program requires Windows NT!"),
			szAppName, MB_ICONERROR);
		return 0;
	}

	hwnd = CreateWindow(szAppName, TEXT("pointerDemo"),
		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)
{
	HDC         hdc;
	PAINTSTRUCT ps ;
	char buffer[65];
	int a = 100;
	char str[20] = "www.daye.com";
	
	switch (message)
	{
	case WM_CREATE:
		return 0;

	case WM_SIZE:
		return 0;

	case WM_RBUTTONDOWN:
		return 0;

	case WM_LBUTTONDOWN:
		hdc = GetDC(hwnd);		

		wsprintf(buffer,"%#X",&a);
		TextOut(hdc, 100, 20, buffer, 8);
		wsprintf(buffer,"%#X",str);
		TextOut(hdc, 200, 20, buffer, 8);
		return 0;

	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);

		EndPaint(hwnd, &ps);
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}

运行;

wsprintf(buffer,"%#X",&a);

TextOut(hdc, 100, 20, buffer, 8);

把a的地址按十六进制格式化到buffer;在坐标100、20,输出buffer的值,输出长度8;

控制台程序使用printf输出,可以自带格式化;

Win32 使用wsprintf格式化,使用TextOut输出;

发布了434 篇原创文章 · 获赞 512 · 访问量 294万+

猜你喜欢

转载自blog.csdn.net/bcbobo21cn/article/details/103896671