Arbitrary extension of the VC ++ dialog box

Source: Internet
Author: User

When entering information, we may have a large amount of information, which is not necessary. In this case, we need to provide an interface for Information Input personnel to choose from. For example, there is a problem in the employee information Entry System of a human affairs department.
The name, gender, age, political face, title, education, department, and contact number must be entered, however, marital status, graduation School, place of origin, and health status are non-lost information and do not require entry in most cases. How to provide a convenient interface for information entry personnel, next we will provide a method that I think is better.

Step 1: Create a dialog-based project in the VC programming environment. The project name is expanddlg, and all options take the default value.

Step 2: create the "all" dialog box. There must be two controls: one is the picture control and the other is the button. The ID values are idc_divider and idc_more. Other controls can be laid out at will. The final result is that the idc_divider control divides the dialog box into two parts. The lower part can be dynamically displayed or not displayed based on your interests. The dialog box is shown as follows:

Step 3: generate the message ing function onmore of the button idc_more. In expanddlgdlg. H, define two functions as follows:

Public:
Void enablevisiblechildren ();
Void expanddialog (INT nresourceid, bool bexpand );

Step 4: Define the function implementation code in expanddlgdlg. cpp as follows:

Void cexpanddlgdlg: expanddialog (INT nresourceid, bool bexpand)
{
// The dialog box is divided into the upper and lower parts by nresourceid. If the value of bexpand is true
// The dialog box is displayed completely. Otherwise, the upper part of the dialog box is displayed.
Static crect rclarge;
Static crect rcsmall;
Cstring sexpand;
// At the beginning, only the upper part of the dialog box is displayed.
If (rclarge. isrectnull ())
{
Crect rclandmark;
Cwnd * pwndlandmark = getdlgitem (nresourceid );
Assert (pwndlandmark );
Getwindowrect (rclarge );
Pwndlandmark-> getwindowrect (rclandmark );
Rcsmall = rclarge;
Rcsmall. Bottom = rclandmark. Top;
}
If (bexpand)
{
// Expand the dialog box to the maximum size
Setwindowpos (null, 0, 0, rclarge. Width (), rclarge. Height (),
Swp_nomove | swp_nozorder );
Sexpand = "<& less ";
Enablevisiblechildren ();
}
Else
{
// Only display the upper part of the dialog box
Setwindowpos (null, 0, 0, rcsmall. Width (), rcsmall. Height (),
Swp_nomove | swp_nozorder );
Sexpand = "& more> ";
Enablevisiblechildren ();
}
Setdlgitemtext (idc_more, sexpand );
}

Void cexpanddlgdlg: enablevisiblechildren ()
{
// Remove the functions and shortcut keys of the undisplayed dialog box.
// Obtain the first window
Cwnd * pwndctl = getwindow (gw_child );
Crect rctest;
Crect rccontrol;
Crect rcshow;
// Obtain the complete rectangle of the dialog box.
Getwindowrect (rcshow );
While (pwndctl! = NULL)
{// Obtain the rectangle size of the currently displayed dialog box
Pwndctl-> getwindowrect (rccontrol );

If (rctest. intersectrect (rcshow, rccontrol ))
Pwndctl-> enablewindow (true );
Else
Pwndctl-> enablewindow (false );
// Obtain the second rectangle.
Pwndctl = pwndctl-> getwindow (gw_hwndnext );
}
}

Void cexpanddlgdlg: onmore ()
{
Static bool bexpand = true;
Expanddialog (idc_divider, bexpand );
Bexpand =! Bexpand;
}

Follow the steps above to generate our executable file and run it. Click [more] On the dialog box to find the dialog box extension. Click [less] And then we find that the dialog box shrinks, I hope it will bring you convenience.
 

Use VC to create an irregular window
 
Yixian Time and Space

After carefully checking the Win32 API, we found that creating a window of any shape is actually very simple. The simple steps in VC are as follows:

After we register and create a window class, we will process the following in the wm_create message:

(1) create an area and use createpolyonrgn. This function creates a polygon area. (You can also use other methods such as createrectrgn to create a rectangular area.) This function returns a hrgn handle;

