硬汉嵌入式论坛

 找回密码
 立即注册
查看: 1925|回复: 0
收起左侧

[其它] 一款开源的VNC软件turbovnc,纯Win32 API实现

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106856
QQ
发表于 2020-6-9 09:18:26 | 显示全部楼层 |阅读模式


纯纯的Win32 API实现,下面是VNC的文件传输实现:

https://github.com/TurboVNC/turbovnc/blob/master/win/vncviewer/FileTransfer.cpp

  1. LRESULT CALLBACK FileTransfer::FileTransferDlgProc(HWND hwnd, UINT uMsg,
  2.                                                    WPARAM wParam,
  3.                                                    LPARAM lParam)
  4. {
  5.   FileTransfer *_this = (FileTransfer *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  6.   int i;

  7.   switch (uMsg) {

  8.     case WM_INITDIALOG:
  9.     {
  10.       SetForegroundWindow(hwnd);
  11.       CenterWindow(hwnd);
  12.       return TRUE;
  13.     }
  14.     break;

  15.     case WM_HELP:
  16.       help.Popup(lParam);
  17.       return 0;

  18.     case WM_COMMAND:
  19.     {
  20.       switch (LOWORD(wParam)) {
  21.         case IDC_CLIENTPATH:
  22.           switch (HIWORD(wParam)) {
  23.             case EN_SETFOCUS:
  24.               SetWindowText(GetDlgItem(hwnd, IDC_FTCOPY), noactionText);
  25.               EnableWindow(GetDlgItem(hwnd, IDC_FTCOPY), FALSE);
  26.               return TRUE;
  27.           }
  28.           break;
  29.         case IDC_SERVERPATH:
  30.           switch (HIWORD(wParam)) {
  31.             case EN_SETFOCUS:
  32.               SetWindowText(GetDlgItem(hwnd, IDC_FTCOPY), noactionText);
  33.               EnableWindow(GetDlgItem(hwnd, IDC_FTCOPY), FALSE);
  34.               return TRUE;
  35.           }
  36.           break;
  37.         case IDC_EXIT:
  38.         case IDCANCEL:
  39.           PostMessage(hwnd, WM_CLOSE, 0, 0);
  40.           return TRUE;
  41.         case IDC_CLIENTUP:
  42.           SetWindowText(GetDlgItem(hwnd, IDC_FTCOPY), noactionText);
  43.           EnableWindow(GetDlgItem(hwnd, IDC_FTCOPY), FALSE);
  44.           SendMessage(_this->m_hwndFTProgress, PBM_SETPOS, 0, 0);
  45.           SetWindowText(_this->m_hwndFTStatus, "");
  46.           if (strcmp(_this->m_ClientPathTmp, "") == 0) return TRUE;
  47.           for (i = (int)(strlen(_this->m_ClientPathTmp) - 2); i >= 0; i--) {
  48.             if (_this->m_ClientPathTmp[i] == '\\') {
  49.               _this->m_ClientPathTmp[i] = '\0';
  50.               break;
  51.             }
  52.             if (i == 0) _this->m_ClientPathTmp[0] = '\0';
  53.           }
  54.           _this->ShowClientItems(_this->m_ClientPathTmp);
  55.           return TRUE;
  56.         case IDC_SERVERUP:
  57.           SetWindowText(GetDlgItem(hwnd, IDC_FTCOPY), noactionText);
  58.           EnableWindow(GetDlgItem(hwnd, IDC_FTCOPY), FALSE);
  59.           SendMessage(_this->m_hwndFTProgress, PBM_SETPOS, 0, 0);
  60.           SetWindowText(_this->m_hwndFTStatus, "");
  61.           if (strcmp(_this->m_ServerPathTmp, "") == 0) return TRUE;
  62.           for (i = (int)(strlen(_this->m_ServerPathTmp) - 2); i >= 0; i--) {
  63.             if (_this->m_ServerPathTmp[i] == '\\') {
  64.               _this->m_ServerPathTmp[i] = '\0';
  65.               break;
  66.             }
  67.             if (i == 0) _this->m_ServerPathTmp[0] = '\0';
  68.           }
  69.           _this->SendFileListRequestMessage(_this->m_ServerPathTmp, 0);
  70.           return TRUE;
  71.         case IDC_CLIENTRELOAD:
  72.           SetWindowText(GetDlgItem(hwnd, IDC_FTCOPY), noactionText);
  73.           EnableWindow(GetDlgItem(hwnd, IDC_FTCOPY), FALSE);
  74.           SendMessage(_this->m_hwndFTProgress, PBM_SETPOS, 0, 0);
  75.           SetWindowText(_this->m_hwndFTStatus, "");
  76.           _this->ShowClientItems(_this->m_ClientPath);
  77.           return TRUE;
  78.         case IDC_SERVERRELOAD:
  79.           SetWindowText(GetDlgItem(hwnd, IDC_FTCOPY), noactionText);
  80.           EnableWindow(GetDlgItem(hwnd, IDC_FTCOPY), FALSE);
  81.           SendMessage(_this->m_hwndFTProgress, PBM_SETPOS, 0, 0);
  82.           SetWindowText(_this->m_hwndFTStatus, "");
  83.           _this->SendFileListRequestMessage(_this->m_ServerPathTmp, 0);
  84.           return TRUE;
  85.         case IDC_FTCOPY:
  86.           // First, check if the action is supported by the server.
  87.           if (_this->m_bFTCOPY == FALSE) {
  88.             // Upload was requested.
  89.             if (!_this->m_clientconn->m_clientMsgCaps.IsEnabled(rfbFileUploadRequest) ||
  90.                 !_this->m_clientconn->m_clientMsgCaps.IsEnabled(rfbFileUploadData)) {
  91.               MessageBox(hwnd, "Sorry but the server does not support uploading files.",
  92.                          "Error", MB_OK | MB_ICONEXCLAMATION);
  93.               char buf[MAX_PATH];
  94.               SPRINTF(buf, "File upload not supported by server");
  95.               SetWindowText(_this->m_hwndFTStatus, buf);
  96.               return TRUE;
  97.             }
  98.           } else {
  99.             // Download was requested.
  100.             if (!_this->m_clientconn->m_clientMsgCaps.IsEnabled(rfbFileDownloadRequest) ||
  101.                 !_this->m_clientconn->m_serverMsgCaps.IsEnabled(rfbFileDownloadData)) {
  102.               MessageBox(hwnd, "Sorry but the server does not support downloading files.",
  103.                          "Error", MB_OK | MB_ICONEXCLAMATION);
  104.               char buf[MAX_PATH];
  105.               SPRINTF(buf, "File download not supported by server");
  106.               SetWindowText(_this->m_hwndFTStatus, buf);
  107.               return TRUE;
  108.             }
  109.           }
  110.           // Now, try to upload/download.
  111.           SetWindowText(GetDlgItem(hwnd, IDC_FTCOPY), noactionText);
  112.           EnableWindow(GetDlgItem(hwnd, IDC_FTCOPY), FALSE);
  113.           if (_this->m_ClientPath[0] == '\0' ||
  114.               _this->m_ServerPath[0] == '\0') {
  115.             SetWindowText(_this->m_hwndFTStatus,
  116.                           "Cannot transfer files: illegal directory.");
  117.             return TRUE;
  118.           }
  119.           if (_this->m_bFTCOPY == FALSE) {
  120.             _this->m_bTransferEnable = TRUE;
  121.             _this->m_bReportUploadCancel = TRUE;
  122.             EnableWindow(GetDlgItem(hwnd, IDC_FTCANCEL), TRUE);
  123.             _this->FileTransferUpload();
  124.           } else {
  125.             return _this->SendMultipleFileDownloadRequests();
  126.           }
  127.           return TRUE;
  128.         case IDC_FTCANCEL:
  129.           // Check if we allowed to interrupt the transfer.
  130.           if (_this->m_bUploadStarted &&
  131.               !_this->m_clientconn->m_clientMsgCaps.IsEnabled(rfbFileUploadFailed)) {
  132.             char buf[MAX_PATH];
  133.             SPRINTF(buf, "Sorry, but interrupting upload is not supported by the server");
  134.             SetWindowText(_this->m_hwndFTStatus, buf);
  135.             return TRUE;
  136.           }
  137.           if (_this->m_bDownloadStarted &&
  138.               !_this->m_clientconn->m_clientMsgCaps.IsEnabled(rfbFileDownloadCancel)) {
  139.             char buf[MAX_PATH];
  140.             SPRINTF(buf, "Sorry, but interrupting download is not supported by the server");
  141.             SetWindowText(_this->m_hwndFTStatus, buf);
  142.             return TRUE;
  143.           }
  144.           // Now try to cancel the operation.
  145.           SetWindowText(GetDlgItem(hwnd, IDC_FTCOPY), noactionText);
  146.           EnableWindow(GetDlgItem(hwnd, IDC_FTCOPY), FALSE);
  147.           _this->m_bTransferEnable = FALSE;
  148.           EnableWindow(GetDlgItem(hwnd, IDC_FTCANCEL), FALSE);
  149.           return TRUE;
  150.         case IDC_CLIENTBROWSE_BUT:
  151.           SetWindowText(GetDlgItem(hwnd, IDC_FTCOPY), noactionText);
  152.           EnableWindow(GetDlgItem(hwnd, IDC_FTCOPY), FALSE);
  153.           SendMessage(_this->m_hwndFTProgress, PBM_SETPOS, 0, 0);
  154.           SetWindowText(_this->m_hwndFTStatus, "");
  155.           _this->CreateFTBrowseDialog(FALSE);
  156.           return TRUE;
  157.         case IDC_SERVERBROWSE_BUT:
  158.           SetWindowText(GetDlgItem(hwnd, IDC_FTCOPY), noactionText);
  159.           EnableWindow(GetDlgItem(hwnd, IDC_FTCOPY), FALSE);
  160.           SendMessage(_this->m_hwndFTProgress, PBM_SETPOS, 0, 0);
  161.           SetWindowText(_this->m_hwndFTStatus, "");
  162.           _this->CreateFTBrowseDialog(TRUE);
  163.           return TRUE;
  164.       }
  165.     }
  166.     break;

  167.     case WM_NOTIFY:
  168.       switch (LOWORD(wParam)) {
  169.         case IDC_FTCLIENTLIST:
  170.           switch (((LPNMHDR)lParam)->code) {
  171.             case NM_SETFOCUS:
  172.               SetWindowText(GetDlgItem(hwnd, IDC_FTCOPY), uploadText);
  173.               EnableWindow(GetDlgItem(hwnd, IDC_FTCOPY), TRUE);
  174.               _this->m_bFTCOPY = FALSE;
  175.               return TRUE;
  176.             case LVN_GETDISPINFO:
  177.               _this->OnGetDispClientInfo((NMLVDISPINFO *)lParam);
  178.               return TRUE;
  179.             case LVN_ITEMACTIVATE:
  180.               LPNMITEMACTIVATE lpnmia = (LPNMITEMACTIVATE)lParam;
  181.               _this->ProcessListViewDBLCLK(_this->m_hwndFTClientList,
  182.                                            _this->m_ClientPath,
  183.                                            _this->m_ClientPathTmp,
  184.                                            lpnmia->iItem);
  185.               return TRUE;
  186.           }
  187.           break;
  188.         case IDC_FTSERVERLIST:
  189.           switch (((LPNMHDR)lParam)->code) {
  190.             case NM_SETFOCUS:
  191.               SetWindowText(GetDlgItem(hwnd, IDC_FTCOPY), downloadText);
  192.               EnableWindow(GetDlgItem(hwnd, IDC_FTCOPY), TRUE);
  193.               _this->m_bFTCOPY = TRUE;
  194.               return TRUE;
  195.             case LVN_GETDISPINFO:
  196.               _this->OnGetDispServerInfo((NMLVDISPINFO *)lParam);
  197.               return TRUE;
  198.             case LVN_ITEMACTIVATE:
  199.               LPNMITEMACTIVATE lpnmia = (LPNMITEMACTIVATE)lParam;
  200.               _this->ProcessListViewDBLCLK(_this->m_hwndFTServerList,
  201.                                            _this->m_ServerPath,
  202.                                            _this->m_ServerPathTmp,
  203.                                            lpnmia->iItem);
  204.               return TRUE;
  205.           }
  206.           break;
  207.       }
  208.       break;

  209.     case WM_CLOSE:
  210.       _this->m_clientconn->m_fileTransferDialogShown = false;
  211.       _this->m_FTClientItemInfo.Free();
  212.       _this->m_FTServerItemInfo.Free();
  213.       {
  214.         VNCviewerApp32 *pApp = (VNCviewerApp32 *)(_this->m_clientconn->m_pApp);
  215.         pApp->RemoveModelessDialog(hwnd);
  216.       }
  217.       DestroyWindow(hwnd);
  218.       return TRUE;

  219.   }
  220.   return 0;
  221. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-8 05:12 , Processed in 0.141040 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表