1 / 27

( 第四十讲 )

( 第四十讲 ). 吉林大学远程教育课件. Windows A P I 编 程. 学 时: 48. 主讲人 : 翟慧杰. 例 1 : #include <windows.h> #define MAX_STRINGS 256// 最大字符串数 typedef struct { HDC hdc; int xText; int yText; int xStart; int yStart; int xIncr; int yIncr;

rufus
Download Presentation

( 第四十讲 )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. (第四十讲) 吉林大学远程教育课件 Windows A P I编 程 学 时:48 主讲人 : 翟慧杰

  2. 例1: #include <windows.h> #define MAX_STRINGS 256//最大字符串数 typedef struct { HDC hdc; int xText; int yText; int xStart; int yStart; int xIncr; int yIncr; int xMax; int yMax; }CBPARAM;

  3. PSTR pszStrings[MAX_STRINGS]; int iTotal = 0;//当前字符串数目 BOOL CALLBACK GetStr (PSTR pString, CBPARAM *pcbp); //函数:DllMain //作用:DLL入口 int WINAPI DllMain ( HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved) { return TRUE; }

  4. //函数:EnterString //作用:输入字符串 int WINAPI EnterString (PSTR pStringIn) { HANDLE hString; PSTR pString; int i, iLength, iCompare; if (iTotal == MAX_STRINGS - 1)//达到最大字符串数目 return FALSE; iLength = strlen (pStringIn);//获取字符串长度 if (iLength == 0)//空字符串 return FALSE; //创建映像文件 hString=CreateFileMapping((HANDLE)-1,NULL, PAGE_READWRITE, 0, 1 + iLength, NULL); if (hString == NULL) return FALSE;

  5. pString = (PSTR) MapViewOfFile (hString, FILE_MAP_WRITE, 0, 0, 0); strcpy (pString, pStringIn); AnsiUpper (pString);//全部转换成大写 for (i = iTotal; i > 0; i--) { iCompare = strcmpi (pStringIn, pszStrings[i - 1]); //比较字符串 if (iCompare >= 0)//字符出不等则调处循环 break; pszStrings[i] = pszStrings[i - 1]; //字符出相等则删除字符串 } pszStrings[i] = pString; iTotal++; return TRUE; }

  6. //函数:DeleteString //作用:输入字符串 int WINAPI DeleteString(PSTR pStringIn) { int i, j, iCompare; if (0 == strlen (pStringIn))//忽略输入的空字符串 return FALSE; for (i = 0 ; i < iTotal ; i++)//字符串排序 { iCompare = lstrcmpi (pszStrings[i], pStringIn); if (iCompare == 0) break; } if (i == iTotal) return FALSE; UnmapViewOfFile (pszStrings[i]); for (j = i ; j < iTotal ; j++) pszStrings[j] = pszStrings[j + 1]; pszStrings[iTotal--] = NULL; return TRUE; }

  7. //函数:GetStrings //作用:输出所有的字符串 int WINAPI GetStrings(CBPARAM * pParam) { BOOL bReturn; int i; for (i = 0; i < iTotal; i++) { bReturn = GetStr(pszStrings[i], pParam); if (bReturn == FALSE) return i + 1; } return iTotal; }

  8. //函数:GetStrCallBack //作用:输出一个字符串 BOOL CALLBACK GetStr (PSTR pString, CBPARAM *pcbp) { TextOut (pcbp->hdc, pcbp->xText, pcbp->yText,pString, strlen (pString)); if ((pcbp->yText += pcbp->yIncr) > pcbp->yMax) { pcbp->yText = pcbp->yStart; if ((pcbp->xText += pcbp->xIncr) > pcbp->xMax) return FALSE; } return TRUE ; }

  9. #include<windows.h> #include<string.h> #include"exetest.h" #define MAXLEN 32 #define MAX_STRINGS 256 //最大字符串数 #define WM_DATACHANGE WM_USER typedef struct { HDC hdc; int xText; int yText; int xStart; int yStart; int xIncr; int yIncr; int xMax; int yMax; }CBPARAM;

  10. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); //声明动态链接库中的函数原型 int WINAPI EnterString (PSTR pStringIn); int WINAPI DeleteString (PSTR pStringIn); int WINAPI GetStrings(CBPARAM * pParam); char szAppName[] = "exetest"; char szString[MAXLEN]; //函数:WinMain //作用:程序入口 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)

  11. { HWND hwnd; MSG msg; WNDCLASSEX wndclass; wndclass.cbSize = sizeof (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 = szAppName; wndclass.lpszClassName = szAppName; wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION); RegisterClassEx (&wndclass);

  12. hwnd = CreateWindow (szAppName, "DLL Demonstration Program", 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; }

  13. //函数:DlgProc //作用:对话可小型处理 BOOL CALLBACK DlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam) { switch (iMsg) { case WM_INITDIALOG://初始化窗口 SendDlgItemMessage (hDlg, IDD_STRING,EM_LIMITTEXT,MAXLEN - 1,0); return TRUE;

  14. case WM_COMMAND://响应用户在对话框中的操作 switch (wParam) { case IDOK://用户单击确认按钮 //获取字符串 GetDlgItemText (hDlg, IDD_STRING, szString, MAXLEN); EndDialog (hDlg, TRUE);//关闭对话框 return TRUE; case IDCANCEL://用户单击关闭按钮 EndDialog (hDlg, FALSE);//关闭对话框 return TRUE; } } return FALSE ; }

  15. //函数:EnumCallBack //作用:通知主窗口中的数据是否发生变化 BOOL CALLBACK EnumCallBack (HWND hwnd, LPARAM lParam) { char szClassName[16] ; GetClassName (hwnd, szClassName, sizeof (szClassName));//获取窗口类名 if (0 == strcmp (szClassName, szAppName)) SendMessage (hwnd, WM_DATACHANGE, 0, 0) ; return TRUE ; } //函数:WndProc //作用:处理主窗口消息 LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) {

  16. static HINSTANCE hInst; static int cxChar, cyChar, cxClient, cyClient; CBPARAM cbparam; HDC hdc; PAINTSTRUCT ps; TEXTMETRIC tm; switch (iMsg) {case WM_CREATE://创建窗口消息 hInst = ((LPCREATESTRUCT) lParam)->hInstance; hdc = GetDC (hwnd); GetTextMetrics (hdc, &tm); cxChar = (int) tm.tmAveCharWidth; cyChar = (int) (tm.tmHeight + tm.tmExternalLeading);

  17. ReleaseDC (hwnd, hdc); return 0 ; case WM_COMMAND: switch (wParam) { case IDM_ENTER ://单击输入菜单项 //生成添加对话框 if (DialogBox (hInst, "EnterDlg", hwnd, &DlgProc)) { if (EnterString (szString))//调用DLL中定义的函数EnterString EnumWindows (&EnumCallBack, 0); else MessageBeep (0); } break;

  18. case IDM_DELETE://单击删除菜单项 //生成删除对话框 if (DialogBox (hInst, "DeleteDlg", hwnd, &DlgProc)) { if (DeleteString (szString)) EnumWindows (&EnumCallBack, 0); else MessageBeep (0); } break; } return 0;

  19. case WM_SIZE://改变窗口大小 cxClient = (int) LOWORD (lParam); cyClient = (int) HIWORD (lParam); return 0; case WM_DATACHANGE://工作区中的数据发生变化 InvalidateRect (hwnd, NULL, TRUE); return 0; case WM_PAINT://重绘窗口 hdc = BeginPaint (hwnd, &ps); cbparam.hdc = hdc; cbparam.xText = cbparam.xStart = cxChar; cbparam.yText = cbparam.yStart = cyChar; cbparam.xIncr = cxChar * MAXLEN; cbparam.yIncr = cyChar; cbparam.xMax = cbparam.xIncr * (1 + cxClient / cbparam.xIncr); cbparam.yMax = cyChar * (cyClient / cyChar - 1);

  20. //调用动态链接库定义中的函数GetStrings GetStrings (&cbparam); EndPaint (hwnd, &ps); return 0; case WM_DESTROY://应用程序结束 PostQuitMessage (0); return 0; } return DefWindowProc (hwnd, iMsg, wParam, lParam); }

More Related