As we know the word COM does not provide enough interface for us to customize the toolbar interface, we can only create four type of control: button, edit box, menu (pop button), combo box, but those interfaces can not meet all out require (we need to draw a icon and a edit box with google logo), So we have to develop a way to implement a self customize UI.
I try to study into the inner part of word toolbar UI and summary the behavior of it.
A. when we create a toolbar in the word, it is not simplely a control which the operation system defined. When we open many word document, in different word windows, in fact, in each windows there is one unique control (each control has a unique windows handle), so if we want to customize the UI, we should consider all the control in different word windows .
B. when we create or switch between word windows, the word application may rearrange the toolbar, that means a toolbar that is our google translate toolbar may change into other toolbar which has save or font icon, and other toolbar may change into our google translate toolbar, but no matter how they change, there is always one google toolbar in a word windows
C. the child control in toolbar may be destroy and recreate by word application.
When we switch the word windows or some unknown action, the childe control in toolbar will be destroy and sent a WM_PARENTNOTIFY message to toolbar window, the word application will recreate controls which we use word api to create, but for our customized control, the word do nothing after destroy it, so we should recreate it ourself
D. when close word window the toolbar in that window may be destroy the toolbar window
E. we can drag the tool bar from the top of word windows and the toolbar can be float or dock on the top, left ,right ,bottom of the word window, when we drag in fact, the toolbar are different window (have different hwnds)
F. the button and pop menu in toolbar in fact are not windows (has not hwnd), it is draw by gdi in toolbar window, and for the edit box and combo box, word application creates it only when it need to interactive (it creates the first time that mouse hover over it, before it is just draw by gdi)
My solution
To solve the customized UI of toolbar, we find the google translate toolbar windows and replace its default windows process function (subclass), and draw and create it in the new windows process function. But we should concert the above behave of toolbar which are mentioned above
A. create a timer to find toolbar
Because the toolbar is not fix to one certain window, so we try to search the toolbar using a timer to find a toolbar window handler every 100ms, if we find the toolbar windows we subclass it. (see the ToolBarControlManager::SeekWordWindows function for seek and ToolBarControl class for control subclass action)
B. compare the toolbar name
We set the toolbar name “GoogleTranslateToolBar” when init and this is out search’s condition
C. process the WM_PARENTNOTIFY message
If toolbar window receive WM_PARENTNOTIFY and check this message is caused by child control destroy we delete the toolbar and wait for the timer to recreate it. Please see ToolBarControl:: OnParentNotify.
D. process the WM_DESTORY message
same as above please ToolBarControl:: OnDestory
E. mouse move problem process the WM_MOUSEMOVE message
Because the toolbar may need wm_mousemove message to draw the high light or something else of the toolbar control, so if we create
F. imit the toolbar
we limit the toolbar from dragging around word window, (see the toolbar behave F)
the code is:
sp_cmd_bar->Protection =
(Office::MsoBarProtection)(Office::msoBarNoChangeDock |
Office::msoBarNoChangeVisible |
Office::msoBarNoCustomize |
Office::msoBarNoResize);
如果看不懂,的确需要可以留言问我