質問

I am writing a simple GUI application in Visual C++/Windows API. I have a Trackbar control on the dialogbox defined in resources as:

CONTROL "",IDC_SLIDER1045,"msctls_trackbar32",0x50010000,23,52,141,16,0x00000000

I want to show the trackbar value on static text control, so I wrote:

case WM_NOTIFY:
if(lParam == TRBN_THUMBPOSCHANGING)
{
    Pos1 = SendMessage(GetDlgItem(hwndDlg, 1045), TBM_GETPOS, 0, 0);

    wsprintf(szPos1, "Change IP address every %d minutes", Pos1);

    SetDlgItemText(hwndDlg, 1044, szPos1);
}
break;

I tried also:

case WM_NOTIFY:
    Pos1 = SendMessage(GetDlgItem(hwndDlg, 1045), TBM_GETPOS, 0, 0);

    wsprintf(szPos1, "Change IP address every %d minutes", Pos1);

    SetDlgItemText(hwndDlg, 1044, szPos1);
break;

Both codes doesn't work. First gives no action, second hangs the application.

My question is: How to get Trackbar value and show it on static text control in real time?

役に立ちましたか?

解決

Be sure to read the SDK documentation for Trackbar. The section titled "Trackbar Notification Messages" tells you how the control tells you about the position.

Note how it documents that you should listen for the WM_HSCROLL or WM_VSCROLL message.

他のヒント

What are 1045 and 1044 in your code? Possibly you mean IDC_SLIDER1045 and static control resource ID. If necessary, include resource.h to the source file.

As other answers have pointed out, Trackbar controls send the traditional WM_HSCROLL and WM_VSCROLL notification messages, provided the appropriate control style is set. However, these notifications only support a 16-bit range. Since Windows Vista, the API includes the new WM_NOTIFY-based notification TRBN_THUMBPOSCHANGING, which sends 32-bit position data in the accompanying structure NMTRBTHUMBPOSCHANGING.

I have solved my question.

For others that need the solution:

From MSDN:

A trackbar notifies its parent window of user actions by sending the parent a WM_HSCROLL or WM_VSCROLL message. A trackbar with the TBS_HORZ style sends WM_HSCROLL messages. A trackbar with the TBS_VERT style sends WM_VSCROLL messages.

Code:

case WM_HSCROLL:
    Pos1 = SendMessage(GetDlgItem(hwndDlg, IDC_SLIDER1045), TBM_GETPOS, 0, 0);

    wsprintf(szPos1, "Change IP address every %d minutes", Pos1);

    SetDlgItemText(hwndDlg, IDC_CHECKBOX1044, szPos1);
break;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top