home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / TASKVCLD.PAK / MAIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  3.8 KB  |  146 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1996 by Borland International, All Rights Reserved
  4. //
  5. // MAIN.CPP
  6. // This file is the OWL half of an example that combines OWL and VCL.
  7. // The program creates an OWL-based TApplication object and frame
  8. // window, but in response to a menu command it invokes a VCL form
  9. // that was created with C++Builder.  The form acts as a modal dialog
  10. // box offering the user a choice of colors.  
  11. //
  12. // MAIN.CPP demonstrates the following interactions with a VCL form:
  13. //
  14. // * tell the form what color is  selected (the form shows the user the
  15. //   current selection)
  16. // * invoke the form, making it visible
  17. // * when the form closes, ask it what color was chosen
  18. //
  19. // All this takes place in TMyFrame::CmColor, the menu command handler.
  20. //----------------------------------------------------------------------------
  21. #include <owl/pch.h>
  22. #include <owl/framewin.h>
  23. #include <owl/applicat.h>
  24. #include <classlib/pointer.h>
  25. #include <vcl/vcl.h>            // OK to include VCL & OWL, but OWL first
  26. #pragma hdrstop
  27. #include "adopt.h"              // for adopting VCL's application window
  28. #include "vcldlg.h"             // C++Builder form unit header
  29. #include "main.h"               
  30.  
  31. //
  32. // class TMyFrame
  33. // ~~~~~ ~~~~~~~~
  34. // A method of this object invokes the C++Builder form defined
  35. // in VCLDLG.CPP.
  36. //
  37. class TMyFrame: public TFrameWindow {
  38. public:
  39.   TMyFrame(TWindow* parent, const char far* title = 0)
  40.       : TFrameWindow(parent, title), Color(ClassLib::TColor::SysWindow) {}
  41.   void SetupWindow(void);
  42.  
  43. protected:
  44.   // In response to the CM_COLOR menu command, invokes a VCL-based
  45.   // dialog box where the user selects a color.
  46.   //
  47.   void CmColor();
  48.  
  49. private:
  50.   // Stores the most recently selected window background color.
  51.   // (Default = COLOR_WINDOW)
  52.   //
  53.   ClassLib::TColor Color;
  54.   
  55. DECLARE_RESPONSE_TABLE(TMyFrame);  
  56. };
  57.  
  58. void
  59. TMyFrame::SetupWindow()
  60. {
  61.   // Without this, the VCL TApplication window (which is created first,
  62.   // before WinMain) becomes an invisible peer of the main application
  63.   // window, with its own icon in the task bar.
  64.   //
  65.   AdoptVCLAppWindow(GetHandle());
  66. }
  67.  
  68. //
  69. // class TMyApp
  70. // ~~~~~ ~~~~~~
  71. // A very normal OWL application object.  Does nothing unusual.
  72. // 
  73. class TMyApp : public OWL::TApplication {
  74.   public:
  75.     TMyApp() : OWL::TApplication("VCL Dialog") {}
  76.  
  77.   private:
  78.     void InitMainWindow();
  79. };
  80.  
  81. //
  82. //
  83. //
  84. void
  85. TMyApp::InitMainWindow()
  86. {
  87.   MainWindow = new TMyFrame(0, Name);
  88.   MainWindow->AssignMenu(IDM_APPMENU);
  89. }
  90.  
  91. //
  92. //
  93. //
  94. DEFINE_RESPONSE_TABLE1(TMyFrame, TWindow)
  95.   EV_COMMAND(CM_COLOR, CmColor),
  96. END_RESPONSE_TABLE;
  97.  
  98. //
  99. // CmColor
  100. // Let the user choose a color.  Invoke a dialog box to do this.
  101. // (The dialog box is a C++Builder form.)
  102. //
  103. void
  104. TMyFrame::CmColor()
  105. {
  106.   // Create the form object
  107.   //
  108.   TPointer<TForm1> dlg = new TForm1(::Application);
  109.  
  110.   // Set the color selection scroll bars to the current color setting
  111.   //
  112.   dlg->SetSelectedColor(Color);
  113.  
  114.   // Center the dialog within the frame window's client area
  115.   //
  116.   ClassLib::TRect rect;
  117.   ClassLib::TPoint upperLeft;
  118.   GetClientRect(rect);
  119.   upperLeft.x = (rect.Width()  - dlg->Width)  / 2;
  120.   upperLeft.y = (rect.Height() - dlg->Height) / 2;
  121.   ClientToScreen(upperLeft);
  122.   dlg->Left = upperLeft.x;
  123.   dlg->Top  = upperLeft.y;
  124.  
  125.   // Invoke the form, running it as a modal dialog
  126.   //
  127.   TModalResult result = dlg->ShowModal();
  128.  
  129.   // If user closed with the OK button, change color setting
  130.   //
  131.   if (result == mrOk) {
  132.     Color = dlg->GetSelectedColor();
  133.     SetBkgndColor(Color);
  134.     Invalidate();
  135.   }
  136. }
  137.  
  138. //
  139. //
  140. //
  141. int
  142. OwlMain(int /*argc*/, char* /*argv*/ [])
  143. {
  144.   return TMyApp().Run();
  145. }
  146.