鱼C论坛

 找回密码
 立即注册
查看: 2025|回复: 6

[已解决]本意用for循环显示6个阴影线画刷,颜色随机,但是显示错误

[复制链接]
发表于 2015-6-1 17:27:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

#include <windows.h>
#include <stdlib.h>

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

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

     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 ("The Hello Program"), // 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 ;
         HBRUSH hbrush;
         int cxclient, cyclient;
         int i;
         PAINTSTRUCT ps;
         srand((unsigned)time(NULL));
         int apt[6] = { HS_HORIZONTAL, HS_BDIAGONAL, HS_VERTICAL, HS_CROSS, HS_FDIAGONAL, HS_DIAGCROSS };
     switch (message)
     {
     case WM_CREATE:
                 cxclient = LOWORD(lParam);
                 cyclient = HIWORD(lParam);
          return 0 ;
         
     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
                  for (i = 0; i < 6;i++)
                  {
                          SelectObject(hdc, CreateHatchBrush(apt[i], RGB(rand() % 256, rand() % 256, rand() % 256)));
                          Rectangle(hdc, cxclient*i / 6, cyclient / 8, cxclient * (i+1) / 6, cyclient * 7 /8);
                  }
          EndPaint (hwnd, &ps) ;
          return 0 ;
         
     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}
最佳答案
2015-6-9 18:07:09
楼主的消息处理错了,在WM_CREATE消息里是不能这样获取窗口大小的,应该处理WM_SIZE消息,也就是:
case WM_SIZE:
                cxclient = LOWORD(lParam);
                cyclient = HIWORD(lParam);

                return 0;
详情参考MSDN

无标题.png

顺便说一句,楼主在处理SelectObject的时候应该处理得更细致一点,比如保存旧画刷与销毁新画刷,这都是MSDN要求进行的动作
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-6-1 17:42:36 | 显示全部楼层
我又改进了下,还是不对。。。
  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include<time.h>
  4. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

  5. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  6.         PSTR szCmdLine, int iCmdShow)
  7. {
  8.         static TCHAR szAppName[] = TEXT("HelloWin");
  9.         HWND         hwnd;
  10.         MSG          msg;
  11.         WNDCLASS     wndclass;

  12.         wndclass.style = CS_HREDRAW | CS_VREDRAW;
  13.         wndclass.lpfnWndProc = WndProc;
  14.         wndclass.cbClsExtra = 0;
  15.         wndclass.cbWndExtra = 0;
  16.         wndclass.hInstance = hInstance;
  17.         wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  18.         wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  19.         wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  20.         wndclass.lpszMenuName = NULL;
  21.         wndclass.lpszClassName = szAppName;

  22.         if (!RegisterClass(&wndclass))
  23.         {
  24.                 MessageBox(NULL, TEXT("This program requires Windows NT!"),
  25.                         szAppName, MB_ICONERROR);
  26.                 return 0;
  27.         }

  28.         hwnd = CreateWindow(szAppName,                  // window class name
  29.                 TEXT("The Hello Program"), // window caption
  30.                 WS_OVERLAPPEDWINDOW,        // window style
  31.                 CW_USEDEFAULT,              // initial x position
  32.                 CW_USEDEFAULT,              // initial y position
  33.                 CW_USEDEFAULT,              // initial x size
  34.                 CW_USEDEFAULT,              // initial y size
  35.                 NULL,                       // parent window handle
  36.                 NULL,                       // window menu handle
  37.                 hInstance,                  // program instance handle
  38.                 NULL);                     // creation parameters

  39.         ShowWindow(hwnd, iCmdShow);
  40.         UpdateWindow(hwnd);

  41.         while (GetMessage(&msg, NULL, 0, 0))
  42.         {
  43.                 TranslateMessage(&msg);
  44.                 DispatchMessage(&msg);
  45.         }
  46.         return msg.wParam;
  47. }

  48. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  49. {
  50.         HDC         hdc;
  51.         HBRUSH hbrush;
  52.         static int cxclient, cyclient;
  53.         int i;
  54.         PAINTSTRUCT ps;
  55.         srand((unsigned)time(NULL));
  56.         COLORREF color[6] = { RGB(rand() % 256, rand() % 256, rand() % 256), RGB(rand() % 256, rand() % 256, rand() % 256), RGB(rand() % 256, rand() % 256, rand() % 256),
  57.                 RGB(rand() % 256, rand() % 256, rand() % 256), RGB(rand() % 256, rand() % 256, rand() % 256), RGB(rand() % 256, rand() % 256, rand() % 256) };
  58.         int apt[6] = { HS_HORIZONTAL, HS_BDIAGONAL, HS_VERTICAL, HS_CROSS, HS_FDIAGONAL, HS_DIAGCROSS };
  59.         switch (message)
  60.         {
  61.         case WM_CREATE:
  62.                 cxclient = LOWORD(lParam);
  63.                 cyclient = HIWORD(lParam);

  64.                 return 0;

  65.         case WM_PAINT:
  66.                 hdc = BeginPaint(hwnd, &ps);
  67.                 for (i = 0; i < 6; i++)
  68.                 {
  69.                 SelectObject(hdc, CreateHatchBrush(apt[i], color[i]));
  70.                 Rectangle(hdc, cxclient*i / 6, 0, cxclient * (i + 1) / 6, cyclient);
  71.                 }
  72.                 EndPaint(hwnd, &ps);
  73.                 return 0;

  74.         case WM_DESTROY:
  75.                 PostQuitMessage(0);
  76.                 return 0;
  77.         }
  78.         return DefWindowProc(hwnd, message, wParam, lParam);
  79. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-6-1 19:19:58 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-6-1 21:49:16 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-6-9 18:07:09 | 显示全部楼层    本楼为最佳答案   
楼主的消息处理错了,在WM_CREATE消息里是不能这样获取窗口大小的,应该处理WM_SIZE消息,也就是:
case WM_SIZE:
                cxclient = LOWORD(lParam);
                cyclient = HIWORD(lParam);

                return 0;
详情参考MSDN

无标题.png

顺便说一句,楼主在处理SelectObject的时候应该处理得更细致一点,比如保存旧画刷与销毁新画刷,这都是MSDN要求进行的动作
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2015-6-12 12:38:57 | 显示全部楼层
南郊居士 发表于 2015-6-9 18:07
楼主的消息处理错了,在WM_CREATE消息里是不能这样获取窗口大小的,应该处理WM_SIZE消息,也就是:
case W ...

嗯,谢谢了啊,问题解决咯
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-15 08:34:42 | 显示全部楼层
咱们码农有力量,嘿
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 11:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表