一个WINDOWS程序设计书上的简单例子,自己修改了一下,有一点不太清楚!

yiruirui0507 2010-12-22 09:51:10


#include <windows.h>

#define ID_TIMER 1

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Beeper1") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;

wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = sizeof(long) ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("Program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

hwnd = CreateWindow (szAppName, TEXT ("Beeper1 Timer Demo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

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

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static BOOL fFlipFlop = FALSE ;
HBRUSH hBrush ;
HDC hdc ;
PAINTSTRUCT ps ;
RECT rc ;

switch (message)
{
case WM_CREATE:
SetTimer (hwnd, ID_TIMER, 1000, NULL) ;
return 0 ;

case WM_TIMER :
MessageBeep (-1) ;
fFlipFlop = !fFlipFlop ;
//SetClassLong(hwnd,GCL_HCURSOR,fFlipFlop ? (long)LoadCursor(NULL,IDC_CROSS):LoadCursor(NULL,IDC_ARROW));
//UpdateWindow(hwnd);
SetCursor(fFlipFlop ? LoadCursor(NULL,IDC_CROSS):LoadCursor(NULL,IDC_ARROW));
InvalidateRect (hwnd, NULL, FALSE) ;
return 0 ;

case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;

GetClientRect (hwnd, &rc) ;
hBrush = CreateSolidBrush (fFlipFlop ? RGB(255,0,0) : RGB(0,0,255)) ;
FillRect (hdc, &rc, hBrush) ;
//SetCursor(fFlipFlop ? LoadCursor(NULL,IDC_CROSS):LoadCursor(NULL,IDC_ARROW));

EndPaint (hwnd, &ps) ;
DeleteObject (hBrush) ;
return 0 ;

case WM_DESTROY :
KillTimer (hwnd, ID_TIMER) ;
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

以上代码是正确的,设置了一个定时器,每隔一秒变换一下背景画刷跟鼠标指针,用的是SetCursor(fFlipFlop ? LoadCursor(NULL,IDC_CROSS):LoadCursor(NULL,IDC_ARROW));
这个函数可以实现,没问题,
我现在想用SetClassLong是完成这个功能,MSDN说需要specified offset into the extra class memory
所以我设置了 wndclass.cbClsExtra = sizeof(long) ;
然后调用 SetClassLong(hwnd,GCL_HCURSOR,fFlipFlop ? (long)LoadCursor(NULL,IDC_CROSS):LoadCursor(NULL,IDC_ARROW));
本来以为没什么问题了,可运行效果没体现出来,只有在背景画刷上移动才能看到效果,请问我哪里写错了?
...全文
107 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yiruirui0507 2010-12-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wltg2001 的回复:]
引用 3 楼 yiruirui0507 的回复:
引用 2 楼 wltg2001 的回复:
其实MSDN上有说明的:
Use the SetClassLong function with care. For example, it is possible to change the background color for a class by using SetClassLong, but……
[/Quote]
恩,这个的确不需要加,难道真的SETCLASSLONG就这么无能吗?MY GOD!!!
wltg2001 2010-12-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yiruirui0507 的回复:]
引用 2 楼 wltg2001 的回复:
其实MSDN上有说明的:
Use the SetClassLong function with care. For example, it is possible to change the background color for a class by using SetClassLong, but this change does not imme……
[/Quote]
试了一下,SetClassLong似乎就是这样的,要移一下鼠标才会发现效果。
另外,wndclass.cbClsExtra = sizeof(long);这个倒是不用加的。
yiruirui0507 2010-12-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wltg2001 的回复:]
其实MSDN上有说明的:
Use the SetClassLong function with care. For example, it is possible to change the background color for a class by using SetClassLong, but this change does not immediately repaint all wi……
[/Quote]

首先感谢你的回答,加上UPDATEWINDOW也不行的,我加了的,你看上面代码注释的部分就晓得了。
wltg2001 2010-12-22
  • 打赏
  • 举报
回复
其实MSDN上有说明的:
Use the SetClassLong function with care. For example, it is possible to change the background color for a class by using SetClassLong, but this change does not immediately repaint all windows belonging to the class.

wltg2001 2010-12-22
  • 打赏
  • 举报
回复
用SetClassLong并不会马上生效,要等下一次重绘时才会出效果。你可以用UpdateWindow强制重绘

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