June 15th, 2015

How can I reposition my window so it isn’t covered by the touch keyboard?

Last week, we saw how we could rearrange our window contents to avoid having the touch keyboard cover the edit control. But what if the touch keyboard covers the entire window? No amount of rearranging will help. We’ll have to move our window.

Let’s make these changes to the Do­Layout function:

void
DoLayout(HWND hwnd, int cx, int cy, bool isKeyboardShowing = false)
{
  ...
  if (g_hwndChild) {
    ...
      if (IntersectRect(&rc, &rcEdit, &rcKeyboardClient)) {
        if (rcKeyboardClient.top > 50) {
          cyEdit = min(rcKeyboardClient.top, cy);
        } else if (isKeyboardShowing) {
          // need to reposition the entire window, ugh.
          int dyAdjust = 50 - rcKeyboardClient.top;
          RECT rcWindow;
          GetWindowRect(hwnd, &rcWindow);
          SetWindowPos(hwnd, nullptr,
            rcWindow.left, rcWindow.top - dyAdjust, 0, 0,
            SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOSIZE);
          return;
        }
      }
      ...
}

If there are at least 50 pixels of the edit control visible, then we consider that good enough. If not, and if we are responding to the keyboard showing, then we take matters into our own hands and move our window so that there are 50 pixels of the edit control visible. I didn’t bother adding a check to make sure we never moved beyond the top of the work area; I’ll leave that as an exercise for the reader, seeing as it’s more typing that tends to distract from the point of the article.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.