• 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

  • 相关阅读:
    单节点Redis使用 Python pipline大批量插入数据
    Redis进阶实践之十六 Redis大批量增加数据
    Redis进阶实践之十四 Redis-cli命令行工具使用详解
    Redis进阶实践之十三 Redis的Redis-trib.rb脚本文件使用详解
    (error) MOVED 5798 172.17.0.3:6379
    Redis进阶实践之十二 Redis的Cluster集群动态扩容
    [ERR] Node is not empty. Either the node already knows other nodes (check with C
    【Redis】编译错误zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
    Redis进阶实践之十一 Redis的Cluster集群搭建
    linux 安装软件各种错误集锦及解决方法
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/2055930.html
Copyright © 2020-2023  润新知