首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

程序开始时滚动条取得焦点,焦点的显示很奇怪,求解

2013-07-01 
程序开始时滚动条获得焦点,焦点的显示很奇怪,求解设置3个滚动条,默认第一滚动条获得焦点,这三个滚动条之间

程序开始时滚动条获得焦点,焦点的显示很奇怪,求解
设置3个滚动条,默认第一滚动条获得焦点,这三个滚动条之间的焦点切换可以用tab键。有个问题就是:程序开始的时候,滚动条的焦点显示并不在滑块上,而是在上方的小按钮上,使用鼠标或则tab切换焦点之后就变正常,后面无论怎么切换,都是正常的,就是开始有问题..程序其他功能没有问题,就是有这个瑕疵..求解
为什么书上啊的案例就是正常的,我的就不是,纳闷中...也许我的描述不够细致,附上代码,运行一下就明白了...求助啊...

#include <Windows.h>
LRESULT CALLBACK wndprog(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK wndscroll(HWND,UINT,WPARAM,LPARAM);
WNDPROC oldscroll[3];
int idfocus;

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,  int nShowCmd )
{
TCHAR szappname[] = TEXT("mywindow");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc = wndprog;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szappname;

if ( !RegisterClass(&wndclass))
{
MessageBox(NULL,"creat window failed!",szappname,MB_ICONERROR);
return 0 ;
}

hwnd = CreateWindow(szappname,TEXT("This is my window !"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,
hInstance,NULL);

ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);

while( GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

LRESULT CALLBACK wndprog(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
HDC hdc;
PAINTSTRUCT ps;
HINSTANCE hinstance;
static RECT rect;
static COLORREF basiccolor[3]={RGB(255,0,0),RGB(0,255,0),RGB(0,0,255)};
static HWND hscroll[3], hlabel[3],hvalue[3],hrect;
static HBRUSH hbrush[3], hbrushstatic;
static int color[3];
static int xclient, yclient;
static int ychar, i ;
static TCHAR *szcolorlabel[3]={TEXT("red"),TEXT("green"),TEXT("blue")};
TCHAR szbuffer[10];

switch (message)
{
case WM_CREATE:
{
hinstance = (HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE); // 获得实例句柄
// 创建左半区静态文本
hrect = CreateWindow(TEXT("static"),NULL,
WS_CHILD | WS_VISIBLE | SS_WHITERECT,
0,0,0,0,hwnd,(HMENU)9,hinstance,NULL);

// 创建滚动条及静态文本
for ( i = 0; i < 3; i++)
{
// 滚动条ID : 0-2
hscroll[i] =  CreateWindow(TEXT("scrollbar"),NULL,
WS_CHILD | WS_VISIBLE | WS_TABSTOP | SBS_VERT,
0,0,0,0,hwnd,(HMENU) i,hinstance,NULL);
// 设置滚动条初始位置
SetScrollRange(hscroll[i],SB_CTL,0,255,FALSE);
SetScrollPos(hscroll[i],SB_CTL,0,FALSE);

// 文本ID : 3-5,
hlabel[i] = CreateWindow(TEXT("static"),szcolorlabel[i],
WS_CHILD | WS_VISIBLE | SS_CENTER,
0,0,0,0,hwnd,(HMENU)(i+3),hinstance,NULL);
//数值文本 ID 5-8
hvalue[i] = CreateWindow(TEXT("static"),TEXT("0"),
WS_CHILD | WS_VISIBLE | SS_CENTER,


0,0,0,0,hwnd,(HMENU)(i+6),hinstance,NULL);
// 创建三原色的画刷
hbrush[i] = (HBRUSH)CreateSolidBrush(basiccolor[i]);
// 设置新的滚动条回调函数并保存系统提供的回调函数
oldscroll[i] = (WNDPROC)SetWindowLong(hscroll[i],GWL_WNDPROC,(LONG)wndscroll);
}
// 设置右区颜色
hbrushstatic = CreateSolidBrush(GetSysColor(COLOR_BTNHIGHLIGHT));
// 获取字体高度
ychar = HIWORD(GetDialogBaseUnits());
}
return 0;
case WM_SIZE: 
{
// 移动各个控件
xclient = LOWORD(lparam);
yclient = HIWORD(lparam);
SetRect(&rect,xclient/2,0, xclient, yclient);
// 左边的静态文本
MoveWindow(hrect,0,0,xclient/2,yclient,TRUE);

for ( i = 0; i < 3; i++)
{
MoveWindow(hscroll[i], ( i * 2 +1 ) * xclient / 14, ychar * 2,
xclient/14, yclient - ychar * 4, TRUE );

MoveWindow(hlabel[i], ( i * 4 +1 ) * xclient / 28 ,ychar ,
xclient/7,  ychar, TRUE );

MoveWindow(hvalue[i], ( i * 2 +1 ) * xclient / 14, yclient-2*ychar,
xclient/14,ychar , TRUE );
}
}
return 0 ;
case WM_VSCROLL:
{
i = GetWindowLong((HWND)lparam, GWL_ID); // 获取滚动条ID
switch(LOWORD(wparam))
{
case SB_PAGEDOWN:
color[i] += 15;
case SB_LINEDOWN:
color[i] = min(255, color[i]+1);
break;
case SB_PAGEUP:
color[i] -= 15;
case SB_LINEUP:
color[i] = max(0, color[i]-1);
break;
case SB_TOP:
color[i] = 0;
break;
case SB_BOTTOM:
color[i] = 255;
break;
case SB_THUMBPOSITION:
case SB_THUMBTRACK:
color[i] = HIWORD(wparam);
break;
default: break;
}
// 更新滚动条位置和色素数值
SetScrollPos(hscroll[i],SB_CTL,color[i],TRUE);
wsprintf(szbuffer, TEXT("%i"),color[i]);
SetWindowText(hvalue[i],szbuffer);
// 更改窗口类的背景画刷
DeleteObject((HBRUSH)SetClassLong(hwnd,GCL_HBRBACKGROUND,
(LONG)CreateSolidBrush(RGB(color[0],color[1],color[2]) ) ) );
InvalidateRect(hwnd,&rect,TRUE);
}
return 0;
case WM_CTLCOLORSTATIC:
i = GetWindowLong((HWND)lparam,GWL_ID);
if ( i > 2 && i < 9)
{
SetTextColor(HDC(wparam),basiccolor[i%3]);
SetBkColor((HDC)wparam, GetSysColor(COLOR_BTNHIGHLIGHT));
return (LRESULT)hbrushstatic;
}
return 0;
case WM_CTLCOLORSCROLLBAR:
 i = GetWindowLong((HWND)lparam,GWL_ID);
     return (LRESULT)hbrush[i];   // 返回一个画刷给滚动条着色

case WM_SYSCOLORCHANGE: // 人为更换系统颜色, 更新颜色
DeleteObject(hbrushstatic);
hbrushstatic = CreateSolidBrush(GetSysColor(COLOR_BTNHIGHLIGHT));
return 0;
case WM_SETFOCUS:
SetFocus(hscroll[idfocus]);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
//add code here
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
DeleteObject(hbrushstatic);
DeleteObject((HBRUSH)SetClassLong(hwnd,GCL_HBRBACKGROUND,


(LONG)GetStockObject(WHITE_BRUSH)) );
for ( int j = 0; j < 3; j++)
DeleteObject(hbrush[j]);

PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wparam,lparam);
}
LRESULT CALLBACK wndscroll(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
int id = GetWindowLong(hwnd,GWL_ID);

switch(message)
{
case WM_KEYDOWN:
if ( wparam == VK_TAB)
SetFocus(GetDlgItem(GetParent(hwnd), (++id)%3));
id %= 3;
case WM_SETFOCUS:
idfocus = id;
break;
}
return CallWindowProc(oldscroll[idfocus],hwnd,message,wparam,lparam);
}

热点排行