The background color of trackbar_class (trackbar controls) "real-time" change is not solved by the wm_erasebackground message.

Source: Internet
Author: User

// Dialogtew.fcdlg. cpp: implementation file
//

# Include "stdafx. H"
# Include "dialogtew.fc. H"
# Include "dialogtew.fcdlg. H"

# Ifdef _ debug
# Define new debug_new
# Endif

Logbrush;
Cbrush brush;

// Caboutdlg Dialog used for app about

Class caboutdlg: Public cdialog
{
Public:
Caboutdlg ();

// Dialog data
Enum {IDD = idd_aboutbox };

Protected:
Virtual void dodataexchange (cdataexchange * PDX); // DDX/DDV support

// Implementation
Protected:
Declare_message_map ()
};

Caboutdlg: caboutdlg (): cdialog (caboutdlg: IDD)
{
}

Void caboutdlg: dodataexchange (cdataexchange * PDX)
{
Cdialog: dodataexchange (PDX );
}

Begin_message_map (caboutdlg, cdialog)
End_message_map ()

// Cdialogtew.fcdlg Dialog

 

Cdialogtew.fcdlg: cdialogtew.fcdlg (cwnd * pparent/* = NULL */)
: Cdialog (cdialogtew.fcdlg: IDD, pparent)
{
M_hicon = afxgetapp ()-> loadicon (idr_mainframe );
}

Void cdialogtew.fcdlg: dodataexchange (cdataexchange * PDX)
{
Cdialog: dodataexchange (PDX );
}

Begin_message_map (cdialogtew.fcdlg, cdialog)
On_wm_syscommand ()
On_wm_paint ()
On_wm_querydragicon ()
//} Afx_msg_map
On_bn_clicked (idcancel, & cdialogtew.fcdlg: onbnclickedcancel)
On_bn_clicked (idc_change, & cdialogtew.fcdlg: onbnclickedchange)
On_wm_erasebkgnd ()
On_wm_ctlcolor ()
End_message_map ()

// Cdialogtew.fcdlg message handlers

Bool cdialogtew.fcdlg: oninitdialog ()
{
Cdialog: oninitdialog ();

// Add "about..." menu item to system menu.

// Idm_aboutbox must be in the system command range.
Assert (idm_aboutbox & 0xfff0) = idm_aboutbox );
Assert (idm_aboutbox <0xf000 );

Cmenu * psysmenu = getsystemmenu (false );
If (psysmenu! = NULL)
{
Cstring straboutmenu;
Straboutmenu. loadstring (ids_aboutbox );
If (! Straboutmenu. isempty ())
{
Psysmenu-> appendmenu (mf_separator );
Psysmenu-> appendmenu (mf_string, idm_aboutbox, straboutmenu );
}
}

// Set the icon for this dialog. The framework does this automatically
// When the application's main window is not a dialog
Seticon (m_hicon, true); // set big icon
Seticon (m_hicon, false); // set small icon

// Todo: add extra initialization here
Logbrush. lbstyle = bs_solid;
Logbrush. lbhatch = hs_cross;
Logbrush. lbcolor = RGB (0,255, 0 );
//... And initialize it with the logbrush.
Brush. createbrushindirect (& logbrush );

Return true; // return true unless you set the focus to a control
}

Void cdialogte1_fcdlg: onsyscommand (uint NID, lparam)
{
If (NID & 0xfff0) = idm_aboutbox)
{
Caboutdlg dlgabout;
Dlgabout. domodal ();
}
Else
{
Cdialog: onsyscommand (NID, lparam );
}
}

// If you add a Minimize button to your dialog, you will need the code below
// To draw the icon. For MFC applications using the document/view model,
// This is automatically done for you by the framework.

Void cdialogtew.fcdlg: onpaint ()
{
If (isiconic ())
{
Cpaintdc DC (this); // device context for painting

Sendmessage (wm_iconerasebkgnd, reinterpret_cast <wparam> (DC. getsafehdc (), 0 );

// Center icon in client rectangle
Int cxicon = getsystemmetrics (sm_cxicon );
Int cyicon = getsystemmetrics (sm_cyicon );
Crect rect;
Getclientrect (& rect );
Int x = (rect. Width ()-cxicon + 1)/2;
Int y = (rect. Height ()-cyicon + 1)/2;

// Draw the icon
DC. drawicon (X, Y, m_hicon );
}
Else
{
Cdialog: onpaint ();
}
}

// The system callthis function to obtain the cursor to display while the user drags
// The minimized window.
Hcursor cdialogtew.fcdlg: onquerydragicon ()
{
Return static_cast }

Void cdialogtew.fcdlg: onbnclickedcancel ()
{
// Todo: add your control notification handler code here
Oncancel ();
}

Void cdialogtew.fcdlg: onbnclickedchange ()
{
// Todo: add your control notification handler code here
 
Logbrush. lbstyle = bs_solid;
Logbrush. lbhatch = hs_cross;
Logbrush. lbcolor = RGB (255, 0, 0 );
Brush. deleteobject ();
Brush. createbrushindirect (& logbrush );
Invalidate (true );
}

Bool cdialogtew.fcdlg: onerasebkgnd (CDC * PDC)
{
Cdialog: onerasebkgnd (PDC );
Rect RT;
Getclientrect (& RT );
PDC-> fillrect (& RT, & brush );

Return 1;
}

Hbrush cdialogtew.fcdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
PDC-> setbkmode (transparent );
HBr = (hbrush) brush. getsafehandle ();
: Outputdebugstringw (_ T ("Here are onctlcolor/N "));

Return HBr;
}
The above is my experiment code. I wanted to use onerasebkgnd and onctlcolor to solve the problem of real-time background changes in trackbar controls. However, I found that this is not so simple. The specific implementation method is still under consideration. Of course, it may also be that the implementation of common control deliberately prevents real-time background changes.

I have created an MFC program based on the dialog box, added the trackbar controls control to the dialog box, and added a button to change the background of the dialog box in real time. This dialog box contains one static control, one button, and one trackbar controls control. You can modify the cdialogtejavasfcdlg file to add a global brush and several message response functions. The result is to start the dialog box program. The background of the dialog box is implemented in onerasebkgnd, and the background of the static control and trackbar control is implemented using onctlcolor. I can debug it step by step. However, when you click the button, the background color of the static control and trackbar control will change (because I called invalidate (true); In the onbnclickedchange function );), however, only the background of the static control is changed (implemented in onctlcolor), while the trackbar control executes onctlcolor, but its background is not changed. When I click the trackbar control, the background changes immediately to the same color as the background of the static control in the dialog box. What's strange to me ?? Why do I need to click it to change the background (NOTE: When I click the trackbar Control, thumb on this control does not change its position ).

During the debugging process, I reduced the code editing page to a very small value. In order to let me see the interaction between the breakpoint and the execution program, I added breakpoints to the Message response function added to the program, the above results are obtained through step-by-step debugging.

If someone has read this article, please give me more advice.

 

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.