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