(2) Call the setjavaswrgn function to set the window shape.

In addition, we can create multiple regions and then combine them into one region using the combinergn method. In this way, we can create a more comprehensive window.
 

Implement IE-style interface through VC Programming
 
Liu Tao · yesky

All the friends who have used IE browser know the flat toolbar and address bar on the IE interface. The buttons on the flat toolbar are flat while the buttons are grayed out, when you place the mouse over a button, the button appears (this state is called the handle), and the image on it becomes bright and eye-catching. Some buttons also contain Chinese characters or drop-down buttons marked with a small black triangle, when you click, the drop-down menu is displayed. How can these technologies be implemented? This article describes how to implement them using VC programming.

The implementation of the IE style is mainly implemented in the cmainframe: oncreate () of the main framework class. Its main idea is as follows: first, define a crebar object to serve as a tool bar and address bar container, then, the IMG object of the image list is declared to store the hotspot image of the buttons on the toolbar and the image displayed normally. To display the flat toolbar, use the createex () function to create the ctoolbar object m_wndtoolbar. Use the modifystyle () function to set the style of the toolbar to flat. You cannot use the ctoolbar: Create () or ctoolbar: setbarstyle () to set this new style. The ctoolbar class does not support tbstyle_flat. To solve this problem, you must bypass the ctoolbar class and use cwnd: modifystyle (). If the style of the setbuttoninfo () Setting button is tbstyle_dropdown, you can set the toolbar button to include a drop-down button. As for the buttons with Chinese prompts, you can use setbuttontext () in the toolbar to easily implement them. The following is some code and comments for implementing the IE style interface:

