Windows程序设计实验---Windows API Programming 1

实验描述:
1)To create window and message loop and write win procedure by calling API functions.

2)To draw and realize text output in the client area by calling API functions.

You should get a result the same as follows :
在这里插入图片描述
源代码如下:

#include <windows.h>

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

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nCmdShow)
{
    
        
	static TCHAR szAppName[] = TEXT("Experiment1");    
	HWND hwnd;    
	WNDCLASS wndclass;    
	MSG msg;        
	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("Experiment1"),
	                    WS_OVERLAPPEDWINDOW,
	                    CW_USEDEFAULT, 
	                    CW_USEDEFAULT,                         
	                    CW_USEDEFAULT, 
	                    CW_USEDEFAULT,                        
	                    NULL, 
	                    NULL, 
	                    hInstance, 
	                    NULL
	                    );    
	ShowWindow(hwnd, nCmdShow);    
	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;    
	int i;    
	HBRUSH hbrush;    
	HPEN hpen;    
	static char *Text[] = {
    
    
				"唯     孤     烟     故",
				"见     帆     花     人",                             
				"长     远     三     西",                             
				"江     影     月     辞",                              
				"天     碧     下     黄",                              
				"迹     空     扬     鹤",                             
				"流     尽     州     楼"                             
				};    
	POINT apt1[4] = {
    
    100,100,200,100,150,187,100,100};    
	POINT apt2[6] = {
    
    150,287,200,300,200,350,100,350,100,300,150,287};                

	switch(message)    
	{
    
            
	case WM_PAINT:            
		hdc = BeginPaint(hwnd,&ps);            
		for( i = 0; i < 7; i++)                
			TextOut(
				hdc, 750, 110 + 30 * i,
				Text[i], lstrlen(Text[i])                          
				);                        
		
		hpen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));            
		SelectObject(hdc, hpen);                        
		Polyline(hdc, apt1, 4);            
		Ellipse(hdc, 100, 187, 200, 287);            
		Polyline(hdc, apt2, 6);                        
		
		hbrush = CreateSolidBrush(RGB(0, 0, 0));            
		SelectObject(hdc, hbrush);            
		RoundRect(hdc, 300, 150, 400, 187, 10, 10);            
		Pie(hdc, 300, 187, 400, 287, 340, 190, 360, 190);            
		Rectangle(hdc, 300, 287, 400, 350);                        
		
		EndPaint(hwnd, &ps);            
		return 0;                    
	case WM_DESTROY :            
		PostQuitMessage(0);            
		return 0;    
	}    
	return DefWindowProc(hwnd, message, wParam, lParam);
}

结果如下:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43574277/article/details/105302857