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

  • 相关阅读:
    格式化时间
    鼠标经过,图片放大事件
    reboot 后 Docker服务及容器自动启动设置
    gin 单个文件函数 上传文件到本地目录里
    深度Linux deepin更新,防火墙操作
    第四篇 mybatis的运行原理(1):重要组件的介绍
    第三篇 常用配置和动态SQL
    第二篇 mybatis的入门
    记一次强转bug
    第一篇 mybatis的简介
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/2055930.html
Copyright © 2011-2022 走看看