1 / 15

( 第二十四讲 )

( 第二十四讲 ). 吉林大学远程教育课件. Windows A P I 编 程. 学 时: 48. 主讲人 : 翟慧杰. 编辑控件实例 2 #include <windows.h> HINSTANCE hInst; HWND hMainWnd; HWND hCtl; int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int); LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

anila
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. 编辑控件实例2 #include <windows.h> HINSTANCE hInst; HWND hMainWnd; HWND hCtl; int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int); LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { hInst = hInstance; WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS; wcex.lpfnWndProc = (WNDPROC)WndProc;

  3. wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance,IDI_APPLICATION); wcex.hCursor = LoadCursor(NULL,IDC_ARROW); wcex.hbrBackground = (HBRUSH)COLOR_WINDOW; wcex.lpszMenuName = NULL; wcex.lpszClassName = "MainWndClass"; wcex.hIconSm = LoadIcon(hInstance,IDI_APPLICATION); if(!RegisterClassEx(&wcex)) return FALSE; int SW_XFS = GetSystemMetrics(SM_CXSCREEN); int SW_YFS = GetSystemMetrics(SM_CYSCREEN);

  4. hMainWnd = CreateWindowEx( WS_EX_CLIENTEDGE, "MainWndClass", "Main window of the edit control", WS_OVERLAPPEDWINDOW, 0, 0, SW_XFS, SW_YFS, NULL, NULL, hInstance, NULL);

  5. if(!hMainWnd) return FALSE; ShowWindow(hMainWnd,nCmdShow); UpdateWindow(hMainWnd); MSG msg; while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static int wwp,whp; RECT rect;

  6. switch(message) { case WM_CREATE: GetClientRect(hWnd,&rect); wwp = rect.right-rect.left; whp = rect.bottom-rect.top; hCtl = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", NULL, WS_CHILD|WS_HSCROLL|WS_VSCROLL|WS_VISIBLE |ES_MULTILINE|ES_AUTOHSCROLL|ES_AUTOVSCROLL,

  7. wwp/4, whp/4, wwp/2,whp/2,hWnd, NULL, hInst, NULL); if(!hCtl) PostQuitMessage(0); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd,message,wParam,lParam); } return 0; }

  8. 第三节 列表框 列表框允许用户从多个对象中选择一项 或多项,这些对象可以是文本、文件和 位图等。当列表框的项不能全部显示出来 时,可以用滚动条来滚动显示。 1.基础知识 默认的列表框只允许用户单选,需要多选时,须把列表框的风格定义为LBS_ MULTIPLESEL和 LBS_EXTENDEDSEL。列表框提供的其他风格取值可以控制列表框的外观和操作。例如是否按照列表框各项的名称排序,是否多行排列,是否有滚动条等。

  9. 列表框的风格取值如表6-7所示。

  10. 当用户对列表框进行了操作时,就会向主 窗口发送一条消息。列表框的消息类型 比较简单,主要是单击、双击和选择等。 同编辑框一样,列表框也会遇到内存不 够的问题,所以消息类型中含有这一项。 消息总是通过WM_COMMAND的形式发 送给主窗口的。其中,wParam的低位字 节表示列表框的标识符,高位字节表示消 息的类型,lParam表示主窗口句柄。列表 框的消息类型如表所示。

  11. 列表框也是通过调用函数CreateWindow来 创建的,第一参数设置为ListBox即可。风 格取值可以取表所示的值。在主窗口里的 消息一般是在 WM_ COMMAND里处理表 中以LBN开头的消息。下面以一个具体的 例子介绍列表框的常见操作。

  12. 2.应用程序举例 列表框的应用: #include <windows.h> #include <stdlib.h> #include <string.h> #define MAXENV 4096 static char szAppName[]="Environ"; //预先申明消息处理、申请窗口类以及应用程序初始化的函数 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); //函数:WinMain //作用:主应用函数 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {

  13. MSG msg ; //申请窗口类 MyRegisterClass(hInstance); //应用程序的初始化 if(!InitInstance(hInstance,iCmdShow)) { return FALSE; } while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wParam ; }

More Related