一.LockFile和UnlockFile
锁定文件部分区域,防止其他程序进行该部分区域的读写
#include <windows.h>
#include <stdio.h>void main()
{HANDLE hFile;HANDLE hAppend;DWORD dwBytesRead, dwBytesWritten, dwPos;BYTE buff[4096];// Open the existing file.hFile = CreateFile(TEXT("one.txt"), // open One.txtGENERIC_READ, // open for reading0, // do not shareNULL, // no securityOPEN_EXISTING, // existing file onlyFILE_ATTRIBUTE_NORMAL, // normal fileNULL); // no attr. templateif (hFile == INVALID_HANDLE_VALUE){printf("Could not open One.txt."); return;}// Open the existing file, or if the file does not exist,// create a new file.hAppend = CreateFile(TEXT("two.txt"), // open Two.txtFILE_APPEND_DATA, // open for writingFILE_SHARE_READ, // allow multiple readersNULL, // no securityOPEN_ALWAYS, // open or createFILE_ATTRIBUTE_NORMAL, // normal fileNULL); // no attr. templateif (hAppend == INVALID_HANDLE_VALUE){printf("Could not open Two.txt."); return;}// Append the first file to the end of the second file.// Lock the second file to prevent another process from// accessing it while writing to it. Unlock the// file when writing is complete.while (ReadFile(hFile, buff, sizeof(buff), &dwBytesRead, NULL)&& dwBytesRead > 0){dwPos = SetFilePointer(hAppend, 0, NULL, FILE_END);LockFile(hAppend, dwPos, 0, dwBytesRead, 0);WriteFile(hAppend, buff, dwBytesRead, &dwBytesWritten, NULL);UnlockFile(hAppend, dwPos, 0, dwBytesRead, 0);}// Close both files.CloseHandle(hFile);CloseHandle(hAppend);
}
二.GetOverlappedResult
此函数可以查询I/O处理过程中的传输状态.如下代码示例,可以通过以下方式查看状态,见SDK示例(Testing for the End of a File)
// Check the result of the asynchronous read
// without waiting (forth parameter FALSE).
bResult = GetOverlappedResult(hFile,&stOverlapped,&dwBytesRead,FALSE) ; if (bResult)
{ float floatOffset=(float)stOverlapped.Offset;printf("ReadFile operation completed (%f)\n",floatOffset/dwFileSize);// Manual-reset event should be reset since it is now signaled.ResetEvent(stOverlapped.hEvent);
}
三.Dialog消息循环
Dialog有着自己的消息循环,见下示例代码,About消息循环在DialogBox函数中创建
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;
}
四.对话框参数
DialogBoxParam 方法多了一个参数,可以在WM_INITDIALOG消息中传递一个参数,同理CreateDialog和CreateDialogParam也是如此
五.ATL窗体类介绍
很好的一篇文章
http://msdn.microsoft.com/en-us/library/aa260759(VS.60).aspx
http://www.vckbase.com/document/viewdoc/?id=1119