win32sdk下的滾動條分4種情況
1.只有主窗體滾動條(響應主窗體滾動條消息)
在mainWndProc中截取WM_VSCROLL即可
2.只有控件滾動條(響應控件滾動條消息,窗口子類別化)
特別的:當用CreateWindow創建滾動條控件時,要用SetScrollInfo設置滾動條屬性,如果SetScrollRange會出想兩滾動條的情況
仍然在mainWndProc中截取WM_VSCROLL消息
3.既有主窗體滾動條又有副窗體滾動條(區分主窗體滾動條與控件滾動消息)
主窗體滾動條發來的消息lParam為0,不是主窗體滾動條發來的消息lParam不為0。
控件滾動條可以用setwindowlong與getwindowlong來區別
4.彈出對話框滾動條 ,下面是針對第四種情況的源代碼,模擬一個window調色板的功能
bsp; {
EndDialog(hDlg,TRUE);
return TRUE;
}
break;
}
return FALSE;
}
/*
* deal the message of RGB Dialog
*/
BOOL CALLBACK RGBDlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
int i=0;
char szString[10];
switch(message)
{
//init RGBDlg
case WM_INITDIALOG:
for(i=0;i<3;i++)
return(TRUE);
//deal the message from scroll
case WM_HSCROLL:
//get the message from which scroll
for(i=0;i<3;i++)
{
if(hwScroll[i]==(HWND)lParam)
{
break;
}
}
switch(LOWord(wParam))
{
//push the down button of the scroll
08000">
szClassName=lpszMainClassName;
if(! RegisterClass(&wndclass))
{
MessageBeep(0);
return
//Head.h
#pragma once
#ifndef Windows_H
#define Windows_H
#include<Windows.h>
#endif
#ifndef RES_H
#define RES_H
#include "res.h"
#endif
/*
* all of controls'' attribute
pan >*/
//Main Window
static char lpszMainClassName[] = "WndMainClass";
static char lpszMainTitle[]="Hello World!";
static HWND hMainWnd=NULL;
static HINSTANCE hMainIns=NULL;
//save rgb values
static int crRGB[3]={0};
//the HDC in memory
HDC mdc;
//the Rect of color palettetyle="COLOR: #008000">
static int rectLeft =0;
static int rectTop =0;
static int rectWidth =100;
static int rectHeight =100;
//declare two int array to save the scroll after change values and before change value
static int CurrentVal[3]={0};
static int PreVal[3]={0};
//declare the scrolls'' HWND
static HWND hwScroll[3];
#ifndef CREATECONTROL_H
#define CREATECONTROL_H
#include "mainControl.h"
#endif
//mainControl.h
/*
* create main window
*/
BOOL CreateMainWindow(int nCmdShow)
{
HWND hwnd=NULL;
hwnd=CreateWindow(lpszMainClassName,
lpszMainTitle,
WS_BORDER|WS_VISIBLE|WS_SYSMENU,
CW_USEDEFAULT,CW_USEDEFAULT,
650,600,
NULL,
NULL,
hMainIns,
NULL);
pan >if(!hwnd)
{
return FALSE;
}
hMainWnd = hwnd;
return TRUE;
}
//res.h
//"選項" MENU ID
#define IDM_RGB 101
#define IDM_EXIT 102
//"幫助" MENU ID
#define IDM_ABOUT 103
//Control
#define IDC_STATIC -1
#define IDC_RED 104
#define IDC_GREEN 105
#define IDC_BLUE 106="COLOR: #000000">
#define IDC_VALRED 107
#define IDC_VALGREEN 108
#define IDC_VALBLUE 109
#define IDC_DAVID 110
//res.rc
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#define APSTUDIO_HIDDEN_SYMBOLS
#include "Windows.h"
#undef APSTUDIO_HIDDEN_SYMBOLS000">
#include "res.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// 中文(中華人民共和國) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)
#ifdef _WIN32
LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED
#pragma code_page(936)
#endif //_WIN32
/////////////////////////////////////////////////////////////////////////////
//
// Menu
//
SCROLLBARMENU MENU
BEGIN
POPUP "選項"
BEGIN
MENUITEM "顏色設置", IDM_RGB
MENUITEM SEPARATOR
MENUITEM "退出", IDM_EXIT
END
POPUP "幫助"
BEGIN
MENUITEM "關於", IDM_ABOUT
END
END
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//
ABOUTBOX DIALOG 20, 25, 124, 77
STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION |
WS_SYSMENU
CAPTION "關於使用滾動條編程的示范"
FONT 14, "隸書"
BEGIN
PUSHBUTTON "確定",IDOK,43,51,40,14
CTEXT "本程序用來說明滾動條的使用",IDC_STATIC,4,9,115,8
CTEXT "VC++>",IDC_STATIC,30,24,76,10,NOT WS_GROUP
CTEXT "一九九九年九月",IDC_STATIC,36,37,65,8,NOT WS_GROUP
CONTROL "",IDC_STATIC,"Static",SS_BLACKFRAME,40,34,56,1
END
RGBDLG DIALOG DISCARDABLE 10,80,132,73
STYLE DS_MODALFRAME|WS_POPUP|WS_VISIBLE|WS_CAPTION
>|WS_SYSMENU
CAPTION "顏色設置"
FONT 10,"System"
BEGIN
DEFPUSHBUTTON "確定",IDOK,18,53,36,14
PUSHBUTTON "取消",IDCANCEL,81,53,35,14
SCROLLBAR IDC_RED,36,10,69,8,WS_TABSTOP
SCROLLBAR IDC_GREEN,36,25,69,8,WS_TABSTOP
SCROLLBAR IDC_BLUE,36,39,69,8,WS_TABSTOP
LTEXT "紅",IDC_STATIC,14,10,15,10
LTEXT "綠",IDC_STATIC,14,24,15,10
LTEXT "藍",IDC_STATIC,14,38,15,10
LTEXT "0",IDC_VALRED,107,9,23,10
LTEXT "0",IDC_VALGREEN,107,24,23,10
LTEXT "0",IDC_VALBLUE,107,39,23,10
END
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
an>"resource.h