SDK Implementation Separator Bar

Source: Internet
Author: User
Tags reset

The separator bar in Windows is a widely used control that is used by most explorer-style applications. However, there is very little information about the full implementation of its introduction, so I realized one, I hope to the SDK enthusiasts help.

In fact, the separator bar is also a very common window, it also has its own window class, its own window process-just like all predefined controls. That is, to create a separator bar, you also need to register the window class and create the window. The following example code demonstrates how to register a window class for a separator bar:

 WNDCLASS wc;
  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW; // 1
  wc.hCursor = LoadCursor(NULL, IDC_SIZEWE); // 2
  wc.hIcon = NULL;
  wc.hInstance = hInstance;
  wc.lpfnWndProc = (WNDPROC)ProcSplitter; // 3
  wc.lpszClassName = "MySplitter"; // 4
  wc.lpszMenuName = NULL;
  wc.style = 0;
  RegisterClass(&wc);

This code believes that you are already familiar with, so here I only briefly explain four points: 1, the background color of the separator bar, where I take the default dialog box background color, 2, the splitter bar of the mouse pointer, here I take the horizontal resize pointer; 3, this is the window of the separator bar process, all the secrets are in this callback function; 4, The window class name of the separator bar, you can take whatever name you like.

After you successfully register the window class, you can create a separator bar. Here's my sample interface, which consists of a tree view, a separator bar, a list view, and a status bar, all of which are based on this interface.

Before writing the window procedure for a separator bar, I first handle the WM_SIZE message of the dialog box as a warm-up for the separator window process. The code is as follows (you'll find that I didn't make any representations to Htree, Hstatus, Hsplitter, and hlist in the entire code, because for this simple example, I declare all of these things as global variables):

Case wm_size:
{
hdwp hdwp;
RECT RECT, Rectstatus, Recttree;
hdwp = Begindeferwindowpos (4);
GetClientRect (hdlg, &rect);
GetClientRect (Hstatus, &rectstatus);
GetWindowRect (Htree, &recttree);
DeferWindowPos (hdwp, Hstatus, NULL, 0, Rect.bottom-rectstatus.bottom, Rect.right, Rectstatus.bottom, SWP_NOZORDER );
DeferWindowPos (hdwp, Htree, NULL, 0, 0, Recttree.right-recttree.left, Rect.bottom-rectstatus.bottom, SWP_NOMOVE | Swp_nozorder);
DeferWindowPos (hdwp, Hsplitter, NULL, Recttree.right-recttree.left, 0, 2, Rect.bottom-rectstatus.bottom, SWP_NOZ order);
DeferWindowPos (hdwp, Hlist, NULL, Recttree.right-recttree.left + 2, 0, Rect.right-recttree.right + rectTree.left -2, Rect.bottom-rectstatus.bottom, Swp_nozorder);
Enddeferwindowpos (HDWP);
}
break;

You must have noticed that most of the code is in the game with the rectangle. This is true, because the process of resizing a window is a process that changes the position and size of each child window. The process of language description is: 1, first, the status bar placed in the bottom of the dialog box; 2, the second step, do not change the position and width of the tree view, reset its height, 3, do not change the position and width of the separator bar, reset its height, 4, make the list view full of the remaining customer area.

If you understand the code above, the window of the splitter bar will not be any more difficult:

LRESULT CALLBACK Procsplitter (HWND hwnd, UINT MSG, WPARAM WPARAM, LPARAM LPARAM)
{
Switch (MSG)
{
Case WM_LBUTTONDOWN:
SetCapture (HWND);
Break
Case WM_LBUTTONUP:
ReleaseCapture ();
Break
Case WM_MOUSEMOVE:
{
if ((WParam & Mk_lbutton) = = Mk_lbutton && getcapture () = hwnd)
{
HDWP hdwp;
RECT RECT, Rectstatus, Recttree;
HDWP = Begindeferwindowpos (3);
GetClientRect (GetParent (HWND), &rect);
GetClientRect (Hstatus, &rectstatus);
GetWindowRect (Htree, &recttree);
DeferWindowPos (HDWP, Htree, NULL, 0, 0, Recttree.right-recttree.left + (short) loword (LParam), rect.bottom-rectstatus.b Ottom, Swp_nomove | Swp_nozorder);
DeferWindowPos (HDWP, Hsplitter, NULL, Recttree.right-recttree.left + (short) loword (LParam), 0, 0, 0, swp_nosize | Swp_nozorder);
DeferWindowPos (HDWP, Hlist, NULL, Recttree.right-recttree.left + (short) loword (LParam) + 2, 0, Rect.right-recttree.rig HT + Recttree.left-(short) LoWord (LParam)-2, Rect.bottom-rectstatus.bottom, Swp_nozorder);
Enddeferwindowpos (HDWP);
}
}
Break
Default
Return DefWindowProc (hwnd, MSG, WParam, LParam);
}
return 0;
}

SetCapture and ReleaseCapture are the keys to capture and release the mouse when the left mouse button is pressed and released, which is the general requirement of the separator bar. The core of this code is to handle mouse movement events, that is, when the left mouse button is pressed and the splitter bar captures the mouse to change the position and width of the three related windows. The concrete rectangle operation is similar to the code principle of the main window wm_size, I will not say more. I don't use functions like MoveWindow to resize because they cause a form to be redrawn multiple times, causing the entire form to blink--and I don't want the status bar to blink.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.