鱼C论坛

 找回密码
 立即注册
查看: 2820|回复: 2

关于在兼容DC上使用TextOut丢失问题

[复制链接]
发表于 2020-2-18 21:18:46 | 显示全部楼层 |阅读模式

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

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

x
先上代码:
  1. #include <windows.h>
  2. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  3. DWORD WINAPI yzx(LPVOID asd);//在兼容DC上绘图的(专用)线程

  4. char szClassName[ ]="WTF";
  5. HBITMAP hbitmap;
  6. HDC hdcm;
  7. HANDLE hev;//控制(绘图)线程的开关


  8. int WINAPI WinMain (HINSTANCE hThisInstance,
  9.                     HINSTANCE hPrevInstance,
  10.                     LPSTR lpszArgument,
  11.                     int nFunsterStil)

  12. {
  13.     HWND hwnd;     
  14.     MSG messages;      
  15.     WNDCLASSEX wincl;   

  16.     wincl.hInstance = hThisInstance;
  17.     wincl.lpszClassName = szClassName;
  18.     wincl.lpfnWndProc = WindowProcedure;      
  19.     wincl.style = CS_DBLCLKS;              
  20.     wincl.cbSize = sizeof (WNDCLASSEX);
  21.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  22.     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  23.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  24.     wincl.lpszMenuName = NULL;   
  25.     wincl.cbClsExtra = 0;
  26.     wincl.cbWndExtra = 0;
  27.     wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

  28.     /* Register the window class, and if it fails quit the program */
  29.     if (!RegisterClassEx (&wincl))
  30.         return 0;

  31.     /* The class is registered, let's create the program*/
  32.     hwnd = CreateWindowEx (
  33.            0,                   /* Extended possibilites for variation */
  34.            szClassName,         /* Classname */
  35.            "hjdl",       /* Title Text */
  36.            WS_OVERLAPPEDWINDOW, /* default window */
  37.            0,       /* Windows decides the position */
  38.            0,       /* where the window ends up on the screen */
  39.            1000,                 /* The programs width */
  40.            800,                 /* and height in pixels */
  41.            HWND_DESKTOP,        /* The window is a child-window to desktop */
  42.            NULL,                /* No menu */
  43.            hThisInstance,       /* Program Instance handler */
  44.            NULL                 /* No Window Creation data */
  45.            );

  46.     /* Make the window visible on the screen */
  47.     ShowWindow (hwnd, nFunsterStil);

  48.     /* Run the message loop. It will run until GetMessage() returns 0 */
  49.     while (GetMessage (&messages, NULL, 0, 0))
  50.     {
  51.         /* Translate virtual-key messages into character messages */
  52.         TranslateMessage(&messages);
  53.         /* Send message to WindowProcedure */
  54.         DispatchMessage(&messages);
  55.     }

  56.     /* The program return-value is 0 - The value that PostQuitMessage() gave */
  57.     return messages.wParam;
  58. }


  59. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  60. {
  61. HDC hdc;
  62. PAINTSTRUCT ps;

  63.     switch (message)                  /* handle the messages */
  64.     {   
  65.         case WM_CREATE:
  66.                        hdc=GetDC(hwnd);
  67.                        hdcm=CreateCompatibleDC(hdc);
  68.                        hbitmap=CreateCompatibleBitmap(hdc,500,500);
  69.                        SelectObject(hdcm,hbitmap);
  70.                        ReleaseDC(hwnd,hdc);
  71.                                                                                        
  72.                        hev=CreateEvent(NULL,0,0,NULL);
  73.                        CreateThread(NULL,0,yzx,hwnd,0,NULL);
  74.                        break;
  75.         case WM_PAINT:
  76.                       hdc=BeginPaint(hwnd,&ps);
  77.                      
  78.                       BitBlt(hdc,0,0,500,500,hdcm,0,0,SRCCOPY);
  79.                      
  80.                       EndPaint(hwnd,&ps);
  81.                       break;
  82.                      
  83.         case WM_LBUTTONUP:

  84.                           SetEvent(hev);
  85.                           
  86.                           break;
  87.                                                       
  88.         case WM_DESTROY:
  89.                         CloseHandle(hev);
  90.                         DeleteObject(hdcm);
  91.                         DeleteObject(hbitmap);
  92.                         PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
  93.                         break;
  94.         default:        /* for messages that we don't deal with */
  95.                 return DefWindowProc(hwnd, message, wParam, lParam);
  96.     }

  97.     return 0;
  98. }


  99. DWORD WINAPI yzx(LPVOID asd)
  100. {
  101. char temp[111];      
  102. int k=0;

  103. while(1)
  104. {
  105. WaitForSingleObject(hev,INFINITE);

  106. Rectangle(hdcm,0,0,500,500);
  107. sprintf(temp,"%d",++k);
  108. TextOut(hdcm,10,10,temp,strlen(temp));   
  109. }

  110. return 0;      
  111. }

复制代码



【1】我的想法是先在hdcm上绘图(单击鼠标左键一次就绘制一遍),但不立即更新客户区(未立即InvalidateRect)。
【2】在绘制后,先最小化窗口、再恢复窗口(windows应自动更新客户显示区)后,没有看到文字(矩形正常显示)!这是为什么呢?
【3】特别的,若连续使用多条TextOut,则只有最后一条TextOut不显示,其余均正常显示!
                     若在最后一条TextOut后面再加一些函数,如:Rectangle/LineTo/GetPixel/InvilidateRect等等,就全都正常显示了!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-18 21:20:29 | 显示全部楼层
困扰了好久,都没弄明白,请大家帮忙看一看。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-19 10:02:26 | 显示全部楼层
...咋没人回答啊?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-4 21:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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