xref: /AOO41X/main/sal/workben/clipboardwben/testviewer/cbvtest.cxx (revision 87d2adbc9cadf14644c3679b041b9226f7630199)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 // MARKER(update_precomp.py): autogen include statement, do not remove
26 #include "precompiled_sal.hxx"
27 // TestWin32.cpp : Definiert den Einsprungpunkt f�r die Anwendung.
28 //
29 
30 #define _WIN32_DCOM
31 
32 #include "stdafx.h"
33 
34 #include <windows.h>
35 
36 #include <ole2.h>
37 #include <objidl.h>
38 #include <objbase.h>
39 #include <process.h>
40 #include <olectl.h>
41 #include <stdlib.h>
42 #include <malloc.h>
43 #include <crtdbg.h>
44 #include <..\..\inc\systools\win32\MtaOleClipb.h>
45 
46 #include "resource.h"
47 
48 #define MAX_LOADSTRING 100
49 
50 // Globale Variablen:
51 HINSTANCE           g_hInst;                        // aktuelle Instanz
52 HWND                g_hwndMain;
53 WCHAR               szTitle[MAX_LOADSTRING];            // Text der Titelzeile
54 WCHAR               szWindowClass[MAX_LOADSTRING];  // Text der Titelzeile
55 LPSTREAM            g_pStm    = NULL;
56 char*               pTextBuff = NULL;
57 DWORD               lData     = 0;
58 UINT                g_nCBChanges = 0;
59 
60 // forward declaration
61 ATOM  MyRegisterClass( HINSTANCE hInstance );
62 BOOL  InitInstance( HINSTANCE, int );
63 HMENU GetSubMenuHandle( HWND hwndParent, UINT uiTopLevelIndex );
64 BOOL  IsClipboardViewer( HWND hwndParent );
65 void  SwitchMenuState( HWND hwndParent );
66 void  RegisterClipboardViewer( BOOL bRegister );
67 void ShowAvailableClipboardFormats( HWND hWnd, HDC hdc, PAINTSTRUCT ps, RECT rcWnd );
68 void ClearClipboardContent( HWND hWnd );
69 
70 void    CALLBACK OnClipboardContentChange( void );
71 LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
72 LRESULT CALLBACK About( HWND, UINT, WPARAM, LPARAM );
73 
74 //----------------------------------------------------
75 // WinMain
76 //----------------------------------------------------
77 
WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)78 int APIENTRY WinMain(HINSTANCE hInstance,
79                      HINSTANCE hPrevInstance,
80                      LPSTR     lpCmdLine,
81                      int       nCmdShow )
82 {
83     // ZU ERLEDIGEN: F�gen Sie hier den Code ein.
84     MSG     msg;
85     HACCEL  hAccelTable;
86     HRESULT hr = E_FAIL;
87 
88     // it's important to initialize ole
89     // in order to use the clipboard
90     //hr = OleInitialize( NULL );
91     hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
92 
93     g_hInst = hInstance;
94 
95     // Globale Zeichenfolgen initialisieren
96     LoadStringW(g_hInst, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
97     LoadStringW(g_hInst, IDC_TESTWIN32, szWindowClass, MAX_LOADSTRING);
98     MyRegisterClass(g_hInst);
99 
100     // Initialisierung der Anwendung durchf�hren:
101     if( !InitInstance( g_hInst, nCmdShow ) )
102     {
103         return FALSE;
104     }
105 
106     hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TESTWIN32);
107 
108     // Hauptnachrichtenschleife:
109     while( GetMessage(&msg, NULL, 0, 0) )
110     {
111         if( !TranslateAccelerator (msg.hwnd, hAccelTable, &msg) )
112         {
113             TranslateMessage( &msg );
114             DispatchMessage( &msg );
115         }
116     }
117 
118     // uninitializing the ole libraries
119     //OleUninitialize( );
120     CoUninitialize( );
121 
122     return msg.wParam;
123 }
124 
125 
126 
127 //
128 //  FUNKTION: MyRegisterClass()
129 //
130 //  AUFGABE: Registriert die Fensterklasse.
131 //
132 //  KOMMENTARE:
133 //
134 //    Diese Funktion und ihre Verwendung sind nur notwendig, wenn dieser Code
135 //    mit Win32-Systemen vor der 'RegisterClassEx'-Funktion kompatibel sein soll,
136 //    die zu Windows 95 hinzugef�gt wurde. Es ist wichtig diese Funktion aufzurufen,
137 //    damit der Anwendung kleine Symbole mit den richtigen Proportionen zugewiesen
138 //    werden.
139 //
MyRegisterClass(HINSTANCE hInstance)140 ATOM MyRegisterClass( HINSTANCE hInstance )
141 {
142     WNDCLASSEXW wcex;
143 
144     wcex.cbSize = sizeof(WNDCLASSEX);
145 
146     wcex.style          = CS_HREDRAW | CS_VREDRAW;
147     wcex.lpfnWndProc    = (WNDPROC)WndProc;
148     wcex.cbClsExtra     = 0;
149     wcex.cbWndExtra     = 0;
150     wcex.hInstance      = hInstance;
151     wcex.hIcon          = LoadIcon(hInstance, (LPCTSTR)IDI_TESTWIN32);
152     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
153     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
154     wcex.lpszMenuName   = (LPCWSTR)IDC_TESTWIN32;
155     wcex.lpszClassName  = szWindowClass;
156     wcex.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
157 
158     return RegisterClassExW(&wcex);
159 }
160 
161 //
162 //   FUNKTION: InitInstance(HANDLE, int)
163 //
164 //   AUFGABE: Speichert die Instanzzugriffsnummer und erstellt das Hauptfenster
165 //
166 //   KOMMENTARE:
167 //
168 //        In dieser Funktion wird die Instanzzugriffsnummer in einer globalen Variable
169 //        gespeichert und das Hauptprogrammfenster erstellt und angezeigt.
170 //
InitInstance(HINSTANCE hInstance,int nCmdShow)171 BOOL InitInstance( HINSTANCE hInstance, int nCmdShow )
172 {
173    g_hwndMain = CreateWindowExW(0, szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
174       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
175 
176    if( !IsWindow( g_hwndMain ) )
177    {
178       return FALSE;
179    }
180 
181    ShowWindow( g_hwndMain, nCmdShow );
182    UpdateWindow( g_hwndMain );
183 
184    return TRUE;
185 }
186 
187 //
188 //  FUNKTION: WndProc(HWND, unsigned, WORD, LONG)
189 //
190 //  AUFGABE:  Verarbeitet Nachrichten f�r das Hauptfenster.
191 //
192 //  WM_COMMAND  - Anwendungsmen� verarbeiten
193 //  WM_PAINT    - Hauptfenster darstellen
194 //  WM_DESTROY  - Beendigungsnachricht ausgeben und zur�ckkehren
195 //
196 //
WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)197 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
198 {
199     int         wmId;
200     int         wmEvent;
201     PAINTSTRUCT ps;
202     HDC         hdc;
203     WCHAR       szHello[MAX_LOADSTRING];
204 
205 
206     LoadStringW(g_hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
207 
208     switch( message )
209     {
210     case WM_CREATE:
211         ClearClipboardContent( hWnd );
212         break;
213 
214         case WM_COMMAND:
215             wmId    = LOWORD(wParam);
216             wmEvent = HIWORD(wParam);
217             // Men�auswahlen analysieren:
218             switch( wmId )
219             {
220                 case IDD_CBVIEWER:
221                     SwitchMenuState( hWnd );
222                     RegisterClipboardViewer( IsClipboardViewer( hWnd ) );
223                     break;
224 
225                 case IDM_EXIT:
226                    DestroyWindow( hWnd );
227                    break;
228 
229                 default:
230                    return DefWindowProc( hWnd, message, wParam, lParam );
231             }
232             break;
233 
234         case WM_PAINT:
235             hdc = BeginPaint (hWnd, &ps);
236             // ZU ERLEDIGEN: Hier beliebigen Code zum Zeichnen hinzuf�gen...
237             RECT rt;
238             GetClientRect( hWnd, &rt );
239 
240             if ( IsClipboardViewer( g_hwndMain ) )
241             {
242                 ShowAvailableClipboardFormats( hWnd, hdc, ps, rt );
243             }
244             else
245             {
246                 WCHAR wcString[MAX_LOADSTRING];
247                 LoadStringW(g_hInst, IDS_MSG_CBVIEWER_IS_OFF, wcString, MAX_LOADSTRING);
248                 DrawTextW( hdc, wcString, wcslen( wcString ), &rt, DT_CENTER );
249             }
250 
251             EndPaint( hWnd, &ps );
252             break;
253 
254         case WM_DESTROY:
255             PostQuitMessage( 0 );
256             break;
257 
258         default:
259             return DefWindowProc( hWnd, message, wParam, lParam );
260    }
261    return 0;
262 }
263 
GetSubMenuHandle(HWND hwndParent,UINT uiTopLevelIndex)264 HMENU GetSubMenuHandle( HWND hwndParent, UINT uiTopLevelIndex )
265 {
266     HMENU hMenuMain = GetMenu( hwndParent );
267     _ASSERT( IsMenu( hMenu ) );
268 
269     HMENU hSubMenu = GetSubMenu( hMenuMain, uiTopLevelIndex );
270     _ASSERT( IsMenu( hSubMenu ) );
271 
272     return hSubMenu;
273 }
274 
IsClipboardViewer(HWND hwndParent)275 BOOL IsClipboardViewer( HWND hwndParent )
276 {
277     HMENU hSubMenu = GetSubMenuHandle( hwndParent, 0 );
278     UINT uiMState = GetMenuState( hSubMenu, 0, MF_BYPOSITION );
279     return ( MF_CHECKED == uiMState );
280 }
281 
SwitchMenuState(HWND hwndParent)282 void SwitchMenuState( HWND hwndParent )
283 {
284     HMENU hSubMenu = GetSubMenuHandle( hwndParent, 0 );
285     WCHAR wcMenuString[MAX_LOADSTRING];
286 
287     if ( IsClipboardViewer( hwndParent ) )
288     {
289         LoadStringW(g_hInst, IDS_CBVIEWER_OFF, wcMenuString, MAX_LOADSTRING);
290         ModifyMenuW( hSubMenu, 0, MF_BYPOSITION | MF_STRING, IDD_CBVIEWER, wcMenuString );
291         CheckMenuItem( hSubMenu, 0, MF_BYPOSITION | MF_UNCHECKED );
292     }
293     else
294     {
295         LoadStringW(g_hInst, IDS_CBVIEWER_ON, wcMenuString, MAX_LOADSTRING);
296         ModifyMenuW( hSubMenu, 0, MF_BYPOSITION | MF_STRING, IDD_CBVIEWER, wcMenuString );
297         CheckMenuItem( hSubMenu, 0, MF_BYPOSITION | MF_CHECKED );
298     }
299 }
300 
RegisterClipboardViewer(BOOL bRegister)301 void RegisterClipboardViewer( BOOL bRegister )
302 {
303     if ( bRegister )
304         MTARegisterClipboardViewer( OnClipboardContentChange );
305     else // unregister
306         MTARegisterClipboardViewer( NULL );
307 
308     InvalidateRect( g_hwndMain, NULL, TRUE );
309     UpdateWindow( g_hwndMain );
310 }
311 
ShowAvailableClipboardFormats(HWND hWnd,HDC hdc,PAINTSTRUCT ps,RECT rcWnd)312 void ShowAvailableClipboardFormats( HWND hWnd, HDC hdc, PAINTSTRUCT ps, RECT rcWnd )
313 {
314     if ( !OpenClipboard( hWnd ) )
315     {
316         WCHAR szErrMsg[] = { L"Couldn't open the clipboard" };
317         DrawTextW( hdc, szErrMsg, wcslen( szErrMsg ), &rcWnd, DT_CENTER );
318         return;
319     }
320     else
321     {
322         WCHAR szCBChangedMsg[100];
323 
324         wsprintfW( szCBChangedMsg, L"Clipboard content changed %d", g_nCBChanges );
325         DrawTextW( hdc, szCBChangedMsg, wcslen( szCBChangedMsg ), &rcWnd, DT_CENTER );
326 
327         CloseClipboard( );
328     }
329 }
330 
ClearClipboardContent(HWND hWnd)331 void ClearClipboardContent( HWND hWnd )
332 {
333     if ( OpenClipboard( hWnd ) )
334     {
335         EmptyClipboard( );
336         CloseClipboard( );
337     }
338 }
339 
340 // clipboard viewer callback function
OnClipboardContentChange(void)341 void CALLBACK OnClipboardContentChange( void )
342 {
343     ++g_nCBChanges;
344     InvalidateRect( g_hwndMain, NULL, TRUE );
345     UpdateWindow( g_hwndMain );
346 }
347 
348