zoukankan      html  css  js  c++  java
  • Visual C++ 2011520

    一.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.txt
                  GENERIC_READ,             // open for reading
                  0,                        // do not share
                  NULL,                     // no security
                  OPEN_EXISTING,            // existing file only
                  FILE_ATTRIBUTE_NORMAL,    // normal file
                  NULL);                    // no attr. template
    
        if (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.txt
                    FILE_APPEND_DATA,         // open for writing
                    FILE_SHARE_READ,          // allow multiple readers
                    NULL,                     // no security
                    OPEN_ALWAYS,              // open or create
                    FILE_ATTRIBUTE_NORMAL,    // normal file
                    NULL);                    // no attr. template
    
        if (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

  • 相关阅读:
    【python37--面向对象】
    【python36--对象】
    流畅的python,Fluent Python 第十九章笔记 (动态属性和特性)
    流畅的python,Fluent Python 第十八章笔记 (使用asyncio包处理并发)
    Python异常的处理记录
    Python 生成器与它的 send,throw,close 方法(转帖以及记录)
    asyncio:异步I/O、事件循环和并发工具(持续跟新中)
    Enum的使用记录。
    mac下nginx的安装和配置,已经相关设置(转帖)。
    流畅的python,Fluent Python 第十七章笔记 (使用future处理并发)
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/2055930.html
Copyright © 2011-2022 走看看