Best winapi questions in October 2010

Windows Visual Themes: Gallery of Parts and States?

13 votes

Microsoft Windows lets programmers draw GUI elements using the look and feel of the current theme using functions like DrawThemeBackground and DrawThemeText. The elements are specified by Class, Part, and State, as described at the Parts and States page at MSDN.

Unfortunately, the page is not very informative (at all!). So the question is: is there somewhere a reference of all these parts and states, preferably with images of the elements (in the default Windows Vista/7 theme)?

I have created a small Windows application, programmed with the table at Parts and States. This application lets the programmer browse and explore all parts and states, using the current OS theme.

Parts and States Explorer
(High-Res)

It can be downloaded at

The (Delphi, Win32 API) source, which is too long to be posted here (due to hundreds of constants) is found at

Windows Search - IFilter search term highlighting

7 votes

My development team are having a problem having snippets of text shown for search results in windows 7 for our own custom files (note we are NOT talking about the preview pane that uses the IPreviewHandler interface). An example of what I mean for .txt files is shown below:

Search example

The text snippet shown here with the highlighted result is not shown for our own files, only the name of the file is.

We have implemented an IFilter for our files and this is indexing the file contents correctly (searching returns the correct results). It just wont show the snippet and highlight the search term.

We suspected it might be something to do with the cwcStartSource, and cwcLenSource values for the STAT_CHUNK in IFilter, but setting these to different values has so far yielded nothing.

Has anyone else had a similar experience or have any thoughts?

Thanks in advance.

You need to generate a Preview Handler for your file type.

It can also render enhanced previews of items in a Preview Pane without launching the default application, if the application has registered a Preview Handler. This can provide functionality such as file type-specific navigation (such a browsing a presentation using next/previous controls, or seeking inside a media file).[29] Preview handlers can also allow certain kind of selections (such as highlighting a text snippet) to be performed from the preview pane itself.

source

There's a bunch of code examples floating around for PDFs and such.

Take a peek here: CodePlex:Windows Preview Handler Pack

Edit:

To clarify, the search results pane in Windows 7 is a preview handler host. It has two main pieces: the Content View and the Preview Pane.

Windows Explorer also includes a view mode called the Content view, which is the default view when viewing search results. The Content view shows the name, location, some of the metadata tags associated with the file, a thumbnail image of the file, and a snippet of the file. This view mode also uses hit highlighting to show where the query term appears in the files, which makes it easy to understand why that file was returned in the search results list.

source

You need a Preview Handler for any custom file types if you want enhanced functionality like viewing the file contents for your custom file type.

In Windows Vista and later, Windows Search is integrated into all Windows Explorer windows for instant access to search.

...

Preview handlers and thumbnail handlers enable users to preview documents in Windows Explorer without having to open the application that created them.

source

To verify that you have implemented your custom file type correctly, you can try the File Type Verifier from the Windows 7 SDK, which will test the following things assosicated with your custom file:

  • Preview Handlers
  • Thumbnail Handlers
  • Property Handlers
  • Verb Handlers
  • Filters (IFilter)
  • Kind Associations
  • Perceived Types
  • Important Properties

Using the Content View

you can take advantage of the Content view by using either of two different approaches. You can use an existing set of properties and layout pattern, or you can create your own combination. These two approaches are described in detail:

For a general overview on the Content View, see this article:

Content View By File Type or Kind

How can I force display detection in Windows?

6 votes

I often boot my Windows 7 PC with the attached KVM switch focused on another computer. When I switch to the booted PC, the display resolution is wrong (and the second attached monitor is not detected).

I can correct this by right-clicking the desktop, choosing Screen Resolution and clicking Detect. This makes Windows detect attached displays and adjust to the most optimal resolution.

I would like to write a small utility to do this automatically. Which Win32 API call or C# object should I use?

You can try:

  1. You can use Spy++ to search for the windows that are open and take a look at their properties and messages.
  2. Use process to start "rundll32.exe shell32.dll,Control_RunDLL desk.cpl" or experiment with calling it directly to see if you can get a window handle, check below link for ideas.
  3. Use the code "send button click to external app" and modify it to search for a window with caption "Screen Resolution" and send a BN_CLICK to the childwindow with the caption "Detect".
  4. Since the computer is already on you might want to fire it up automatically on logon, for that use the task scheduler.

How to programmatically create a shortcut using Win32

6 votes

I need to programmatically create a shortcut using C++.

How can I do this using Win32 SDK?

What API function can be used for this purpose?

Try Windows Shell Links. This page also contains a C++ example. Descriptive Snippet:

Using Shell Links

This section contains examples that demonstrate how to create and resolve shortcuts from within a Win32-based application. This section assumes you are familiar with Win32, C++, and OLE COM programming.