一個經典的WIN32窗口應用

源自圖書《精通Windows程序設計–基於VisualC++實現》

這裏使用**visual studio2019 **

//1. 新建一個cmd控制檯應用程序
//2.將項目->屬性->連接器->系統->子系統(窗口)
![在這裏插入圖片描述](https://img-blog.csdnimg.cn/20200205211619522.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzIwMTQ0NTk3,size_16,color_FFFFFF,t_70)
#ifndef __cmd_win32__
#define __cmd_win32__
#include <Windows.h>
#include <iostream>
#include "cmd_win32.h"
using namespace std;
#define MAX_LOADSTRING 100  //廢棄的
static TCHAR szWindowClass[] = TEXT("黑暗過後的黎明");
static TCHAR szTitle[] = TEXT("黎明來了");
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	MSG msg;
	HACCEL hAccelTable = LoadAccelerators(hInstance, szTitle);
	RegisterClass(hInstance); //註冊窗口
	if (!InitInstance(hInstance, nCmdShow))return FALSE;
	while (GetMessage(&msg, NULL, 0, 0)) //進入消息循環
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}return msg.wParam;
	while (::GetMessage(&msg, NULL, 0, 0))
	{
		::TranslateMessage(&msg);
		::DispatchMessage(&msg);
	}
}
ATOM MyRegisterClass( HINSTANCE hInstance) {
	WNDCLASS wndclass;
	//wndclass.cbSize = sizeof(wndclass);
	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc = (WNDPROC)WndProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hInstance = hInstance;
	wndclass.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_EXCLAMATION);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	//wndclass.lpszMenuName = (LPCTSTR)IDC_CHAP2EXA2;
	wndclass.lpszClassName = szWindowClass;
	//wndclass.hIconSm = LoadIconW(wndclass.hInstance, (LPCTSTR)IDI_WARNING);
	return MyRegisterClass(&wndclass);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;
	hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
	if (!hWnd)
	{
		return FALSE;
	}
	//顯示
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
	return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wPARAM, LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT ps;
	RECT rect;
	switch (message)
	{
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		GetClientRect(hWnd, &rect);
		DrawText(hdc, TEXT("大家好,這是我的第一個窗口程序!"), -1, &rect,
			DT_SINGLELINE | DT_CENTER | DT_VCENTER);
		EndPaint(hWnd, &ps);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hWnd, message, wPARAM, lParam);
}

#endif  !__cmd_win32__
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章