windows程序设计基础知识

1、windows.h头文件

windows.h是主要的包含文件,包含其他windows头文件,其中包含最重要和最基本的是:
windef.h——基本类型定义
winnt.h——支持unicode的类型定义
winbase.h——内核函数
winuser.h——用户接口函数
wingdi.h——图形设备接口函数
这些头文件定义了windows的所有数据类型、函数调用、数据结构和常数标识符,是windows文档中的一个重要部分。

2、程序入口点

我们在vs2008中创建一个win32 project的windows工程,如下:

// testWindows.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "testWindows.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_TESTWINDOWS, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTWINDOWS));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TESTWINDOWS));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_TESTWINDOWS);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   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;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}

入口点:

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)

Ctrl+Gtiao跳转到tchar.h,定义如下

#define _tmain      main
#define _tWinMain   WinMain
#ifdef  _POSIX_
#define _tenviron   environ
#else
#define _tenviron  _environ
#endif
#define __targv     __argv

Ctrl+G查看WinMain的定义,在winbase.h如下:

int
#if !defined(_MAC)
#if defined(_M_CEE_PURE)
__clrcall
#else
WINAPI
#endif
#else
CALLBACK
#endif
WinMain (
    __in HINSTANCE hInstance,
    __in_opt HINSTANCE hPrevInstance,
    __in_opt LPSTR lpCmdLine,
    __in int nShowCmd
    );

int
#if defined(_M_CEE_PURE)
__clrcall
#else
WINAPI
#endif
wWinMain(
    __in HINSTANCE hInstance,
    __in_opt HINSTANCE hPrevInstance,
    __in_opt LPWSTR lpCmdLine,
    __in int nShowCmd
    );

LPWSTR :其中的"LP"表示长指针(Long pointer)
匈牙利表示法:在变量名称前增加表示变量数据类型的端前缀,如nShowCmd、hPrevInstance等。
在windef.h文件中有如下:

#define CALLBACK    __stdcall
#define WINAPI      __stdcall
#define WINAPIV     __cdecl
#define APIENTRY    WINAPI
#define APIPRIVATE  __stdcall
#define PASCAL      __stdcall

#define WINAPI __stdcall指定了一个调用约定,包括如何生成机器代码以在堆栈中防止函数调用的参数。下面来看看WinMain的参数。
hInstance:实例句柄。仅是一个应用程序用来识别某些事件的标志,唯一地标识一个程序,其他的函数需要使用该句柄作为参数调用该程序。同一应用程序的所有实例共享代码和只读的内存
hPrevInstance:可用来确定自身的其他实例是否正在运行。可将某些数据从前面的实例移到自己的数据区域,目前该参数总是NULL
lpCmdLine:运行程序的命令行
nShowCmd:程序最初显示的方式,可以是正常或者最大化整个窗口、或最小化显示在任务列表栏中。

3、MessageBox

该函数主要是用于显示消息提示,可认为是一个对话框。

4、编译、链接和运行

正常情况下,在编译阶段,程序没有错误,编译器从源代码文件生成一个.obj文件。
在链接阶段,程序没有错误,链接程序结合.obj文件和.lib文件建立.exe文件。在windows下的程序,都会使用windows库等一些库里面的函数,因此必须链接一些lib文件。
经过编译和链接,我们得到了一个exe可执行文件,这个时候我们可以双击直接运行该exe。

参考<Windows程序设计第五版>

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#是一种现代化的面向对象程序设计语言,它是由Microsoft于2000年推出的。C#语言最初是为了开发Windows应用程序设计的,但现在已经可以用于开发各种类型的应用程序,包括Web和移动应用程序等。下面是一些C# Windows程序设计基础知识: 1. 界面设计:在C#中,可以使用Windows窗体应用程序来创建图形界面。窗体应用程序包含一个主窗体和各种控件,如按钮、文本框、标签等,它们可以通过拖拽和放置的方式来创建和布局。您可以使用Visual Studio等集成开发环境来创建和编辑窗体应用程序。 2. 事件处理:当用户与应用程序交互时,不同的控件会触发不同的事件。在C#中,可以通过编写事件处理程序来响应这些事件。事件处理程序可以是一个方法,当事件发生时,该方法会被自动调用。您可以在Visual Studio中使用设计器来创建事件处理程序。 3. 数据库访问:在Windows应用程序中,通常需要访问数据库来存储和检索数据。C#提供了一种名为ADO.NET的技术,可以用于连接和操作各种类型的数据库,如SQL Server、MySQL等。您可以使用ADO.NET提供的类和方法来执行数据库操作,如查询、插入、更新和删除数据等。 4. 多线程编程:在Windows应用程序中,为了避免阻塞用户界面,通常需要使用多线程编程技术。C#提供了一种名为Task Parallel Library的技术,可以用于创建和管理线程。您可以使用Task Parallel Library提供的类和方法来实现多线程编程,如创建和启动线程、线程间通信等。 5. 错误处理:在Windows应用程序中,可能会出现各种类型的错误,如用户输入错误、数据库连接错误等。为了提高程序的健壮性,需要对这些错误进行处理。C#提供了一种名为异常处理的技术,可以用于捕获和处理各种类型的异常。您可以使用try-catch语句来实现异常处理,如捕获异常、记录日志、显示错误消息等。 希望这些基础知识能够帮助您更好地理解C# Windows程序设计

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值