yuzhengxin2011 2020-02-22 12:06 采纳率: 0%
浏览 565

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

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

如下是代码:

#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[111];      
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条回答 默认 最新

  • kiloyqp 2020-02-22 12:41
    关注

    建议在主线程中进行界面更新操作

    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误