Windows编程的基础问题

Micro_J 2002-09-19 08:04:08
程序清单:
#include <windows.h>
#include <math.h>
POINT P[5];

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
double theta;
bool p;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("SquareDemo") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
theta = 0;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
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 (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("SquareDemo"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters

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)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;

switch (message)
{
case WM_PAINT:
//hdc = GetDC(hwnd);
hdc = BeginPaint(hwnd,&ps);
P[0].x = P[4].x = 100 * cos(theta) - 100 * sin(theta) + 300;
P[0].y = P[4].y = 100 * cos(theta) + 100 * sin(theta) + 300;
P[1].x = (-100) * cos(theta) - 100 * sin(theta) + 300;
P[1].y = (-100) * sin(theta) + 100 * cos(theta) + 300;
P[3].x = 100 * cos(theta) + 100 * sin(theta) + 300;
P[3].y = 100 * sin(theta) - 100 * cos(theta) + 300;
P[2].x = (-100) * cos(theta) + 100 * sin(theta) + 300;
P[2].y = (-100) * sin(theta) - 100 * cos(theta) + 300;
Polygon(hdc,P,5);
EndPaint(hwnd,&ps);
//ReleaseDC(hwnd,hdc);
return 0 ;
case WM_LBUTTONUP:

theta += .1;
UpdateWindow(hwnd);//***********

return 0;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
这段程序在一个窗口上画一个正方形,然后当鼠标左键点击时,旋转一个角度。
问题是:
为什么打星的一行UpdateWindow没有用。在鼠标点击后还是没有重画窗口?
...全文
22 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingvictor 2002-09-19
  • 打赏
  • 举报
回复
楼上说得都对
MasterProgrammer 2002-09-19
  • 打赏
  • 举报
回复
的确如此!!!
想问一问GoAround: 我在程序中用API做了一个类似TOOLTIPS的小界面,但是画在别的程序界面里,当我刷新时,给程序发WM_PAINT消息无用,用InvalidateRect()无效.该如何做???
就象金山词霸的显示一样!
潘李亮 2002-09-19
  • 打赏
  • 举报
回复
楼上说的对
GoAround 2002-09-19
  • 打赏
  • 举报
回复
去掉
UpdateWindow(hwnd);//***********
加上
InvalidateRect(hwnd, NULL, TRUE);

UpdateWindow在窗口更新区域为空是不发送WM_PAINT消息,InvalidateRect用来标记更新区域。系统在更新区域不为空且应用程序消息队列中没有其他消息时向窗口发送WM_PAINT消息

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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