首页 > 代码库 > SDK源码分析 15

SDK源码分析 15

  1 #include <windows.h>
  2 #define ID_LIST     1
  3 #define ID_TEXT     2
  4 #define MAXREAD     8192
  5 #define DIRATTR     (DDL_READWRITE|DDL_READONLY|DDL_HIDDEN|DDL_SYSTEM|  6                      DDL_DIRECTORY|DDL_ARCHIVE|DDL_DRIVES)
  7 #define DTFLAGS     (DT_WORDBREAK|DT_EXPANDTABS|DT_NOCLIP|DT_NOPREFIX)
  8 
  9 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
 10 LRESULT CALLBACK ListProc(HWND, UINT, WPARAM, LPARAM);
 11 WNDPROC OldList;
 12 
 13 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nCmdShow)
 14 {
 15     WNDCLASS wndclass;
 16     HWND hWnd;
 17     MSG msg;
 18     TCHAR szClassName[] = TEXT("MyClass"), szWindowsName[] = TEXT("MyWindows");
 19 
 20     wndclass.cbClsExtra = 0;
 21     wndclass.cbWndExtra = 0;
 22     wndclass.hbrBackground = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));//(HBRUSH)GetStockObject(WHITE_BRUSH);
 23     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
 24     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 25     wndclass.hInstance = hInstance;
 26     wndclass.lpfnWndProc = WndProc;
 27     wndclass.lpszClassName = szClassName;
 28     wndclass.lpszMenuName = NULL;
 29     wndclass.style = CS_VREDRAW | CS_HREDRAW;
 30 
 31     if (!RegisterClass(&wndclass))
 32     {
 33         MessageBox(hWnd, TEXT("The Application on Windows NT!"), TEXT("ERROR~"), MB_OK);
 34     }
 35 
 36     hWnd = CreateWindow(szClassName,
 37         szWindowsName,
 38         WS_OVERLAPPEDWINDOW,
 39         CW_USEDEFAULT,
 40         CW_USEDEFAULT,
 41         CW_USEDEFAULT,
 42         CW_USEDEFAULT,
 43         NULL, NULL, hInstance, NULL);
 44     ShowWindow(hWnd, nCmdShow);
 45     UpdateWindow(hWnd);
 46 
 47     while (GetMessage(&msg, NULL, NULL, NULL))
 48     {
 49         TranslateMessage(&msg);
 50         DispatchMessage(&msg);
 51     }
 52     return msg.wParam;
 53 }
 54 
 55 LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
 56 {
 57     static BOOL          bValidFile;
 58     static BYTE          buffer[MAXREAD];
 59     static HWND          hWndList, hWndText;
 60     static RECT          rect;
 61     static TCHAR         szFile[MAX_PATH + 1];
 62     HANDLE               hFile;
 63     int                  i, cxChar, cyChar;
 64     TCHAR                szBuffer[MAX_PATH + 1];
 65     PAINTSTRUCT          ps;
 66     HDC                  hdc;
 67 
 68     switch (msg)
 69     {
 70     case WM_CREATE:
 71         cxChar = LOWORD(GetDialogBaseUnits());
 72         cyChar = HIWORD(GetDialogBaseUnits());
 73 
 74         rect.left = 20 * cxChar;//矩形位置 左边 =20*cxchar
 75         rect.top = 3 * cyChar;//矩形位置 顶边 =3*cyChar
 76 
 77         //创建 一个列表框
 78         hWndList = CreateWindow(TEXT("listbox"), NULL,
 79             WS_CHILDWINDOW | WS_VISIBLE | LBS_STANDARD,
 80             cxChar, cyChar * 3,
 81             cxChar * 13 + GetSystemMetrics(SM_CXVSCROLL),
 82             cyChar * 10,
 83             hWnd, (HMENU)ID_LIST,
 84             (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
 85             
 86         //获取当前目录 保存到szBuffer
 87         GetCurrentDirectory(MAX_PATH + 1, szBuffer);
 88         
 89         //创建一个 静态文本框 左对齐
 90         hWndText = CreateWindow(TEXT("static"), szBuffer,
 91             WS_CHILDWINDOW | WS_VISIBLE | SS_LEFT,
 92             cxChar, cyChar, cxChar*MAX_PATH, cyChar,
 93             hWnd, (HMENU)ID_TEXT, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
 94 
 95         //窗口子类化 列表框 返回旧 过程
 96         OldList = (WNDPROC)SetWindowLong(hWndList, GWL_WNDPROC, (LPARAM)ListProc);
 97         
 98         //发送消息给列表框 
 99         SendMessage(hWndList, LB_DIR, DIRATTR, (LPARAM)TEXT("*.*"));
100         return 0;
101     case WM_SIZE:
102         rect.bottom = LOWORD(lParam);
103         rect.right = HIWORD(lParam);
104         //设置矩形范围
105         return 0;
106     case WM_COMMAND:
107         if (LOWORD(wParam) == ID_LIST && HIWORD(wParam) == LBN_DBLCLK)//如果是列表框消息 和 列表框双击
108         {
109             if (LB_ERR == (i = SendMessage(hWndList, LB_GETCURSEL, 0, 0)))//如果出错就跳出
110                 break;
111             SendMessage(hWndList, LB_GETTEXT, i, (LPARAM)szBuffer);//发送 获取文本的消息 保存到szBuffer
112 
113 
114             //打开文件名:szBuffer 只读 只读请求 空 文件必须要存在  如果返回INVALID_HANDLE_VALUE 代表出错
115             if (INVALID_HANDLE_VALUE != (hFile = CreateFile(szBuffer,
116                 GENERIC_READ, FILE_SHARE_READ, NULL,
117                 OPEN_EXISTING, 0, NULL)))
118             {
119                 CloseHandle(hFile);//关闭文件句柄
120                 bValidFile = TRUE;//标记真
121                 lstrcpy(szFile, szBuffer);//字符串拷贝 到szFile
122                 GetCurrentDirectory(MAX_PATH + 1, szBuffer);//测试 文件是否子文件
123                 if (szBuffer[lstrlen(szBuffer) - 1] != \\)//如果最后的内容不是 ‘\\‘
124                 {
125                     lstrcat(szBuffer, TEXT("\\")); //字符串szBuffer加入 ‘\\‘
126                 }
127                 SetWindowText(hWndText, lstrcat(szBuffer, szFile));//设置静态文本框内容 
128             }
129             else
130             {
131                 bValidFile = FALSE;//标记关闭
132                 szBuffer[lstrlen(szBuffer) - 1] = \0;//加入结束符号
133                 if (!SetCurrentDirectory(szBuffer + 1))//改变当前目录 取反
134                 {
135                     szBuffer[3] = :;//字符串加入
136                     szBuffer[4] = \0;//字符串加入
137                     SetCurrentDirectory(szBuffer + 2);//更改新的的 盘符
138                 }
139                 GetCurrentDirectory(MAX_PATH + 1, szBuffer);//获取当前内容
140                 SetWindowText(hWndText, szBuffer);//设置文本
141                 SendMessage(hWndList, LB_RESETCONTENT, 0, 0);
142                 SendMessage(hWndList, LB_DIR, DIRATTR, (LPARAM)TEXT("*.*"));
143             }
144             InvalidateRect(hWnd, NULL, TRUE);//立即重绘
145         }
146         return 0;
147     case WM_PAINT:
148         if (!bValidFile)//如果没在读取
149             break;
150         if (INVALID_HANDLE_VALUE =http://www.mamicode.com/= (hFile = CreateFile(szFile,
151             GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL)))//打开文件 失败
152         {
153             bValidFile = FALSE;//标记关闭 跳出
154             break;
155         }
156         ReadFile(hFile, buffer, MAXREAD, (LPDWORD)&i, NULL);//读取文件内容 
157         CloseHandle(hFile);//关闭文件
158 
159         hdc = BeginPaint(hWnd, &ps);
160         
161         SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
162         SetTextColor(hdc, GetSysColor(COLOR_BTNTEXT));
163         SetBkColor(hdc, GetSysColor(COLOR_BTNFACE));
164         DrawTextA(hdc, (LPCSTR)buffer, i, &rect, DTFLAGS);//画出文本
165 
166         EndPaint(hWnd, &ps);
167         return 0;
168     case WM_DESTROY:
169         PostQuitMessage(0);
170         return 0;
171     }
172 
173     return DefWindowProc(hWnd, msg, wParam, lParam);
174 }
175 
176 LRESULT CALLBACK ListProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
177 {
178     if(msg==WM_KEYDOWN && wParam ==VK_RETURN)
179     { 
180         SendMessage(GetParent(hWnd), WM_COMMAND, MAKELONG(1, LBN_DBLCLK), (LPARAM)hWnd);
181     }
182     return CallWindowProc(OldList, hWnd, msg, wParam, lParam);
183 }

 

SDK源码分析 15