📄 ebtrackbar.c
字号:
// $Id: ebtrackbar.c,v 1.1.1.1 2005/01/18 11:47:58 tangjb Exp $
//
// trackbar.c: the TrackBar Control module.
//
// Copyright (C) 2000, 2001, 2002, Wei Yongming.
//
// NOTE: Originally by Zheng Yiran
//
// Create date: 2000/12/02
/*
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Library General Public
** License as published by the Free Software Foundation; either
** version 2 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Library General Public License for more details.
**
** You should have received a copy of the GNU Library General Public
** License along with this library; if not, write to the Free
** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
** MA 02111-1307, USA
*/
/*
** Alternatively, the contents of this file may be used under the terms
** of the Mozilla Public License (the "MPL License") in which case the
** provisions of the MPL License are applicable instead of those above.
*/
// Modify records:
//
// Who When Where For What Status
//-----------------------------------------------------------------------------
//
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "ctrlmisc.h"
#include "ebtrackbar.h"
// use these definition when drawing trackbar
#define UpDnBlankWidth 10
#define LfRtBlankWidth 15
#define SliderGap 5
#define SliderMaxWidth 20
#define SliderMaxHeight 35
#define MidBlankWidth 6 // please keep it even for good appearance
#define TickLength 4
#define TipSliderGap 12
#define TickSliderGap 6
// use these definition when drawing trackbar
static int EBTrackBarCtrlProc (HWND hwnd, int message, WPARAM wParam, LPARAM lParam);
BOOL RegisterEBTrackBarControl (void)
{
WNDCLASS WndClass;
WndClass.spClassName = CTRL_EBTRACKBAR;
WndClass.dwStyle = WS_NONE;
WndClass.dwExStyle = WS_EX_NONE;
WndClass.hCursor = GetSystemCursor (0);
WndClass.iBkColor = GetWindowElementColor (BKC_CONTROL_DEF);
WndClass.WinProc = EBTrackBarCtrlProc;
return RegisterWindowClass (&WndClass);
}
void EBTrackBarControlCleanup (void)
{
UnregisterWindowClass (CTRL_EBTRACKBAR);
}
static void EBTrackBarOnDraw (HWND hwnd, HDC hdc, EBTRACKBARDATA* pData, DWORD dwStyle)
{
RECT rcClient;
int x, y, w, h, len, mousepos;
int pos, max, min, TickFreq, EndTipLen;
int sliderx, slidery, sliderw, sliderh;
int TickStart, TickEnd;
float TickGap, Tick;
char sPos[10];
int bkmode,slidermode,fillmode;
GetClientRect (hwnd, &rcClient);
// get data of trackbar.
x = rcClient.left;
y = rcClient.top;
w = RECTW (rcClient);
h = RECTH (rcClient);
mousepos = pData->mousepos;
pos = pData->nPos;
max = pData->nMax;
min = pData->nMin;
TickFreq = pData->nTickFreq;
bkmode = dwStyle & ETBS_BKTYPE;
slidermode = dwStyle & ETBS_IDTNTYPE;
fillmode = dwStyle & ETBS_FILLTYPE;
// draw the border according to trackbar style.
//画背景;
switch(bkmode)
{
case ETBS_BKNORMAL:
{
SetBrushColor (hdc, GetWindowBkColor(hwnd));
if (dwStyle & ETBS_BORDER) {
Draw3DDownFrame (hdc, x, y, x + w - 1, y + h - 1, PIXEL_invalid);
FillBox (hdc, x + pData->nTbBorder, y + pData->nTbBorder, w - (pData->nTbBorder<<1), h - (pData->nTbBorder<<1));
} else {
FillBox (hdc, x, y, w, h);
}
//draw the blank in middle of trackbar.
if (dwStyle & ETBS_VERTICAL) {
Draw3DDownFrame (hdc, x + (w>>1) - (MidBlankWidth>>1), y + LfRtBlankWidth,
x + (w>>1) + (MidBlankWidth>>1), y + h - LfRtBlankWidth, PIXEL_lightgray);
} else {
Draw3DDownFrame (hdc, x + LfRtBlankWidth, y + (h>>1) - (MidBlankWidth>>1),
x + w - LfRtBlankWidth,
y + (h>>1) + (MidBlankWidth>>1), PIXEL_lightgray);
}
}
break;
case ETBS_BKBITMAP:
{
if (pData && pData->nImage)
{
// FillWithBitmapAlpha(hwnd,hdc,((PBITMAP*)(pData->nImage))[0],dwStyle &ETBS_BKALPHA);
FillWithPicture(hwnd, hdc, NULL, ((PBITMAP*)(pData->nImage))[0],
BMP_TYPE_ALPHAVALUE, dwStyle &ETBS_BKALPHA);
SetBkMode(hdc,BM_TRANSPARENT);
}
}
break;
case ETBS_BKBMPFILE:
{
if (pData && pData->nImage)
{
char bmpfile[MAX_PATH+1];
char * pbmpfile;
pbmpfile = &bmpfile[0];
PrefixFileName((const char *)(pData->nImage),"bk",pbmpfile,MAX_PATH);
// FillWithBmpfile(hwnd,hdc,pbmpfile,dwStyle &ETBS_BKALPHA);
FillWithPicture(hwnd,hdc,pbmpfile, NULL, BMP_TYPE_ALPHACHANNEL,dwStyle &ETBS_BKALPHA);
SetBkMode(hdc,BM_TRANSPARENT);
}
}
break;
case ETBS_BKICON:
{
if (pData && pData->nImage)
{
HICON hIcon;
hIcon = ((HICON*)(pData->nImage))[0];
DrawIcon (hdc, 0, 0, 0, 0, hIcon);
SetBkMode(hdc,BM_TRANSPARENT);
}
break;
}
break;
default:
break;
}
switch(fillmode)
{
case ETBS_FILLNONE:
break;
case ETBS_FILLLINE:
{
}
break;
case ETBS_FILLIDTN:
{
}
break;
case ETBS_FILLRECT:
{
}
break;
default:
break;
}
switch(slidermode)
{
case ETBS_IDTNAROW:
{
//画游标; draw the slider of trackbar according to pos.
if (dwStyle & ETBS_VERTICAL) {
sliderw = w - (UpDnBlankWidth<<1) - (SliderGap<<1);
if (sliderw > SliderMaxHeight) { /* slider can not too big */
sliderw = SliderMaxHeight;
sliderh = SliderMaxWidth;
} else {
sliderh = sliderw * SliderMaxWidth / SliderMaxHeight;
}
sliderx = x + (w>>1) - (sliderw>>1);
if (pData->state & ETBS_DRAGGED) { /* user is dragging slider */
len = h - (LfRtBlankWidth<<1);
if ( mousepos > y + h - LfRtBlankWidth ) {
pos = 0 ;
mousepos = y + h - LfRtBlankWidth;
} else if (mousepos < y + LfRtBlankWidth ) {
pos = len;
mousepos = y + LfRtBlankWidth;
} else
pos = y + h - mousepos - LfRtBlankWidth ;
slidery = mousepos - (sliderh>>1);
pData->nPos = (int)(((max - min) * pos) / (float)len + 0.5) + min;
} else
slidery = y + LfRtBlankWidth + (int)(((max - pos) *
(h - (LfRtBlankWidth<<1))) / (float)(max - min)) - (sliderh>>1);
} else {
sliderh = h - (UpDnBlankWidth<<1) - (SliderGap<<1);
if (sliderh > SliderMaxHeight) {
sliderh = SliderMaxHeight;
sliderw = SliderMaxWidth;
} else {
sliderw = sliderh * SliderMaxWidth / SliderMaxHeight;
}
if (pData->state & ETBS_DRAGGED) {
len = w - (LfRtBlankWidth<<1);
if ( mousepos > x + w - LfRtBlankWidth ) {
pos = len ;
mousepos = x + w - LfRtBlankWidth;
} else if (mousepos < x + LfRtBlankWidth ) {
pos = 0;
mousepos = x + LfRtBlankWidth;
} else
pos = mousepos - x - LfRtBlankWidth ;
sliderx = mousepos - (sliderw>>1);
pData->nPos = (int)(((max - min) * pos*1.0) / (float)len + 0.5) + min;
} else
sliderx = x + LfRtBlankWidth + (int)(((pos - min) *
(w - (LfRtBlankWidth<<1))*1.0) / (float)(max - min)) - (sliderw>>1);
slidery = y + (h>>1) - (sliderh>>1);
}
Draw3DUpFrame (hdc, sliderx, slidery, sliderx + sliderw, slidery + sliderh, PIXEL_lightgray);
}
break;
case ETBS_IDTNBITMAP:
case ETBS_IDTNBMPFILE:
{
if (pData && pData->nSliderImage)
{
PBITMAP pbmp = (PBITMAP)(pData->nSliderImage);
if(dwStyle &ETBS_BKALPHA)
{
unsigned char r,g,b,a;
Pixel2RGBA(HDC_SCREEN,GetWindowBkColor (hwnd),&r,&g,&b,&a);
pbmp->bmType |= BMP_TYPE_ALPHACHANNEL;
pbmp->bmAlpha = a;
}
if (dwStyle & ETBS_VERTICAL) {
sliderw = pbmp->bmWidth;
sliderh = pbmp->bmHeight;
sliderx = x+(w-sliderw)/2;
if (pData->state & ETBS_DRAGGED) { /* user is dragging slider */
len = h - sliderh;
if ( mousepos > y + len + sliderh >> 1 ) {
pos = 0 ;
mousepos = y + len + sliderh >> 1;
} else if (mousepos < (y + sliderh >> 1) ) {
pos = len;
mousepos = y + sliderh >> 1;
} else
pos = y + h - mousepos - sliderh >> 1 ;
slidery = mousepos - (sliderh >>1 );
pData->nPos = (int)(((max - min) * pos * 1.0) / (float)len + 0.5) + min;
} else
slidery = y + (int)(((max - pos) * (h - (sliderh))*1.0) / (float)(max - min));
} else {
sliderw = pbmp->bmWidth;
sliderh = pbmp->bmHeight;
if (pData->state & ETBS_DRAGGED) {
len = w - (sliderw);
if ( mousepos > x + w - sliderw >> 1 ) {
pos = len ;
mousepos = x + w - sliderw >> 1;
} else if (mousepos < x + sliderw >> 1 ) {
pos = 0;
mousepos = x + sliderw >> 1;
} else
pos = mousepos - x - sliderw >> 1 ;
sliderx = mousepos - (sliderw >> 1);
pData->nPos = (int)((max - min) * pos / (float)len + 0.5) + min;
} else
{
sliderx = x + (int)(((pos - min) * (w - sliderw) *1.0 ) / (float)(max - min));
}
slidery = y + (h>>1) - (sliderh>>1);
}
FillBoxWithBitmap(hdc, sliderx, slidery, 0, 0,pbmp);
}
}
break;
case ETBS_IDTNICON:
{
if (pData && pData->nImage)
{
HICON hIcon;
//PICON picon;
hIcon = ((HICON*)(pData->nImage))[1];
//picon = (PICON)hIcon;
if (dwStyle & ETBS_VERTICAL) {
sliderw = 32;//picon->width;
sliderh = 12;
sliderx = x+(w-sliderw)/2;
if (pData->state & ETBS_DRAGGED) { /* user is dragging slider */
len = h - sliderh;
if ( mousepos > y + len + sliderh >> 1 ) {
pos = 0 ;
mousepos = y + len + sliderh >> 1;
} else if (mousepos < (y + sliderh >> 1) ) {
pos = len;
mousepos = y + sliderh >> 1;
} else
pos = y + h - mousepos - sliderh >> 1 ;
slidery = mousepos - (sliderh >>1 );
pData->nPos = (int)((max - min) * pos / (float)len + 0.5) + min;
} else
slidery = y + sliderh >> 1 + (int)((max - pos) *
(h - (sliderh)) / (float)(max - min)) - (sliderh>>1);
} else {
sliderw = 32;
sliderh = 12;
if (pData->state & ETBS_DRAGGED) {
len = w - (sliderw);
if ( mousepos > x + w - sliderw >> 1 ) {
pos = len ;
mousepos = x + w - sliderw >> 1;
} else if (mousepos < x + sliderw >> 1 ) {
pos = 0;
mousepos = x + sliderw >> 1;
} else
pos = mousepos - x - sliderw >> 1 ;
sliderx = mousepos - (sliderw >> 1);
pData->nPos = (int)((max - min) * pos / (float)len + 0.5) + min;
} else
sliderx = x + sliderw >> 1 + (int)((pos - min) *
(w - sliderw) / (float)(max - min)) - (sliderw>>1);
slidery = y + (h>>1) - (sliderh>>1);
}
DrawIcon (hdc, 0, 0, 0, 0, hIcon);
}
break;
}
break;
default:
break;
}
//画显示的文字; draw the tip of trackbar.
if ((dwStyle & ETBS_TIP) && !(dwStyle & ETBS_VERTICAL)) {
SIZE text_ext;
SetTextColor (hdc, PIXEL_black);
SetBkMode (hdc, BM_TRANSPARENT);
TextOut (hdc, x + pData->nTbBorder + 1, y + (h>>1) - (sliderh>>1) - TipSliderGap,
pData->sStartTip);
GetTextExtent (hdc, pData->sEndTip, -1, &text_ext);
EndTipLen = text_ext.cx + 4;
TextOut (hdc, (EndTipLen > (w>>1) - 20 ? x + (w>>1) + 20 : x + w -EndTipLen),
y + (h>>1) - (sliderh>>1) - TipSliderGap, pData->sEndTip);
sprintf (sPos, "%d", pData->nPos);
GetTextExtent (hdc, sPos, -1, &text_ext);
TextOut (hdc, x + ((w - text_ext.cx) >> 1),
y + (h>>1) -(sliderh>>1) - TipSliderGap, sPos);
}
//画出标尺线,背景; draw the tick of trackbar.
if(bkmode == ETBS_BKNORMAL)
{
SetPenColor (hdc, pData->nFgndColor);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -