sdk_exe.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: samples/dll/my_exe.cpp
  3. // Purpose: Sample showing how to use wx DLL from a Win32 application
  4. // Author: Vadim Zeitlin
  5. // Created: 2009-12-07
  6. // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. /*
  10. This program is intentionally as simple as possible and shouldn't be seen
  11. as an example of how to write a proper Win32 application (why should you
  12. want to do this anyhow when you have wxWidgets). It's just a test bed for
  13. the wx DLL which it uses.
  14. */
  15. // ============================================================================
  16. // declarations
  17. // ============================================================================
  18. // ----------------------------------------------------------------------------
  19. // headers
  20. // ----------------------------------------------------------------------------
  21. #include <windows.h>
  22. #include <windowsx.h>
  23. #include <stdio.h>
  24. #include <tchar.h>
  25. #include "my_dll.h"
  26. namespace
  27. {
  28. // ----------------------------------------------------------------------------
  29. // constants
  30. // ----------------------------------------------------------------------------
  31. const TCHAR *MAIN_WIN_CLASS_NAME = _TEXT("my_exe_main_win_class");
  32. const int IDB_RUN_GUI_FROM_DLL = 100;
  33. // ----------------------------------------------------------------------------
  34. // globals
  35. // ----------------------------------------------------------------------------
  36. HINSTANCE g_hInstance;
  37. HWND g_hwndMain;
  38. // ============================================================================
  39. // implementation
  40. // ============================================================================
  41. // ----------------------------------------------------------------------------
  42. // callbacks
  43. // ----------------------------------------------------------------------------
  44. void
  45. OnCommand(HWND /* hwnd */, int id, HWND /* hwndCtl */, UINT /* codeNotify */)
  46. {
  47. if ( id == IDB_RUN_GUI_FROM_DLL )
  48. {
  49. run_wx_gui_from_dll("child instance");
  50. }
  51. }
  52. void OnDestroy(HWND hwnd)
  53. {
  54. wx_dll_cleanup();
  55. PostQuitMessage(0);
  56. }
  57. LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  58. {
  59. switch ( msg )
  60. {
  61. HANDLE_MSG(hwnd, WM_COMMAND, OnCommand);
  62. HANDLE_MSG(hwnd, WM_DESTROY, OnDestroy);
  63. default:
  64. return DefWindowProc(hwnd, msg, wParam, lParam);
  65. }
  66. return 0;
  67. }
  68. // ----------------------------------------------------------------------------
  69. // initialization functions
  70. // ----------------------------------------------------------------------------
  71. bool RegisterMainClass()
  72. {
  73. WNDCLASS wc;
  74. ZeroMemory(&wc, sizeof(wc));
  75. wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  76. wc.lpfnWndProc = MainWndProc;
  77. wc.hInstance = g_hInstance;
  78. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  79. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  80. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  81. wc.lpszClassName = MAIN_WIN_CLASS_NAME;
  82. return RegisterClass(&wc) != 0;
  83. }
  84. bool CreateMainWindow()
  85. {
  86. g_hwndMain = CreateWindow
  87. (
  88. MAIN_WIN_CLASS_NAME,
  89. _TEXT("Main Win32 app"),
  90. WS_OVERLAPPEDWINDOW,
  91. CW_USEDEFAULT, CW_USEDEFAULT,
  92. 400, 300,
  93. NULL, NULL, g_hInstance, NULL
  94. );
  95. if ( !g_hwndMain )
  96. return false;
  97. CreateWindow
  98. (
  99. _TEXT("static"),
  100. _TEXT("Main Win32 application"),
  101. WS_CHILD | WS_VISIBLE,
  102. 10, 10, 200, 30,
  103. g_hwndMain, (HMENU)-1, g_hInstance, NULL
  104. );
  105. CreateWindow
  106. (
  107. _TEXT("button"),
  108. _TEXT("Run GUI from DLL"),
  109. WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
  110. 200, 200, 150, 35,
  111. g_hwndMain, (HMENU)IDB_RUN_GUI_FROM_DLL, g_hInstance, NULL
  112. );
  113. return true;
  114. }
  115. } // anonymous namespace
  116. // ----------------------------------------------------------------------------
  117. // entry point
  118. // ----------------------------------------------------------------------------
  119. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
  120. {
  121. g_hInstance = hInstance;
  122. if ( !RegisterMainClass() )
  123. return 1;
  124. if ( !CreateMainWindow() )
  125. return 2;
  126. ShowWindow(g_hwndMain, nCmdShow);
  127. MSG msg;
  128. while ( GetMessage(&msg, NULL, 0, 0) )
  129. {
  130. TranslateMessage(&msg);
  131. DispatchMessage(&msg);
  132. }
  133. return 0;
  134. }