Int cmainframe: oncreate (maid)
{
Crebar m_wndrebar; // declare the crebar object
Cimagelist IMG; // declare an image list object
Cstring STR;
If (cframewnd: oncreate (lpcreatestruct) =-1)
Return-1;
If (! M_wndrebar.create (this) // create a crebar object
{
Trace0 ("failed to create Rebar/N ");
Return-1; // fail to create
}
If (! M_wndtoolbar.createex (this) // create a toolbar object
{
Trace0 ("failed to create toolbar/N ");
Return-1; // fail to create
}
// Set up toolbar Properties
M_wndtoolbar.gettoolbarctrl (). setbuttonwidth (50,150 );
File: // set the maximum and minimum sizes of buttons on the toolbar
M_wndtoolbar.gettoolbarctrl (). setextendedstyle (tbstyle_ex_drawddarrows );
File: // The toolbar can contain a drop-down button.
IMG. Create (idb_hottoolbar, 22, 0, RGB (255, 0,255 ));
File: // load hotspot image resources to the image list. idb_hottoolbar is the hotspot image resource ID.
M_wndtoolbar.gettoolbarctrl (). sethotimagelist (& IMG); // tool bar to load hotspot Images
IMG. Detach ();
IMG. Create (idb_coldtoolbar, 22, 0, RGB (255, 0,255 ));
File: // The image list is loaded with normal image resources. The idb_coldtoolbar is the image resource ID.
M_wndtoolbar.gettoolbarctrl (). setimagelist (& IMG); // load the image to the toolbar
IMG. Detach ();
M_wndtoolbar.modifystyle (0, tbstyle_flat | tbstyle_transparent );
File: // tool bar is flat
M_wndtoolbar.setbuttons (null, 9); // There are nine buttons on the toolbar
// Set up each toolbar button
File: // set the style and button Chinese prompts for the nine buttons respectively.
M_wndtoolbar.setbuttoninfo (0, id_button0, tbstyle_button, 0 );
Str. loadstring (IDs _ button0 );
M_wndtoolbar.setbuttontext (0, STR );
M_wndtoolbar.setbuttoninfo (1, id_button1, tbstyle_button, 1 );
Str. loadstring (IDs _ button1 );
M_wndtoolbar.setbuttontext (1, STR );
M_wndtoolbar.setbuttoninfo (2, id_button2, tbstyle_button, 2 );
Str. loadstring (IDs _ button2 );
M_wndtoolbar.setbuttontext (2, STR );
M_wndtoolbar.setbuttoninfo (3, id_button3, tbstyle_button, 3 );
Str. loadstring (IDs _ button3 );
M_wndtoolbar.setbuttontext (3, STR );
M_wndtoolbar.setbuttoninfo (4, id_button4, tbstyle_button, 4 );
Str. loadstring (IDs _ button4 );
M_wndtoolbar.setbuttontext (4, STR );
M_wndtoolbar.setbuttoninfo (5, id_button5, tbstyle_button, 5 );
Str. loadstring (IDs _ button5 );
M_wndtoolbar.setbuttontext (5, STR );
M_wndtoolbar.setbuttoninfo (6, id_button6, tbstyle_button | tbstyle_dropdown, 6 );
Str. loadstring (IDs _ button6 );
M_wndtoolbar.setbuttontext (6, STR );
M_wndtoolbar.setbuttoninfo (7, id_button7, tbstyle_button, 7 );
Str. loadstring (IDs _ button7 );
M_wndtoolbar.setbuttontext (7, STR );
M_wndtoolbar.setbuttoninfo (8, id_button8, tbstyle_button | tbstyle_dropdown, 8 );
Str. loadstring (IDs _ button8 );
M_wndtoolbar.setbuttontext (8, STR );
File: // resize the button
Crect recttoolbar;
M_wndtoolbar.getitemrect (0, & recttoolbar); // obtain the size of the first button in the toolbar.
M_wndtoolbar.setsizes (recttoolbar. Size (), csize (30,20 ));
File: // The first parameter is the button size, and the second parameter is the image size.
File: // create a combo box as the address bar
If (! M_wndaddress.create (cbs_dropdown | ws_child, crect (0, 0,200,120), this, afx_idw_toolbar + 1 ))
{
Trace0 ("failed to create ComboBox/N ");
Return-1; // fail to create
}
File: // Add the toolbar and address bar
M_wndrebar.addbar (& m_wndtoolbar );
Str. loadstring (ids_address );
M_wndrebar.addbar (& m_wndaddress, STR, null, rbbs_fixedbmp | rbbs_break );
File: // defines the rebarbandinfo object and sets the ideal size for the toolbar and address bar.
Rebarbandinfo rbbi;
Rbbi. cbsize = sizeof (rbbi );
Rbbi. fmask = rbbim_childsize | rbbim_idealsize | rbbim_size;
Rbbi. cxminchild = recttoolbar. Width ();
Rbbi. cyminchild = recttoolbar. Height ();
Rbbi. Cx = rbbi. cxideal = recttoolbar. Width () * 9;
M_wndrebar.getrebarctrl (). setbandinfo (0, & rbbi); // set the toolbar size
Rbbi. cxminchild = 0;
Crect rectaddress;
Rbbi. fmask = rbbim_childsize | rbbim_idealsize;
M_wndaddress.geteditctrl ()-> getwindowrect (& rectaddress );
Rbbi. cyminchild = rectaddress. Height () + 10;
Rbbi. cxideal = 200;
M_wndrebar.getrebarctrl (). setbandinfo (2, & rbbi); // you can specify the size of the address bar.
M_wndtoolbar.setbarstyle (m_wndtoolbar.getbarstyle () |
Cbrs_tooltips | cbrs_flyby | cbrs_size_fixed );
If (! M_wndstatusbar.create (this) |
! M_wndstatusbar.setindicators (indicators,
Sizeof (indicators)/sizeof (uint )))
{
Trace0 ("failed to create status bar/N ");
Return-1; // fail to create
}
Return 0;
}

The above code is compiled in the windows and Visual C ++ environments. The program runs normally. If you are interested, you can experiment with it yourself.
 

Another method for VC to limit the window size
 
Yixian Time and Space

Generally, the methods we see are interception of wm_getmaxmininfo messages.

I have another experience to achieve.

Generally, the window size is changed because you drag the window border. Therefore, we can intercept the message wm_nchittest in the main window and determine in its response function whether the return value of cwnd: onnchittest () is htright, htleft, httop, or htbottom. If yes, it indicates that the user has already clicked one of the four borders. At this time, we should return htclient. then, the shape of the mouse will not change to a horizontal or vertical bidirectional arrow, and you will not be able to change the window size by dragging the border.

In addition, a small vulnerability should be added to remove SC _size from the system menu.
 

Copyright window before the main program
CPCW

Winapi winmain (hinstance, hinstance, lpstr, INT)
{
DWORD ltime;
Try
{
Application-> initialize ();
Aboutbox = new taboutbox (aboutbox );
Aboutbox-> borderstyle = bsnone;
Aboutbox-> okbutton-> visible = false;
Aboutbox-> Height = 185;
Aboutbox-> show ();
Aboutbox-> Update ();
Ltime = gettickcount ();
Application-> createform (_ classid (tmainform), & mainform );
While (gettickcount ()-ltime)/1000 <3 );
Aboutbox-> hide ();
Aboutbox-> free ();
Application-> Run ();
}
Catch (exception & exception)
{
Application-> showexception (& exception );
}
Return 0;
}

 

Visual c ++ 6.0 add bitmap in the MDI main frame window
 
Liu Tao yesky

I want to add a color bitmap to the MDI program to beautify the interface during the development project. I have also tried several methods, but there are some minor problems, I have finally successfully implemented this function. I will introduce my implementation methods to you.

First, it is clear that the main framework window of an MDI application contains a special subwindow called the mdiclient window. The main framework class of the application contains a member variable m_hwndmdiclient, which refers to the mdiclient window. The mdiclient window is used to manage the customer area of the main framework window, which is difficult to program the MDI customer window. The reason is that the customer zone of mdiframewnd is completely overwritten by the mdiclient window. In this way, the background color and the optical mark of the mdiframewnd in the MDI main window do not work. At the same time, Microsoft does not support using the mdiclient window as a subclass. The mdiclient window can only use the standard background color and cursor. Therefore, the message processing function of wm_paint cannot be simply reloaded for the MDI client window as for the normal window. We can intercept the duplicated message about the mdiclient window in the main framework window, and then add our own code. I use pretranslatemessage (MSG * PMSG) to intercept the message wm_paint in the MDI customer window. In this function, the wm_paint message is sent to the main frame window, and the color bitmap is displayed in the message processing function. My specific implementation is as follows: 1. Add a 256-color bitmap resource to the program and name it idb_bitmap1; 2. Use classwizard to add the function cmainframe: pretranslatemessage (MSG * PMSG) to the main framework class ); 3. Use classwizard to add the function cmainframe: onpaint () to the main framework class. Two functions are provided:

