程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> win32sdk下滾動條的學習

win32sdk下滾動條的學習

編輯:.NET實例教程

win32sdk下的滾動條分4種情況
1.只有主窗體滾動條(響應主窗體滾動條消息)
在mainWndProc中截取WM_VSCROLL即可
2.只有控件滾動條(響應控件滾動條消息,窗口子類別化)
特別的:當用CreateWindow創建滾動條控件時,要用SetScrollInfo設置滾動條屬性,如果SetScrollRange會出想兩滾動條的情況
仍然在mainWndProc中截取WM_VSCROLL消息
3.既有主窗體滾動條又有副窗體滾動條(區分主窗體滾動條與控件滾動消息)
主窗體滾動條發來的消息lParam為0,不是主窗體滾動條發來的消息lParam不為0。
控件滾動條可以用setwindowlong與getwindowlong來區別
4.彈出對話框滾動條 ,下面是針對第四種情況的源代碼,模擬一個window調色板的功能



//Main.cpp
#include "Head.h"

/*
 * deal the message of About Dialog
 */
BOOL CALLBACK About(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
    switch(message)
    {
    //Init About Dialog
    case WM_INITDIALOG:
        return(TRUE);
    case WM_COMMAND:
        if(LOWORD(wParam)==IDOK||LOWord(wParam)==IDCANCEL)
      &n

$False$

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++)


        {
            CurrentVal[i]=crRGB[i];
            PreVal[i]=crRGB[i];
            hwScroll[i]=GetDlgItem(hDlg,(IDC_RED+i));
            SetScrollRange(hwScroll[i],SB_CTL,0,255,FALSE);                            //set scroll area
            SetScrollPos(hwScroll[i],SB_CTL,crRGB[i],TRUE);                            //set scroll position
            SetDlgItemText(hDlg,IDC_VALRED+i,itoa(CurrentVal[i],szString,10));        //set RGBDlg''s Text value
        }

        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">
        case SB_LINEDOWN:
            CurrentVal[i]++;
            if(CurrentVal[i]>255)
            {
                CurrentVal[i]=255;
            }
            break;
        //push the up button of the scroll
        case SB_LINEUP:
            CurrentVal[i]--;
            if(CurrentVal[i] < 0)
            {
                CurrentVal[i]OR: #000000">=0;
            }
            break;
        //push the right space area of the scroll block
        case SB_PAGEDOWN:
            CurrentVal[i]+=(255-CurrentVal[i])/2;
            break;
        //push the left space area of the scroll block
        case SB_PAGEUP:
            CurrentVal[i]-=(255-CurrentVal[i])/2;
            break0">;
        case SB_THUMBTRACK:
            CurrentVal[i]=HIWord(wParam);
            break;
        }
        SetScrollPos(hwScroll[i],SB_CTL,CurrentVal[i],TRUE);
        SetDlgItemText(hDlg,IDC_VALRED+i,itoa(CurrentVal[i],szString,10));
        crRGB[i]=CurrentVal[i];
        SendMessage(hMainWnd,WM_COMMAND,IDC_DAVID,0L);
        break;
    case WM_COMMAND:
        switch(wParam)
        {
        case IDOK:
            for(int i=0;i<3;in >++)
            {
                crRGB[i]=CurrentVal[i];
            }
            EndDialog(hDlg,TRUE);
            SendMessage(hMainWnd,WM_COMMAND,IDC_DAVID,0L);
            return (TRUE);
        case IDCANCEL:
            for(int i=0;i<3;i++)
            {
                crRGB[i]=PreVal[i];
            }
            EndDialog(hDlg,TRUE);
            SendMessage(hMainWnd,WM_COMMAND,IDC_DAVID,0L);
         an >return (TRUE);
        }
        break;
    }
    return FALSE;
}
/*
 * deal the message of the programm
 */
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT message
                        ;,WPARAM wParam,LPARAM IParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    HBRUSH hBrush;
    switch(message)
    {
    case WM_PAINT:
        hdc=BeginPaint(hMainWnd,&ps);
        hBrush=CreateSolidBrush(RGB(crRGB[0],crRGB[1],crRGB[2]));
        SelectObject(hdc,hBrush);
        Rectangle(hdc,rectLeft,rectTop,rectLeft+rectWidth,rectTop+>rectHeight);
        EndPaint(hMainWnd,&ps);
        break;
    case WM_COMMAND:
        switch(LOWord(wParam))
        {
        //如果子對話框sengmessage WM_PAINT消息過來在WM_PAINT消息裡面刷新則太浪費系統資源,所以自定義個消息來做刷新繪圖板的操作
        case IDC_DAVID:
            hdc=BeginPaint(hMainWnd,&ps);
            hBrush=CreateSolidBrush(RGB(crRGB[0],crRGB[1],crRGB[2]));
            SelectObject(hdc,hBrush);
            Rectangle(hdc,rectLeft,rectTop,rectLeft+rectWidth,rectTop+rectHeight);
            EndPaint(hMainWnd,&OLOR: #000000">ps);
            //refresh the color palette
            RECT cpRect;
            cpRect.left=rectLeft;
            cpRect.top=rectTop;
            cpRect.right=rectLeft+rectWidth;
            cpRect.bottom=rectTop+rectHeight;
            InvalidateRect(hMainWnd,&cpRect,1);
            break;
        case IDM_EXIT:
            SendMessage(hMainWnd,WM_DESTROY,0,0);
            break;
        casespan> IDM_RGB:
            DialogBox(hMainIns,"RGBDLG",hMainWnd,(DLGPROC)RGBDlgProc);
            break;
        case IDM_ABOUT:
            DialogBox(hMainIns,"ABOUTBOX",hMainWnd,(DLGPROC)About);
            break;
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
    default:
        return DefWindowProc(hwnd,message,wParam,IParam);
    }
    return(0);
}

/*
 *init WNDCLASS of the main window and regedit it
 */tyle="COLOR: #000000">
BOOL InitWindowsClass()
{
    WNDCLASS wndclass;
    wndclass.style=0;
    wndclass.lpfnWndProc=MainWndProc;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hInstance=hMainIns;
    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName="SCROLLBARMENU";
    wndclass.lp

 szClassName=lpszMainClassName;
    if(! RegisterClass(&wndclass))
    {
        MessageBeep(0);
        return

> FALSE;
    }
    
    return TRUE;
}

/*
 * The entrance of the programm
 */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance
                   , LPSTR lpCmdLine, int nCmdShow)
{
    MSG Msg;
    hMainIns=hInstance;

    /*
     * create the main window and the control
     * layout the main window and the control
     */
    if(!InitWindowsClass())
    {
        return FALSE;
    }
    if(!CreateMainWindow(nCmdShow))
    {
        return FALSE;
    }



    ShowWindow(hMainWnd,nCmdShow);

    while(GetMessage(&Msg,NULL,0,0))
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return (int)Msg.wParam;
}

 



//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)
    {
     &nbsp;  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
   &nbsp;    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        &nb
sp;   "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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved