srt外挂字幕合并器源代码

本文档提供了一个用于合并SRT字幕文件的C语言源代码实现,包括字幕节点结构定义、内存管理、字幕合并算法及Windows GUI应用程序的创建。主要功能包括读取SRT文件、合并字幕、设置延迟时间和错误容限,并通过Windows API创建简单的用户界面进行操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有点乱...嘿嘿
 
第一次用c编写Windows的窗口程序。
SubTitleCombination.cpp关于窗口界面的,足足花了我一个星期,在上班的时候自己偷偷写的。
StrCombination.cpp,处理字幕整合。花了我一天的时间,主要是malloc的问题。

写好之后,很不满意,如果有时间的话,再把代码好好整理整理。

ps:
用vs编译代码之前,要先将unicode 改成 multicode。
在项目属性里面。

程序运行效果见:
http://blog.youkuaiyun.com/zhanjh/archive/2008/11/09/3261000.aspx
下载地址(在csdn上下东西还真够麻烦的,可惜找不到其他的地方):
http://d.download.youkuaiyun.com/down/762283/zhanjh

StrCombination.cpp
  1. #include "StrCombination.h"
  2. #include <stdlib.h>
  3. #define ST_SIZE 50
  4. #define ST_TIME_SIZE 29
  5. #define ST_NUM_SIZE 10
  6. #define ST_CONTENT_SIZE 250
  7. #define ST_MALLOC_SIZE 50
  8. typedef struct tagSubTitle {
  9.     unsigned long   index;
  10.     unsigned long   time;
  11.     unsigned int    csize;
  12.     char            strTime[ST_TIME_SIZE];
  13.     char            *content;
  14.     tagSubTitle     *next;
  15. }SubTitle, *PSubtitile;
  16. typedef struct tagSTNode {
  17.     PSubtitile      item;
  18.     tagSTNode       *next;
  19. }STNode, *PSTNode;
  20. unsigned int stm_index = 0;
  21. unsigned int stm_size = 0;
  22. PSubtitile head = NULL;
  23. int error;
  24. int delay;
  25. char *allchars;
  26. unsigned long allcIndex = 0;
  27. unsigned long mergedSize = 0;
  28. unsigned long subtIndex = 0;
  29. PSTNode pstNodeHead = NULL;
  30. PSTNode pstNode = NULL;
  31. bool getSubTitle(PSubtitile pst, char *subT, unsigned long &index);
  32. void extendPChar(PSubtitile pst,int oldSize, int newSize);
  33. void st_free(PSubtitile pst);
  34. PSubtitile mergeSubTitle(PSubtitile pst1, PSubtitile pst2);
  35. PSubtitile newPst()
  36. {
  37.     
  38.     if(stm_index == 0 || stm_index >= ST_MALLOC_SIZE)
  39.     {
  40.         if(pstNodeHead == NULL)
  41.         {
  42.             pstNodeHead = (PSTNode)malloc(sizeof(tagSTNode));
  43.             pstNodeHead->item = NULL;
  44.             pstNodeHead->next = NULL;
  45.             pstNode =pstNodeHead;
  46.         }
  47.         stm_size += ST_MALLOC_SIZE;
  48.         PSTNode pstnode = (PSTNode)malloc(sizeof(tagSTNode));
  49.         pstnode->item = (PSubtitile)malloc(ST_MALLOC_SIZE * sizeof(tagSubTitle));
  50.         pstnode->next = NULL;
  51.         pstNode->next = pstnode;
  52.         pstNode = pstnode;
  53.         stm_index = 0;
  54.     }
  55.     PSubtitile pst = pstNode->item + stm_index;
  56.     pst->content = NULL;
  57.     pst->next = NULL;
  58.     stm_index++;
  59.     return pst;
  60. }
  61. int tenExp(unsigned int i)
  62. {
  63.     if(i==0)
  64.         return 0;
  65.     int result = 10;
  66.     while(i>0)
  67.     {
  68.         result *= 10;
  69.         i --;
  70.     }
  71.         
  72. }
  73. char *getMergedChars()
  74. {
  75.     unsigned long tempSBTIndex = subtIndex;
  76.     int tempDigitSize = 0;
  77.     while(tempSBTIndex != 0)
  78.     {
  79.         tempSBTIndex /= 10;
  80.         tempDigitSize ++;
  81.     }
  82.     unsigned long numberSize = 0;
  83.     for(int i = 1; i<tempDigitSize; i++)
  84.     {
  85.         numberSize += i * (tenExp(i) - tenExp(i-1));
  86.     }
  87.     numberSize += tempDigitSize * (subtIndex - tenExp(tempDigitSize-1));
  88.     mergedSize += numberSize;
  89.     char *result = (char *)malloc(mergedSize * sizeof(char));
  90.     unsigned long stIndex = 1;
  91.     unsigned long index = 0;
  92.     while(head->next != NULL)
  93.     {
  94.         head = head->next;
  95.         int digitSize = 0;
  96.         int tempIndex = stIndex;
  97.         while(tempIndex != 0)
  98.         {
  99.             for(int i= 0; i < digitSize; i++)
  100.             {
  101.                 result[index - i] = result[index -i - 1];
  102.             }
  103.             result[index - digitSize] = tempIndex % 10 + 48;
  104.             tempIndex /= 10;
  105.             index ++;
  106.             digitSize ++;
  107.         }
  108.         stIndex ++;
  109.         
  110.         result[index++] = 13;
  111.         result[index++] = 10;
  112.         for(int i = 0; i < ST_TIME_SIZE; i++)
  113.         {
  114.             result[index++] = head->strTime[i];
  115.         }
  116.         result[index++] = 13;
  117.         result[index++] = 10;
  118.         for(int i = 0; i < head->csize; i++)
  119.         {
  120.             result[index++] = head->content[i];
  121.         }
  122.         result[index++] = 13;
  123.         result[index++] = 10;
  124.         result[index++] = 13;
  125.         result[index++] = 10;
  126.         
  127.     }
  128.     result[index] = 0;
  129.     mergedSize = index + 1;
  130.     return result;
  131. }
  132. char *newPChar()
  133. {
  134.     char *result = allchars + allcIndex;
  135.     return result;
  136. }
  137. void addToAllChars(char c)
  138. {
  139.     allchars[allcIndex] = c;
  140.     allcIndex ++;
  141. }
  142. char* ST_Merge(char subT1[], char subT2[],unsigned long &mgSize, int stcSize,int parameterDelay, int parameterError)
  143. {
  144.     allchars = (char *)malloc(2 * stcSize * sizeof(char));
  145.     delay = parameterDelay;
  146.     error = parameterError;
  147.     unsigned long index1 = 0;
  148.     unsigned long index2 = 0;
  149.     unsigned long time1 = 0;
  150.     unsigned long time2 = 0;
  151.     unsigned long index = 1;
  152.     PSubtitile pst1;
  153.     PSubtitile pst2;
  154.     PSubtitile list;
  155.     head = newPst();
  156.     pst1 = newPst();
  157.     pst2 = newPst();
  158.     list = head;
  159.     
  160.     while(!(subT1[index1] == 0 && subT2[index2] == 0))
  161.     {
  162.         if(time1 <= time2 && subT1[index1] != 0)
  163.         {
  164.             pst1 = newPst();
  165.             getSubTitle(pst1, subT1, index1);
  166.             time1 = pst1->time;
  167.             list = mergeSubTitle(list, pst1);
  168.         }
  169.         else
  170.         {
  171.             pst2 = newPst();
  172.             getSubTitle(pst2, subT2, index2);
  173.             time2 = pst2->time;
  174.             list = mergeSubTitle(list, pst2);
  175.             
  176.         }
  177.     }
  178.     char *result = getMergedChars();
  179.     mgSize = mergedSize;
  180.     ST_Clearup();
  181.     return result;
  182. }
  183. PSubtitile mergeSubTitle(PSubtitile pst1, PSubtitile pst2)
  184. {
  185.     if(pst1->content == NULL)
  186.     {
  187.         if(pst2->content != NULL)
  188.         {
  189.             pst1->next = pst2;
  190.             pst2->index = 1;
  191.             mergedSize += pst2->csize + ST_TIME_SIZE + 2;
  192.             subtIndex = pst2->index;
  193.             return pst2;
  194.         }
  195.         else
  196.             return pst1;
  197.     }
  198.     if((pst2->time <= pst1->time + error) && (pst2->time + error >= pst1->time)){
  199.         unsigned int csize = pst1->csize + pst2->csize + 2;
  200.         mergedSize +=pst2->csize + 2 + ST_TIME_SIZE + 2;
  201.         char *c = newPChar();
  202.         
  203.         int cindex = 0;
  204.         for(int i = 0; i < pst1->csize; i++)
  205.         {
  206.             addToAllChars(pst1->content[i]);
  207.             cindex ++;
  208.         }
  209.         addToAllChars(13);
  210.         addToAllChars(10);
  211.         
  212.         for(int i = 0; i < pst2->csize; i++)
  213.         {
  214.             addToAllChars(pst2->content[i]);
  215.             cindex ++;
  216.         }
  217.         pst1->content = c;
  218.         pst1->csize = csize;
  219.         return pst1;
  220.     }
  221.     else
  222.     {
  223.         pst1->next = pst2;
  224.         pst2->index = pst1->index + 1;
  225.         mergedSize += pst2->csize + ST_TIME_SIZE + 2;
  226.         subtIndex = pst2->index;
  227.         return pst2;
  228.     }
  229.     
  230. }
  231. void st_free(PSubtitile pst)
  232. {
  233.     free(pst->content);
  234.     if(pst->next != NULL)
  235.         st_free(pst->next);
  236.     free(pst);
  237. }
  238. bool isSTEnd(char *subT, unsigned long &index)
  239. {
  240.     if(subT[index] == 0)
  241.         return true;
  242.     if(subT[index+3] == 0)
  243.     {
  244.         index += 3;
  245.         return true;
  246.     }
  247.     if(subT[index] == 13 && subT[index+1] == 10 && subT[index+2] == 13 && subT[index+3] == 10)
  248.     {
  249.         int tempIndex = index + 4;
  250.         int digit = 0;
  251.         int nextIndex = 0;
  252.         while(subT[tempIndex] == 32 ||
  253.             (subT[tempIndex] == 13 && subT[tempIndex+1] == 10) ||
  254.             (subT[tempIndex-1] == 13 && subT[tempIndex] == 10))
  255.         {
  256.             tempIndex ++;
  257.         }
  258.         nextIndex =  tempIndex;
  259.         while(subT[tempIndex] >= 48 && subT[tempIndex] <= 57)
  260.         {
  261.             digit ++;
  262.             tempIndex ++;
  263.         }
  264.         if(digit = 0)
  265.             return false;
  266.         while(subT[tempIndex] == 32)
  267.             tempIndex ++;
  268.         if(subT[tempIndex] == 13 && subT[tempIndex+1] == 10)
  269.         {
  270.             index = nextIndex;
  271.             return true;
  272.         }
  273.     }
  274.     return false;
  275. }
  276. bool getSubTitle(PSubtitile pst, char *subT, unsigned long &index)
  277. {
  278.     if(pst == NULL)
  279.         return false;
  280.     if(subT[index] <= 0)
  281.         return false;
  282.     while(!(subT[index] == 13 && subT[index+1] == 10))
  283.         index ++;
  284.     while(subT[index] == 32 ||
  285.         (subT[index] == 13 && subT[index+1] == 10) ||
  286.         (subT[index-1] == 13 && subT[index] == 10))
  287.     {
  288.         index ++;
  289.     }
  290.     pst->time = ((unsigned long)(((subT[index]-48) * 10 + (subT[index+1] -48)) * 60 * 60 + 
  291.                                  ((subT[index+3]-48) * 10 + (subT[index+4] -48)) * 60   +
  292.                                  ((subT[index+6]-48) * 10 + (subT[index+7] -48)))) * 1000 +
  293.                                    (subT[index+9]-48) * 100 + (subT[index+10]-48) * 10 + (subT[index+11]-48);
  294.     int tstIndex = 0;
  295.     while(!(subT[index] == 13 &&subT[index+1] == 10))
  296.     {
  297.         if(subT[index] != 32 || (subT[index] == 32 && subT[index+1] != 32))
  298.         {
  299.             pst->strTime[tstIndex] = subT[index];
  300.             tstIndex ++;
  301.         }
  302.         index ++;
  303.     }
  304.     index += 2;
  305.     /*for(int i = 0; i< ST_TIME_SIZE; i++)
  306.     {
  307.         pst->strTime[i] = subT[index+i];
  308.     }
  309.     index += 31;*/
  310.     int contentSize = 0;
  311.     int cindex = 0;
  312.     pst->content = newPChar();
  313.     while(!isSTEnd(subT, index))
  314.     {
  315.         
  316.         addToAllChars(subT[index]);
  317.         cindex ++;
  318.         index ++;
  319.     }
  320.     pst->csize = cindex;
  321.     return true;
  322. }
  323. void extendPChar(PSubtitile pst,int oldSize, int newSize)
  324. {
  325.     char *b = (char *)malloc(newSize * sizeof(char));
  326.     pst->content  = newPChar();
  327.     if(oldSize > 0)
  328.     {
  329.         for(int i = 0; i < oldSize; i++)
  330.         {
  331.             b[i] = pst->content[i];
  332.         }
  333.     }
  334.     pst->content = b;
  335. }
  336. void ST_Clearup()
  337. {
  338.     PSTNode pnode;
  339.     do{
  340.         pnode = pstNodeHead;
  341.         pstNodeHead = pstNodeHead->next;
  342.         free(pnode->item);
  343.         free(pnode);
  344.     }while(pstNodeHead != NULL);
  345.     free(allchars);
  346.     stm_index = 0;
  347.     stm_size = 0;
  348.     head = NULL;
  349.     error;
  350.     delay;
  351.     allcIndex = 0;
  352.     mergedSize = 0;
  353.     subtIndex = 0;
  354.     pstNodeHead = NULL;
  355.     pstNode = NULL;
  356. }
