windows 1 1 Windows Windows 95 Windows 98

  • Slides: 26
Download presentation

第一章 windows程序设计基础知识 • 1. 1 必备知识 • Windows的操作系统: Windows 95 Windows 98 Windows Me

第一章 windows程序设计基础知识 • 1. 1 必备知识 • Windows的操作系统: Windows 95 Windows 98 Windows Me Windows 2000 Windows XP Windows 2003 (32) Vista Win 7(64) • 开发 具VB、 Visual Fox. Pro 、VC++、Delphi、C# 、 VC++. NET。 TC是 16位DOS程序编译器

大多数API位于三个核心DLL中: kernel 32. dll 、 user 32. dll、GDI 32. dll 采用其开发windows应用程序过于繁琐。 • API&MFC

大多数API位于三个核心DLL中: kernel 32. dll 、 user 32. dll、GDI 32. dll 采用其开发windows应用程序过于繁琐。 • API&MFC

 • API函数的调用方法 (2)连接到指定的库文件 kernel 32. lib user 32. lib gdi 32. lib winspool.

• API函数的调用方法 (2)连接到指定的库文件 kernel 32. lib user 32. lib gdi 32. lib winspool. lib comdlg 32. lib advapi 32. lib shell 32. lib oleaut 32. lib uuid. lib odbc 32. lib odbccp 32. lib 显式设置:#pragma comment(lib, ” WS 2_32. lib ”) (3)在API函数前加“: : ”表示这是一个全局函数,以 与 C++类的成员函数区分。

调用API例子(C++) #include <windows. h> 就可以使用API函数

调用API例子(C++) #include <windows. h> 就可以使用API函数

n n n #include "stdafx. h" // 这是VC自动添加的头文件, 没有什么用途 #include <windows. h> // 包含Message.

n n n #include "stdafx. h" // 这是VC自动添加的头文件, 没有什么用途 #include <windows. h> // 包含Message. Box 函数声明的头文件 int main(int argc, char* argv[]) { // 调用API函数Message. Box int n. Select = : : Message. Box(NULL, "Hello, Windows XP", "Greetings", MB_OKCANCEL); if(n. Select == IDOK) printf(" 用户选择了“确定”按钮 n"); else printf(" 用户选择了“取消”按钮 n"); return 0; }

n n 2. C#调用格式 using System. Runtime. Interop. Services; //引用此名称空间,简化后面的代码. . . //使用Dll. Import.

n n 2. C#调用格式 using System. Runtime. Interop. Services; //引用此名称空间,简化后面的代码. . . //使用Dll. Import. Attribute特性来引入api函数, 注意声明的是空方法,即方法体为空。 [Dll. Import("user 32. dll")] public static extern Return. Type Function. Name(type arg 1, type arg 2, . . . ); //调用时与调用其他方法并无区别

调用API例子(VB) 1. VB不能直接调用Win API,要先声明然后再调用。 2. 声明 API 过程 ,比如声名Set. Window. Text. A 首先,在模块的声明部分对过程进行声明: Private

调用API例子(VB) 1. VB不能直接调用Win API,要先声明然后再调用。 2. 声明 API 过程 ,比如声名Set. Window. Text. A 首先,在模块的声明部分对过程进行声明: Private Declare Function Set. Window. Text Lib "user 32" Alias "Set. Window. Text. A" (By. Val hwnd As Long, By. Val lp. String As String) As Long 3. 调用 API 过程 在声明了函数之后,调用它的方式与标准的 Visual Basic 函数相 同。在下例中,Form Load 事件调用了上面声明的过程。 Private Sub Form_Load() Set. Window. Text Form 1. h. Wnd, "Welcome to VB" End Sub 在VB环境下,涉及到的所有integer(整型数),都是 16位

调用API例子(C#) 读写INI文件。 using System; using System. IO; using System. Text; using System. Runtime. Interop.

调用API例子(C#) 读写INI文件。 using System; using System. IO; using System. Text; using System. Runtime. Interop. Services; namespace File. IO { public class INIFile. IO { [Dll. Import("kernel 32")] private static extern long Write. Private. Profile. String(string section, string key, string val, string file. Path); [Dll. Import("kernel 32")] private static extern int Get. Private. Profile. String(string section, string key, string def, String. Builder ret. Val, int size, string file. Path);

调用API例子(C#) public INIFile. IO() { } public static void INIWrite. Value(string section, string key,

调用API例子(C#) public INIFile. IO() { } public static void INIWrite. Value(string section, string key, string val, string file. Path) { Write. Private. Profile. String(section, key, val, file. Path); } public static string INIRead. Value(string section, string key, string file. Path) { String. Builder temp = new String. Builder(255); int i = Get. Private. Profile. String(section, key, "", temp, 255, file. Path); return temp. To. String(); } }

某些API函数要求以callback作为你参数之一。 如Set. Timer,Line. DDA,Enum. Objects。 n n n UINT_PTR Set. Timer( HWND h. Wnd,

某些API函数要求以callback作为你参数之一。 如Set. Timer,Line. DDA,Enum. Objects。 n n n UINT_PTR Set. Timer( HWND h. Wnd, // 窗口句柄 UINT_PTR n. IDEvent, // 定时器ID,多个定时 器时,可以通过该ID判断是哪个定时器 n UINT u. Elapse, // 时间间隔, 单位为毫秒 n TIMERPROC lp. Timer. Func // 回调函数 n );

n int __stdcall Compare. Ints(const byte* velem 1, const byt e* velem 2) n

n int __stdcall Compare. Ints(const byte* velem 1, const byt e* velem 2) n { n int elem 1 = *(int*)velem 1; n int elem 2 = *(int*)velem 2; n if(elem 1 < elem 2) n return -1; n if(elem 1 > elem 2) n return 1; n return 0; n }

n int __stdcall Compare. Strings(const byte * velem 1, const byte* velem 2) n

n int __stdcall Compare. Strings(const byte * velem 1, const byte* velem 2) n { n const char* elem 1 = (char*)velem 1; n const char* elem 2 = (char*)velem 2; n return strcmp(elem 1, elem 2); n }

n int main(int argc, char* argv[]) n { n int i; n int array[]

n int main(int argc, char* argv[]) n { n int i; n int array[] = {5432, 4321, 3210, 2109, 1098}; n cout << "Before sorting ints with Bubblesortn"; n for(i=0; i < 5; i++) n cout << array[i] << ’n’; n Bubblesort((byte*)array, 5, sizeof(array[0]), &Compare. Ints); n cout << "After the sortingn"; n for(i=0; i < 5; i++) n cout << array[i] << ’n’;

n const char str[5][10] = {"estella", "danielle", "crissy", "bo", "angi e"}; n cout <<

n const char str[5][10] = {"estella", "danielle", "crissy", "bo", "angi e"}; n cout << "Before sorting strings with Quicksortn"; n for(i=0; i < 5; i++) n cout << str[i] << ’n’; n Quicksort((byte*)str, 5, 10, &Compare. Strings); n cout << "After the sortingn"; n for(i=0; i < 5; i++) n cout << str[i] << ’n’; n return 0; n }