Bool cmainframe: pretranslatemessage (MSG * PMSG)
{
// Todo: add your specialized code here and/or call the base class
If (PMSG-> hwnd = m_hwndmdiclient & PMSG-> message = wm_paint)
Postmessage (wm_paint );
Return cmdiframewnd: pretranslatemessage (PMSG );
}

Void cmainframe: onpaint ()
{
Cdc dc, memdc;
DC. m_hdc =: getdc (this-> m_hwndmdiclient );
Crect rect;
Cbitmap bitmap;
Bitmap szbitmap;
Bitmap. loadbitmap (idb_bitmap1 );
Bitmap. GetObject (sizeof (Bitmap), & szbitmap );
Csize size (szbitmap. bmwidth, szbitmap. bmheight );
Memdc. createcompatibledc (& DC );
Cbitmap * oldbitmap = memdc. SelectObject (& Bitmap );
Getclientrect (& rect );
Stretchblt (DC. m_hdc, 0, 0, rect. Width (), rect. Height (),
Memdc. m_hdc, 0, 0, size. CX, size. Cy, srccopy );
Memdc. SelectObject (oldbitmap );
Memdc. deletedc ();
DC. deletedc ();
Cmdiframewnd: onpaint ();
}

The above steps can be used to display the color bitmap in the MDI program. In my example, we use the 256 color bitmap. You can also display the true color bitmap, I will not talk much about the specific method. If you are interested, you can give it a try.

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.