home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2000 July / VPR0007A.BIN / OLS / DEN8_2B6 / den8_2b6.lzh / MainFrm.cpp < prev    next >
C/C++ Source or Header  |  1996-08-11  |  9KB  |  367 lines

  1. // MainFrm.cpp : CMainFrame クラスの動作の定義を行います。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "DdeClient.h"
  6.  
  7. #include "MainFrm.h"
  8.  
  9. #include "CommandDialog.h"
  10.  
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. void    DoEvents(int nTimes)
  18. {
  19.     MSG msg;
  20.  
  21.     for (int i = 0; i < nTimes; i++)
  22.     {
  23.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  24.         {
  25.             if (!TheApp.PreTranslateMessage(&msg))  // これ追加してみました
  26.             {
  27.                 TranslateMessage(&msg);
  28.                 DispatchMessage(&msg);
  29.             }
  30.         }
  31.     }
  32. }
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CMainFrame
  36.  
  37. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  38.  
  39. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  40.     //{{AFX_MSG_MAP(CMainFrame)
  41.     ON_WM_CREATE()
  42.     ON_MESSAGE(WM_DDE_ACK, OnDdeAck)
  43.     ON_COMMAND(ID_EDIT_PASTE_LINK, OnEditPasteLink)
  44.     ON_MESSAGE(WM_DDE_TERMINATE, OnDdeTerminate)
  45.     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_LINK, OnUpdateEditPasteLink)
  46.     ON_WM_DESTROY()
  47.     //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49.  
  50. static UINT indicators[] =
  51. {
  52.     ID_SEPARATOR,           // ステータス ライン インジケータ
  53.     ID_INDICATOR_KANA,
  54.     ID_INDICATOR_CAPS,
  55.     ID_INDICATOR_NUM,
  56.     ID_INDICATOR_SCRL,
  57. };
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CMainFrame クラスの構築/消滅
  61.  
  62. CMainFrame::CMainFrame()
  63. {
  64.     // TODO: この位置にメンバの初期化処理コードを追加してください。
  65.     m_status = NONE;
  66.     m_hwndServer = NULL;    
  67. }
  68.  
  69. CMainFrame::~CMainFrame()
  70. {
  71. }
  72.  
  73. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  74. {
  75.     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  76.         return -1;
  77.     
  78.     if (!m_wndToolBar.Create(this) ||
  79.         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  80.     {
  81.         TRACE0("Failed to create toolbar\n");
  82.         return -1;      // 作成に失敗
  83.     }
  84.  
  85.     if (!m_wndStatusBar.Create(this) ||
  86.         !m_wndStatusBar.SetIndicators(indicators,
  87.           sizeof(indicators)/sizeof(UINT)))
  88.     {
  89.         TRACE0("Failed to create status bar\n");
  90.         return -1;      // 作成に失敗
  91.     }
  92.  
  93.     // TODO: もしツール チップスが必要ない場合、ここを削除してください。
  94.     m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
  95.         CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  96.  
  97.     // TODO: ツール バーをドッキング可能にしない場合は以下の3行を削除
  98.     //       してください。
  99.     m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  100.     EnableDocking(CBRS_ALIGN_ANY);
  101.     DockControlBar(&m_wndToolBar);
  102.  
  103.     return 0;
  104. }
  105.  
  106. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  107. {
  108.     // TODO: この位置で CREATESTRUCT cs を修正して、Window クラスやスタイルを
  109.     //       修正してください。
  110.     CString sSection("Main Frame");
  111.  
  112.     cs.x = TheApp.GetProfileInt(sSection, "Left", 0);
  113.     cs.y = TheApp.GetProfileInt(sSection, "Top", 0);
  114.     cs.cx = TheApp.GetProfileInt(sSection, "Width", 256);
  115.     cs.cy = TheApp.GetProfileInt(sSection, "Height", 128);
  116.  
  117.     return CFrameWnd::PreCreateWindow(cs);
  118. }
  119.  
  120. /////////////////////////////////////////////////////////////////////////////
  121. // CMainFrame クラスの診断
  122.  
  123. #ifdef _DEBUG
  124. void CMainFrame::AssertValid() const
  125. {
  126.     CFrameWnd::AssertValid();
  127. }
  128.  
  129. void CMainFrame::Dump(CDumpContext& dc) const
  130. {
  131.     CFrameWnd::Dump(dc);
  132. }
  133.  
  134. #endif //_DEBUG
  135.  
  136. /////////////////////////////////////////////////////////////////////////////
  137. // CMainFrame メッセージ ハンドラ
  138.  
  139. LONG    CMainFrame::OnDdeAck(WPARAM wParam, LPARAM lParam)
  140. {
  141.     switch (m_status)
  142.     {
  143.     case INITIATE:
  144.         m_hwndServer = (HWND)wParam;
  145.         m_status = IDLE;
  146.         break;
  147.     case TRANSACT:
  148.         DDEACK ack;
  149.         HGLOBAL hCommand;
  150.         UnpackDDElParam(WM_DDE_ACK, lParam, (UINT*)&ack, (UINT*)&hCommand);
  151.         GlobalFree(hCommand);
  152.         char cBuf   [128];
  153.         wsprintf(cBuf, "%02x", ack.bAppReturnCode & 0xff);
  154.         AfxMessageBox(cBuf);
  155.         m_status = IDLE;
  156.         break;
  157.     case TERMINATE:
  158.     case NONE:
  159.     case IDLE:
  160.         MessageBeep(MB_OK);
  161.         break;
  162.     }
  163.  
  164.     return 1;
  165. }
  166.  
  167. void    CMainFrame::OnEditPasteLink() 
  168. {
  169.     switch (m_status)
  170.     {
  171.     case NONE:
  172.         DDEInitiate();
  173.         break;
  174.     case IDLE:
  175.         DDETerminate();
  176.         break;
  177.     case INITIATE:
  178.     case TRANSACT:
  179.     case TERMINATE:
  180.         MessageBeep(MB_ICONHAND);
  181.         break;
  182.     }
  183. }
  184.  
  185. void    CMainFrame::DDEInitiate()
  186. {
  187.     m_status = INITIATE;
  188.  
  189.     if (m_pViewActive)
  190.         m_pViewActive->EnableWindow(FALSE);
  191.     m_wndToolBar.LoadToolBar(IDR_MAINFRAME);
  192.  
  193.     CString sApp = TheApp.GetProfileString("DDE", "App", "Den8");
  194.     ATOM aApp = GlobalAddAtom(sApp);
  195.     if (aApp == 0)
  196.     {
  197.         AfxMessageBox("booo");
  198.         m_status = NONE;
  199.         return;
  200.     }
  201.  
  202.     CString sTopic = TheApp.GetProfileString("DDE", "Topic", "Command");
  203.     ATOM aTopic = GlobalAddAtom(sTopic);
  204.     if (aTopic == 0)
  205.     {
  206.         GlobalDeleteAtom(aApp);
  207.         AfxMessageBox("booo");
  208.         m_status = NONE;
  209.         return;
  210.     }
  211.  
  212.     char cMessage[128];
  213.     wsprintf(cMessage, "Initiating: App=%s Topic=%s", (LPCSTR)sApp, (LPCSTR)sTopic);
  214.     AfxMessageBox(cMessage, MB_ICONINFORMATION);
  215.  
  216.     ::SendMessage(HWND_BROADCAST, WM_DDE_INITIATE, (WPARAM)m_hWnd, MAKELPARAM(aApp, aTopic));
  217.  
  218.     GlobalDeleteAtom(aApp);
  219.     GlobalDeleteAtom(aTopic);
  220.  
  221.     if (m_hwndServer == NULL)
  222.     {
  223.         AfxMessageBox("サーバが応答しません。サーバを起動してから、\n[編集]->[リンク貼り付け]を実行してください");
  224.         m_status = NONE;
  225.         return;
  226.     }
  227.  
  228.     if (m_pViewActive)
  229.         m_pViewActive->EnableWindow(TRUE);
  230.     m_wndToolBar.LoadToolBar(IDR_MAINFRAME1);
  231.  
  232.     m_status = IDLE;
  233. }
  234.  
  235. void    CMainFrame::DDETerminate()
  236. {
  237.     m_status = TERMINATE;
  238.  
  239.     if (::PostMessage(m_hwndServer, WM_DDE_TERMINATE, (WPARAM)m_hWnd, 0) == FALSE)
  240.     {
  241.         m_status = NONE;
  242.         return;
  243.     }
  244.  
  245.     if (m_pViewActive)
  246.         m_pViewActive->EnableWindow(FALSE);
  247.     m_wndToolBar.LoadToolBar(IDR_MAINFRAME);
  248.  
  249.     for (DWORD dwBegin = GetTickCount(); m_status == TERMINATE && GetTickCount() - dwBegin < 8000; )
  250.     {
  251.         Sleep(100);
  252.         DoEvents(32);
  253.     }
  254. }
  255.  
  256. LONG    CMainFrame::OnDdeTerminate(WPARAM wParam, LPARAM lParam)
  257. {
  258.     switch (m_status)
  259.     {
  260.     case TERMINATE:
  261.         m_status = NONE;
  262.         break;
  263.     case IDLE:
  264.     case TRANSACT:
  265.         ::PostMessage(m_hwndServer, WM_DDE_TERMINATE, (WPARAM)m_hWnd, 0);
  266.         m_status = NONE;
  267.         if (m_pViewActive)
  268.             m_pViewActive->EnableWindow(FALSE);
  269.         m_wndToolBar.LoadToolBar(IDR_MAINFRAME);
  270.         break;
  271.     case INITIATE:
  272.     case NONE:
  273.         MessageBeep(0xffffffff);
  274.         break;
  275.     }
  276.     
  277.     return 1;
  278. }
  279.  
  280. void    CMainFrame::OnUpdateEditPasteLink(CCmdUI* pCmdUI) 
  281. {
  282.     if (m_status == NONE)
  283.     {
  284.         pCmdUI->SetText("リンク貼り付け(&L)");
  285.     }
  286.     else
  287.     {
  288.         pCmdUI->SetText("リンク解除(&L)");
  289.     }
  290. }
  291.  
  292. void    CMainFrame::OnDestroy() 
  293. {
  294.     CString sSection("Main Frame");
  295.  
  296.     CRect rc;
  297.     GetWindowRect(rc);
  298.     TheApp.WriteProfileInt(sSection, "Left", rc.left);
  299.     TheApp.WriteProfileInt(sSection, "Top", rc.top);
  300.     TheApp.WriteProfileInt(sSection, "Width", rc.Width());
  301.     TheApp.WriteProfileInt(sSection, "Height", rc.Height());
  302.  
  303.     CFrameWnd::OnDestroy();
  304. }
  305.  
  306. BOOL CMainFrame::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle, CWnd* pParentWnd, CCreateContext* pContext) 
  307. {
  308.     dwDefaultStyle &= ~FWS_ADDTOTITLE;
  309.  
  310.     return CFrameWnd::LoadFrame(nIDResource, dwDefaultStyle, pParentWnd, pContext);
  311. }
  312.  
  313. BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam) 
  314. {
  315.     if (wParam == ID_DO_TEST)
  316.     {
  317.         DoTest((LPCSTR)lParam);
  318.         return TRUE;
  319.     }
  320.     
  321.     return CFrameWnd::OnCommand(wParam, lParam);
  322. }
  323.  
  324. void    CMainFrame::DoTest(LPCSTR pszCommandLine)
  325. {
  326.     if (m_status != IDLE)
  327.     {
  328.         MessageBeep(MB_ICONHAND);
  329.         return;
  330.     }
  331.  
  332.     m_status = TRANSACT;
  333.  
  334.     HGLOBAL hCommand;
  335.     if (!(hCommand = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, strlen(pszCommandLine) + 1)))
  336.     {
  337.         AfxMessageBox("fail");
  338.         m_status = IDLE;
  339.         return;
  340.     }
  341.     char* pszCommand;
  342.     if (!(pszCommand = (char*)GlobalLock(hCommand)))
  343.     {
  344.         GlobalFree(hCommand);
  345.         AfxMessageBox("fail");
  346.         m_status = IDLE;
  347.         return;
  348.     }
  349.     strcpy(pszCommand, pszCommandLine);
  350.     GlobalUnlock(hCommand);
  351.     LPARAM lParam = PackDDElParam(WM_DDE_EXECUTE, 0, (UINT)hCommand);
  352.     if (::PostMessage(m_hwndServer, WM_DDE_EXECUTE, (WPARAM)m_hWnd, lParam) == FALSE)
  353.     {
  354.         GlobalFree(hCommand);
  355.         FreeDDElParam(WM_DDE_EXECUTE, lParam);
  356.         AfxMessageBox("post error");
  357.         m_status = IDLE;
  358.         return;
  359.     }
  360.  
  361.     for (DWORD dwBegin = GetTickCount(); m_status == TRANSACT && GetTickCount() - dwBegin < 120000; )
  362.     {
  363.         Sleep(250);
  364.         DoEvents(32);
  365.     }
  366. }
  367.