1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 // TestWin32.cpp : Defines the entry point for the application
37 #include <..\..\inc\systools\win32\MtaOleClipb.h>
41 #define MAX_LOADSTRING 100
44 HINSTANCE hInst
; // current instance
45 WCHAR szTitle
[MAX_LOADSTRING
]; // Text for title
46 WCHAR szWindowClass
[MAX_LOADSTRING
]; // Text for title
47 ATOM
MyRegisterClass( HINSTANCE hInstance
);
48 BOOL
InitInstance( HINSTANCE
, int );
49 LRESULT CALLBACK
WndProc( HWND
, UINT
, WPARAM
, LPARAM
);
50 LRESULT CALLBACK
About( HWND
, UINT
, WPARAM
, LPARAM
);
51 void PasteClipboardData(HWND hwndParent
);
52 void PasteClipboardData2(HWND hwndParent
);
54 LPSTREAM g_pStm
= NULL
;
55 char* pTextBuff
= NULL
;
58 //----------------------------------------------------
60 //----------------------------------------------------
62 unsigned int _stdcall
ThreadProc(LPVOID pParam
)
64 IDataObject
* pIDataObj
= NULL
;
74 OleInitialize( NULL
);
76 hr
= OleGetClipboard( &pIDataObj
);
78 hr
= CoGetInterfaceAndReleaseStream(
80 __uuidof(IDataObject
),
81 reinterpret_cast<LPVOID
*>(&pIDataObj
));
83 formatETC
.cfFormat
= CF_TEXT
;
85 formatETC
.dwAspect
= DVASPECT_CONTENT
;
86 formatETC
.lindex
= -1;
87 formatETC
.tymed
= TYMED_HGLOBAL
;
89 hr
= pIDataObj
->GetData( &formatETC
, &stgMedium
);
90 pGlobMem
= GlobalLock( stgMedium
.hGlobal
);
91 if ( NULL
!= pGlobMem
)
93 if ( NULL
!= pTextBuff
)
98 sizeGlobBuff
= GlobalSize( stgMedium
.hGlobal
);
99 pTextBuff
= (char*)malloc( sizeGlobBuff
+ 1 );
100 ZeroMemory( pTextBuff
, sizeGlobBuff
+ 1 );
102 memcpy( pTextBuff
, pGlobMem
, sizeGlobBuff
);
103 lData
= sizeGlobBuff
;
105 InvalidateRect( hwnd
, NULL
, TRUE
);
106 UpdateWindow( hwnd
);
109 GlobalUnlock( stgMedium
.hGlobal
);
111 ReleaseStgMedium( &stgMedium
);
113 pIDataObj
->Release();
122 //----------------------------------------------------
124 //----------------------------------------------------
126 int APIENTRY
WinMain(HINSTANCE hInstance
,
127 HINSTANCE hPrevInstance
,
131 // TODO: Add code here.
136 // it's important to initialize ole
137 // in order to use the clipboard
138 //hr = OleInitialize( NULL );
139 hr
= CoInitializeEx( NULL
, COINIT_MULTITHREADED
);
140 //hr = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
142 // Initialize global strings
143 LoadStringW(hInstance
, IDS_APP_TITLE
, szTitle
, MAX_LOADSTRING
);
144 LoadStringW(hInstance
, IDC_TESTWIN32
, szWindowClass
, MAX_LOADSTRING
);
145 MyRegisterClass(hInstance
);
147 // Initialization of the application to perform:
148 if( !InitInstance( hInstance
, nCmdShow
) )
153 hAccelTable
= LoadAccelerators(hInstance
, (LPCTSTR
)IDC_TESTWIN32
);
155 // Main message loop:
156 while( GetMessage(&msg
, NULL
, 0, 0) )
158 if( !TranslateAccelerator (msg
.hwnd
, hAccelTable
, &msg
) )
160 TranslateMessage( &msg
);
161 DispatchMessage( &msg
);
165 // uninitializing the ole libraries
166 //OleUninitialize( );
175 // FUNCTION: MyRegisterClass()
177 // PURPOSE: Registers the window class
180 // This function and its usage are only necessary if this code
181 // needs to be compatible with Win32 systems prior to 'RegisterClassEx'
182 // function, which was added to Windows 95. If it important to call
183 // this function to allow the use of small icons in the correct proportions.
185 ATOM
MyRegisterClass( HINSTANCE hInstance
)
189 wcex
.cbSize
= sizeof(WNDCLASSEX
);
191 wcex
.style
= CS_HREDRAW
| CS_VREDRAW
;
192 wcex
.lpfnWndProc
= (WNDPROC
)WndProc
;
195 wcex
.hInstance
= hInstance
;
196 wcex
.hIcon
= LoadIcon(hInstance
, (LPCTSTR
)IDI_TESTWIN32
);
197 wcex
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
198 wcex
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
199 wcex
.lpszMenuName
= (LPCWSTR
)IDC_TESTWIN32
;
200 wcex
.lpszClassName
= szWindowClass
;
201 wcex
.hIconSm
= LoadIcon(wcex
.hInstance
, (LPCTSTR
)IDI_SMALL
);
203 return RegisterClassExW(&wcex
);
207 // FUNCTION: InitInstance(HANDLE, int)
209 // PURPOSE: Saves instance handle and creates main window
212 // In this function, the instance access number is stored in a global variable
213 // and the main program window is displayed.
215 BOOL
InitInstance( HINSTANCE hInstance
, int nCmdShow
)
219 hInst
= hInstance
; // Store instance access number in our global variable
221 hWnd
= CreateWindowExW(0, szWindowClass
, szTitle
, WS_OVERLAPPEDWINDOW
,
222 CW_USEDEFAULT
, 0, CW_USEDEFAULT
, 0, NULL
, NULL
, hInstance
, NULL
);
229 ShowWindow( hWnd
, nCmdShow
);
230 UpdateWindow( hWnd
);
236 // FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
238 // PURPOSE: Processes messages for the main window.
240 // WM_COMMAND - Handle application menu
241 // WM_PAINT - Display main windows
242 // WM_DESTROY - Output completion message and return
245 LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
250 TCHAR szHello
[MAX_LOADSTRING
];
253 LoadString(hInst
, IDS_HELLO
, szHello
, MAX_LOADSTRING
);
258 wmId
= LOWORD(wParam
);
259 // Analyze menu selections
263 //PasteClipboardData(hWnd);
264 PasteClipboardData2(hWnd
);
268 DestroyWindow( hWnd
);
272 return DefWindowProc( hWnd
, message
, wParam
, lParam
);
277 hdc
= BeginPaint (hWnd
, &ps
);
278 // TODO: Add any code for drawing
280 GetClientRect( hWnd
, &rt
);
282 if ( NULL
!= pTextBuff
)
284 DrawText( hdc
, pTextBuff
, lData
, &rt
, DT_CENTER
);
288 DrawText( hdc
, szHello
, strlen(szHello
), &rt
, DT_CENTER
);
291 EndPaint( hWnd
, &ps
);
295 PostQuitMessage( 0 );
299 return DefWindowProc( hWnd
, message
, wParam
, lParam
);
304 void PasteClipboardData2(HWND hwndParent
)
306 IDataObject
* pIDataObject
;
314 hr
= MTAGetClipboard( &pIDataObject
);
315 if ( SUCCEEDED( hr
) )
317 formatETC
.cfFormat
= CF_TEXT
;
318 formatETC
.ptd
= NULL
;
319 formatETC
.dwAspect
= DVASPECT_CONTENT
;
320 formatETC
.lindex
= -1;
321 formatETC
.tymed
= TYMED_HGLOBAL
;
323 hr
= pIDataObject
->GetData( &formatETC
, &stgMedium
);
324 pGlobMem
= GlobalLock( stgMedium
.hGlobal
);
325 if ( NULL
!= pGlobMem
)
327 if ( NULL
!= pTextBuff
)
332 sizeGlobBuff
= GlobalSize( stgMedium
.hGlobal
);
333 pTextBuff
= (char*)malloc( sizeGlobBuff
+ 1 );
334 ZeroMemory( pTextBuff
, sizeGlobBuff
+ 1 );
336 memcpy( pTextBuff
, pGlobMem
, sizeGlobBuff
);
337 lData
= sizeGlobBuff
;
339 InvalidateRect( hwndParent
, NULL
, TRUE
);
340 UpdateWindow( hwndParent
);
343 GlobalUnlock( stgMedium
.hGlobal
);
345 ReleaseStgMedium( &stgMedium
);
347 pIDataObject
->Release();
351 //----------------------------------------------------
352 // clipboard handling
353 //----------------------------------------------------
356 void PasteClipboardData(HWND hwndParent)
358 IDataObject* pIDataObj = NULL;
362 hr = OleGetClipboard( &pIDataObj );
363 if ( SUCCEEDED( hr ) )
365 HRESULT hr = CoMarshalInterThreadInterfaceInStream(
366 __uuidof(IDataObject), //The IID of inteface to be marshaled
367 pIDataObj, //The interface pointer
368 &g_pStm //IStream pointer
371 HANDLE hThread = (HANDLE)_beginthreadex(
374 ThreadProc, //Start Address
376 (unsigned int)hwndParent, //Creation Flag
380 //Wait for the thread to finish execution
381 //A thread handle is signaled is thread execution
385 DWORD dwRet = ::MsgWaitForMultipleObjects(
386 1, //Count of objects
387 &hThread, //pointer to the array of objects
388 FALSE, //Wait for all objects?
389 INFINITE, //Wait How Long?
390 QS_ALLINPUT //Wait for all messges
393 //This means that the object is signaled
394 if ( dwRet != WAIT_OBJECT_0 + 1 )
397 //Remove the messages from the queue
400 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0)
403 TranslateMessage(&msg);
404 //Let the windowproc handle the message
405 DispatchMessage(&msg);
409 CloseHandle( hThread );
410 pIDataObj->Release();
415 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */