Saturday, November 8, 2008

File Icon from File Extension

First I tried to find the icon by searching the registry. Like, for .txt files, go to ".txt" under HKEY_CLASSES_ROOT, then see what is written for "(Default)", in this case, "txtfile". Again find "txtfile" under HKEY_CLASSES_ROOT and look into the "DefaultIcon" subkey to find the filename and index of the icon for this type of files. Then extract the icon with Win32 API.

That's a lot of work and unfortunately, this approach does not work for all file extensions, because some extensions have custom IconHandlers (don't really know what that means though). Then I found a better and neater way to do it by using the SHGetFileInfo function. You just need to give the file extension and set the flags appropriately to get the job done. Don't forget to call DestroyIcon on the HICON after you are done with it.

Saturday, November 1, 2008

Preventing Horizontal Scrollbar in Browser

This is another simple solution that can be done using HTML/CSS. If you want to force the WebBrowser control to wrap the page contents so that the horizontal scrollbar never appears, you can do it by adding the style "word-wrap: break-word;" to your BODY tag.

Preventing Image Drag from Browser

If you just want to prevent users from dragging an image from your hosted WebBrowser control, there is no need to implement IDropSource or anything. You can do this by simply adding
onmousemove='return false;' to your IMG tag. I wasted a lot of time on this, but it has nothing to do with the WebBrowser control, all you need is this simple HTML/JavaScript snippet.

WebBrowser Control Customization

The WebBrowser control is a very useful control for showing rich content in your application. It can be customized to suit your needs. I was able to customize the context menu and add drag and drop feature by implementing the interfaces IOleClientSite, IDocHostUIHandler and IDropTarget. See the WebBrowser Customization tutorial on MSDN for details. Here is another very helpful link to a sample project regarding context menu customization.

WebBrowser Control Repaint Issue

I hosted the WebBrowser control by attaching to a CAxWindow in my ATL application. But I was having trouble when the control was not getting repainted properly. After spending a lot of time, I found the solution here. It seems it is a bug with the control. I tracked down the function that was resizing the control and I called the UpdateWindow API passing it the m_hWnd of the host CAxWindow. The repaint problem was solved after that.