StrCombination.h
  1. #if !defined STR_COMBINATION_H
  2. #define STR_COMBINATION_H
  3. void ST_Initialize();
  4. char* ST_Merge(char subT1[], char subT2[],unsigned long &mgSize, int stcSize,int parameterDelay, int parameterError);
  5. void ST_Clearup();
  6. #endif
SubTitleCombination.cpp
  1. #include <windows.h>
  2. #include "StrCombination.h"
  3. #define IDC_TITLE_LEFT 101
  4. #define IDC_BTN_OPEN_LEFT 102
  5. #define IDC_TITLE_RIGHT 103
  6. #define IDC_BTN_OPEN_RIGHT 104
  7. #define IDC_BTN_SAVE_LEFT 105
  8. #define IDC_BTN_SAVE_RIGHT 106
  9. #define IDC_TEXT_LEFT 201
  10. #define IDC_TEXT_RIGHT 202
  11. #define IDC_NUMBER_DELAY 301
  12. #define IDC_NUMBER_EORROR 302
  13. #define IDC_BTN_MERGE_LEFT 303
  14. #define IDC_BTN_MERGE_RIGHT 304
  15. #define IDC_LABEL_DELAY 305
  16. #define IDC_LABEL_ERROR 306
  17. const char g_szClassName[] = "dowClass";
  18. LPSTR pszSubtitleDest;
  19. LPSTR pszSubtitleSrc;
  20. BOOL getFileName(HWND hwnd,char pszFileName[], bool isForSave)
  21. {
  22.     OPENFILENAME ofn;
  23.     ZeroMemory(&ofn, sizeof(ofn));
  24.     ofn.lStructSize = sizeof(OPENFILENAME);
  25.     ofn.hwndOwner = hwnd;
  26.     ofn.lpstrFilter = "Srt Files (*.srt)/0*.srt/0All Files (*.*)/0*.*/0";
  27.     ofn.lpstrFile = pszFileName;
  28.     ofn.nMaxFile = MAX_PATH;
  29.     if(isForSave)
  30.         ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  31.     else
  32.         ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
  33.     ofn.lpstrDefExt = "srt";
  34.     return GetOpenFileName(&ofn);
  35. }

  36. LPSTR getWinText(HWND hEdit, DWORD &dwBuffSize)
  37. {
  38.     DWORD dwSize = GetWindowTextLength(hEdit);
  39.     dwBuffSize = dwSize + 1;
  40.     LPSTR result = (LPSTR)GlobalAlloc(GPTR,dwBuffSize); //(LPSTR)GlobalAlloc(GPTR, dwBufferSize)
  41.     GetWindowText(hEdit, result, dwBuffSize);
  42.     return result;
  43. }
  44. void DoFileSave(HWND hwnd, LPSTR lps, DWORD buffsize, HWND hEdit)
  45. {
  46.     char pszFileName[MAX_PATH] = "";
  47.     if(!getFileName(hwnd, pszFileName, false))
  48.         return;
  49.     HANDLE hFile;
  50.     hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
  51.         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  52.     if(hFile != INVALID_HANDLE_VALUE)
  53.     {
  54.         DWORD dwWritten;
  55.         WriteFile(hFile, lps, buffsize, &dwWritten, NULL);
  56.     }
  57.     SetWindowText(hEdit, lps);
  58.     free(lps);
  59.     CloseHandle(hFile);
  60. }
  61. void doFileMerge(HWND hwnd, DWORD destId, DWORD srcId)
  62. {
  63.     DWORD bsizeDest = 0, bsizeSrc= 0, bsizeMg;
  64.     pszSubtitleDest = getWinText(GetDlgItem(hwnd,destId), bsizeDest);
  65.     pszSubtitleSrc = getWinText(GetDlgItem(hwnd, srcId), bsizeSrc);
  66.     BOOL bSuccess = FALSE;
  67.     int delay = GetDlgItemInt(hwnd,IDC_NUMBER_DELAY,&bSuccess, FALSE);
  68.     if(!bSuccess)
  69.         delay = 0;
  70.     int error = GetDlgItemInt(hwnd, IDC_NUMBER_EORROR, &bSuccess, FALSE);
  71.     if(!bSuccess)
  72.         error = 0;
  73.     LPSTR pszCombination = ST_Merge(pszSubtitleDest, pszSubtitleSrc, bsizeMg, bsizeDest+bsizeSrc, delay, error);
  74.     DoFileSave(hwnd, pszCombination, bsizeMg, GetDlgItem(hwnd,destId));
  75. }
  76. BOOL loadSrtFileToEdit(HWND hEdit, LPCTSTR pszFileName)
  77. {
  78.     HANDLE hFile;
  79.     BOOL bSuccess = FALSE;
  80.     hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
  81.         OPEN_EXISTING, 0, NULL);
  82.     if(hFile != INVALID_HANDLE_VALUE)
  83.     {
  84.         DWORD dwFileSize;
  85.         dwFileSize = GetFileSize(hFile, NULL);
  86.         if(dwFileSize != 0xFFFFFFFF)
  87.         {
  88.             LPSTR pszFileText;
  89.             pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);
  90.             if(pszFileText != NULL)
  91.             {
  92.                 DWORD dwRead;
  93.                 if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
  94.                 {
  95.                     pszFileText[dwFileSize] = 0; // Add null terminator
  96.                     if(SetWindowText(hEdit, pszFileText))
  97.                         bSuccess = TRUE; // It worked!
  98.                 }
  99.                 GlobalFree(pszFileText);
  100.             }
  101.         }
  102.         CloseHandle(hFile);
  103.     }
  104.     return bSuccess;
  105. }
  106. void doFileLoad(HWND hwnd, DWORD titleId, DWORD contentId)
  107. {
  108.     HWND hTitle;
  109.     HWND hEdit;
  110.     char pszFileName[MAX_PATH] = "";
  111.     if(!getFileName(hwnd, pszFileName, true))
  112.         return;
  113.     hTitle = GetDlgItem(hwnd, titleId);
  114.     if(hTitle == NULL)
  115.     {
  116.         MessageBox(hwnd, "Cannot find hTitle""Error", MB_OK | MB_ICONERROR);
  117.         return;
  118.     }
  119.     if(!SetWindowText(hTitle, pszFileName))
  120.     {
  121.         GlobalFree(pszFileName);
  122.         return;
  123.     }
  124.     hEdit = GetDlgItem(hwnd, contentId);
  125.     if(hEdit == NULL)
  126.     {
  127.         MessageBox(hwnd, "Cannot find hEdit""Error", MB_OK | MB_ICONERROR);
  128.         return;
  129.     }
  130.     if(loadSrtFileToEdit(hEdit,pszFileName))
  131.     {
  132.     }
  133. }
  134. HWND addHEdit(HWND hWndParent,DWORD hWndId,DWORD dwStyle, int x, int y, int nWidth, int nHeight)
  135. {
  136.     HFONT hfDefault;
  137.     HWND hEdit;
  138.     hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT""",
  139.         dwStyle,
  140.         x, y, nWidth, nHeight, hWndParent, (HMENU)hWndId, GetModuleHandle(NULL), NULL);
  141.     if(hEdit == NULL)
  142.         MessageBox(hWndParent, "Could not create edit box.""Error", MB_OK | MB_ICONERROR);
  143.     hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
  144.     SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
  145.     return hEdit;
  146. }
  147. void addText(HWND hwnd, PCSTR pszTxt,DWORD hwndId, int x, int y)
  148. {
  149.     HWND htxt; 
  150.     HFONT hfDefault;
  151.     hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
  152.     htxt= CreateWindowEx(WS_EX_TRANSPARENT,
  153.         "Static",pszTxt,WS_CHILD | WS_VISIBLE  ,
  154.         x, y, 50, 15, hwnd, (HMENU)hwndId, GetModuleHandle(NULL),NULL);
  155.     LOGFONT stFont;
  156.     memset(&stFont, 0, sizeof(LOGFONT));
  157.     stFont.lfHeight = 14;
  158.     stFont.lfWeight = FW_NORMAL;
  159.     WPARAM wp = (WPARAM)CreateFontIndirect(&stFont);
  160.     SendMessage(htxt, WM_SETFONT,wp,MAKELPARAM(FALSE,0));
  161. }
  162. HWND addContentEdit(HWND hWndParent,DWORD hWndId, int x, int y, int nWidth, int nHeight)
  163. {
  164.     return addHEdit(hWndParent, hWndId,
  165.         WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_READONLY ,
  166.         x, y, nWidth, nHeight);
  167. }
  168. HWND addTitleEdit(HWND hWndParent,DWORD hWndId, int x, int y, int nWidth, int nHeight)
  169. {
  170.     return addHEdit(hWndParent,hWndId,
  171.         WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_READONLY ,
  172.         x, y, nWidth, nHeight);
  173. }
  174. HWND addNumberEdit(HWND hWndParent,DWORD hWndId, int x, int y, int nWidth, int nHeight)
  175. {
  176.     return addHEdit(hWndParent,hWndId,
  177.         WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_NUMBER ,
  178.         x, y, nWidth, nHeight);
  179. }
  180. HWND addButton(HWND hWndParent, DWORD hWndId,PCSTR pszName, int x, int y)
  181. {
  182.     HWND hBtn;
  183.     hBtn=CreateWindowEx(0,"BUTTON",pszName,WS_CHILD|WS_VISIBLE|BS_FLAT|WS_BORDER,
  184.         x,y,40,20,hWndParent,(HMENU)hWndId,GetModuleHandle(NULL),NULL);  
  185.     return hBtn;
  186. }
  187. HWND addSButton(HWND hWndParent, DWORD hWndId,PCSTR pszName, int x, int y)
  188. {
  189.     HWND hBtn;
  190.     hBtn=CreateWindowEx(0,"BUTTON",pszName,WS_CHILD|WS_VISIBLE|BS_FLAT|WS_BORDER,
  191.         x,y,25,20,hWndParent,(HMENU)hWndId,GetModuleHandle(NULL),NULL);  
  192.     return hBtn;
  193. }
  194. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  195. {
  196.     switch(msg)
  197.     {
  198.     case WM_CREATE:
  199.         {
  200.             addTitleEdit(hwnd, IDC_TITLE_LEFT,5 ,5, 200, 20);
  201.             addTitleEdit(hwnd, IDC_TITLE_RIGHT, 430, 5, 200, 20);
  202.             addContentEdit(hwnd, IDC_TEXT_LEFT, 5, 30, 350, 530);
  203.             addContentEdit(hwnd, IDC_TEXT_RIGHT, 430, 30, 350, 530);
  204.             addButton(hwnd, IDC_BTN_OPEN_LEFT, "open", 210, 5);
  205.             addButton(hwnd, IDC_BTN_OPEN_RIGHT, "open", 635, 5);
  206.             addText(hwnd, "Delay/ms",IDC_LABEL_DELAY, 365, 85);
  207.             addNumberEdit(hwnd,IDC_NUMBER_DELAY,365,100,50,20);
  208.             addText(hwnd, "Eorror/ms",IDC_LABEL_ERROR, 365, 135);
  209.             addNumberEdit(hwnd,IDC_NUMBER_EORROR,365,150,50,20);
  210.             addSButton(hwnd, IDC_BTN_MERGE_LEFT, "<=", 362, 205);
  211.             addSButton(hwnd, IDC_BTN_MERGE_RIGHT, "=>", 392, 205);
  212.             
  213.         }
  214.         break;
  215.     case WM_SIZE:
  216.         {
  217.             RECT rcClient;
  218.             GetClientRect(hwnd, &rcClient);
  219.             HWND teLeft = GetDlgItem(hwnd, IDC_TITLE_LEFT);
  220.             HWND teRight = GetDlgItem(hwnd, IDC_TITLE_RIGHT);
  221.             HWND boLeft = GetDlgItem(hwnd, IDC_BTN_OPEN_LEFT);
  222.             HWND boRight = GetDlgItem(hwnd, IDC_BTN_OPEN_RIGHT);
  223.             HWND ceLeft = GetDlgItem(hwnd, IDC_TEXT_LEFT);
  224.             HWND ceRight = GetDlgItem(hwnd, IDC_TEXT_RIGHT);
  225.             HWND nmDelay = GetDlgItem(hwnd, IDC_NUMBER_DELAY);
  226.             HWND nmError = GetDlgItem(hwnd, IDC_NUMBER_EORROR);
  227.             HWND lDelay = GetDlgItem(hwnd, IDC_LABEL_DELAY);
  228.             HWND lError = GetDlgItem(hwnd, IDC_LABEL_ERROR);
  229.             HWND bmLeft = GetDlgItem(hwnd, IDC_BTN_MERGE_LEFT);
  230.             HWND bmRight = GetDlgItem(hwnd, IDC_BTN_MERGE_RIGHT);
  231.             int xCenter = rcClient.right / 2;
  232.             int lLeft = 430 * rcClient.right/800;
  233.             int cHeight = 565 * rcClient.bottom / 600;
  234.             int cWidth = 365 * rcClient.right / 800;
  235.             SetWindowPos(teLeft, NULL,5,5, 200, 20, SWP_NOZORDER);
  236.             SetWindowPos(teRight, NULL, lLeft, 5, 200, 20, SWP_NOZORDER);
  237.             
  238.             SetWindowPos(boLeft, NULL, 210, 5,40, 20, SWP_NOZORDER);
  239.             SetWindowPos(boRight, NULL, lLeft + 205, 5,40, 20, SWP_NOZORDER);
  240.             SetWindowPos(ceLeft, NULL, 5, 30,  cWidth,cHeight, SWP_NOZORDER);
  241.             SetWindowPos(ceRight, NULL, lLeft, 30,  cWidth,cHeight, SWP_NOZORDER);
  242.             SetWindowPos(nmDelay, NULL, xCenter - 25, 100, 50, 20 ,SWP_NOZORDER);
  243.             SetWindowPos(nmError, NULL, xCenter - 25, 150, 50, 20, SWP_NOZORDER);
  244.             SetWindowPos(lDelay, NULL, xCenter - 25, 85, 50, 15, SWP_NOZORDER);
  245.             SetWindowPos(lError, NULL, xCenter - 25, 135, 50, 15, SWP_NOZORDER);
  246.             SetWindowPos(bmLeft, NULL, xCenter - 26, 205, 25, 20, SWP_NOZORDER);
  247.             SetWindowPos(bmRight, NULL, xCenter + 4, 205, 25, 20, SWP_NOZORDER);
  248.         }
  249.         break;
  250.     case WM_CLOSE:
  251.         DestroyWindow(hwnd);
  252.         break;
  253.     case WM_DESTROY:
  254.         PostQuitMessage(0);
  255.         break;
  256.     case WM_COMMAND:
  257.         switch(LOWORD(wParam))
  258.         {
  259.         case IDC_BTN_OPEN_LEFT:
  260.             {
  261.                 doFileLoad(hwnd, IDC_TITLE_LEFT, IDC_TEXT_LEFT);
  262.             }
  263.             break;
  264.         case IDC_BTN_OPEN_RIGHT:
  265.             {
  266.                 doFileLoad(hwnd, IDC_TITLE_RIGHT, IDC_TEXT_RIGHT);
  267.             }
  268.             break;
  269.         case IDC_BTN_MERGE_LEFT:
  270.             {
  271.                 doFileMerge(hwnd, IDC_TEXT_LEFT, IDC_TEXT_RIGHT);
  272.             }
  273.             break;
  274.         case IDC_BTN_MERGE_RIGHT:
  275.             {
  276.                 doFileMerge(hwnd, IDC_TEXT_RIGHT, IDC_TEXT_LEFT);
  277.             }
  278.             break;
  279.         }
  280.         break;
  281.       
  282.     default:
  283.         return DefWindowProc(hwnd, msg, wParam, lParam);
  284.     }
  285.     return 0;
  286. }
  287. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  288.                    LPSTR lpCmdLine, int nCmdShow)
  289. {
  290.     WNDCLASSEX wc;
  291.     HWND hwnd;
  292.     MSG Msg;
  293.     wc.cbSize        = sizeof(WNDCLASSEX);
  294.     wc.style         = 0;
  295.     wc.lpfnWndProc   = WndProc;
  296.     wc.cbClsExtra    = 0;
  297.     wc.cbWndExtra    = 0;
  298.     wc.hInstance     = hInstance;
  299.     wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  300.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  301.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  302.     wc.lpszMenuName = NULL;
  303.     wc.lpszClassName = g_szClassName;
  304.     wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  305.     if(!RegisterClassEx(&wc))
  306.     {
  307.         MessageBox(NULL, "Window Registration Failed!""Error!",
  308.             MB_ICONEXCLAMATION | MB_OK);
  309.         return 0;
  310.     }
  311.     hwnd = CreateWindowEx(
  312.         WS_EX_CLIENTEDGE,
  313.         g_szClassName,
  314.         "字幕合并器",
  315.         WS_OVERLAPPEDWINDOW,
  316.         CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
  317.         NULL, NULL, hInstance, NULL);
  318.     if(hwnd == NULL)
  319.     {
  320.         MessageBox(NULL, "Window Creation Failed!""Error!",
  321.             MB_ICONEXCLAMATION | MB_OK);
  322.         return 0;
  323.     }
  324.     ShowWindow(hwnd, nCmdShow);
  325.     UpdateWindow(hwnd);
  326.     while(GetMessage(&Msg, NULL, 0, 0) > 0)
  327.     {
  328.         TranslateMessage(&Msg);
  329.         DispatchMessage(&Msg);
  330.     }
  331.     return Msg.wParam;
  332. }


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值