yuzhengxin 发表于 2020-2-18 21:18:46

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

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

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


int WINAPI WinMain (HINSTANCE hThisInstance,
                  HINSTANCE hPrevInstance,
                  LPSTR lpszArgument,
                  int nFunsterStil)

{
    HWND hwnd;   
    MSG messages;      
    WNDCLASSEX wincl;   

    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      
    wincl.style = CS_DBLCLKS;            
    wincl.cbSize = sizeof (WNDCLASSEX);
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;   
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

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

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

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

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

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


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

    switch (message)                  /* handle the messages */
    {   
      case WM_CREATE:
                     hdc=GetDC(hwnd);
                     hdcm=CreateCompatibleDC(hdc);
                     hbitmap=CreateCompatibleBitmap(hdc,500,500);
                     SelectObject(hdcm,hbitmap);
                     ReleaseDC(hwnd,hdc);
                                                                                       
                     hev=CreateEvent(NULL,0,0,NULL);
                     CreateThread(NULL,0,yzx,hwnd,0,NULL);
                     break;
      case WM_PAINT:
                      hdc=BeginPaint(hwnd,&ps);
                     
                      BitBlt(hdc,0,0,500,500,hdcm,0,0,SRCCOPY);
                     
                      EndPaint(hwnd,&ps);
                      break;
                     
      case WM_LBUTTONUP:

                        SetEvent(hev);
                        
                        break;
                                                      
      case WM_DESTROY:
                        CloseHandle(hev);
                        DeleteObject(hdcm);
                        DeleteObject(hbitmap);
                        PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                        break;
      default:      /* for messages that we don't deal with */
                return DefWindowProc(hwnd, message, wParam, lParam);
    }

    return 0;
}


DWORD WINAPI yzx(LPVOID asd)
{
char temp;      
int k=0;

while(1)
{
WaitForSingleObject(hev,INFINITE);

Rectangle(hdcm,0,0,500,500);
sprintf(temp,"%d",++k);
TextOut(hdcm,10,10,temp,strlen(temp));   
}

return 0;      
}




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

yuzhengxin 发表于 2020-2-18 21:20:29

困扰了好久,都没弄明白,请大家帮忙看一看。

yuzhengxin 发表于 2020-2-19 10:02:26

...咋没人回答啊?
页: [1]
查看完整版本: 关于在兼容DC上使用TextOut丢失问题