⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wprintf.c

📁 <Win2k系统编程>源码.次数为国人自编,内容丰富,还是不错的.
💻 C
📖 第 1 页 / 共 3 页
字号:


/*****************************************************************************\
* SetPrintfTabs                                                             
*
* Sets the Tabstops in the printf window.
*        
* Arguments:
*    INT n - number of tabs to set.
*    LPINT pTabs - arrays of tabstops
*
* Returns:                                                                   
*    VOID                                                                              
\*****************************************************************************/

VOID
SetPrintfTabs(
    INT n,
    LPINT pTabs
    )
{
    INT i;

    nTabs = n;
    for (i = 0; i < nTabs; i++)
        tabs[i] = *pTabs++;
}



/*****************************************************************************\
* ClearPrintfWindow
*                                                                              
* Clears all text from the window                                             
*                                                                             
* Arguments:                                                                 
*    HWND hwnd - window handle for the Degubing window                  
*
* Returns:
*    VOID                                                                              
\*****************************************************************************/

VOID
ClearPrintfWindow(
    HWND hwnd
    )
{
    INT   i, iQueue;
    PTXT  pTxt;
    HTXT  hTxt;

    if (hwnd != NULL && IsWindow(hwnd)) {
        hTxt  = (HTXT)GetWindowLong(hwnd, 0);
        pTxt = *hTxt;

        EnterCrit(pTxt);

        iQueue = TOP(pTxt);
        for (i = 0; i < (pTxt)->iCount; i++, INC(pTxt, iQueue))
            if ((pTxt)->arLines[iQueue].hText != NULL) {
                LocalFree ((HANDLE)pTxt->arLines[iQueue].hText);
                pTxt->arLines[iQueue].hText = NULL;
            }

        pTxt->iFirst            = 0;  /* Set the queue up to have 1 NULL line */
        pTxt->iCount            = 1;
        pTxt->iTop              = 0;
        pTxt->iLeft             = 0;
        pTxt->MaxLen            = 0;
        pTxt->arLines[0].hText  = NULL;
        pTxt->arLines[0].iLen   = 0;

        wprintfSetScrollRange(hwnd, FALSE);
        InvalidateRect(hwnd, NULL, TRUE);

        LeaveCrit(pTxt);
    }
}


/*****************************************************************************\
* PrintfWndProc( hwnd, uiMessage, wParam, lParam )                          
*
* The window proc for the debugging window.  This processes all          
* of the window's messages.                                              
*                                                                              
* Arguments:                                                                 
*   HWND hwnd - window handle for the parent window                    
*   UINT uiMessage -message number                                         
*   WPARAM wParam - message-dependent                                      
*   LPARAM lParam - message-dependent                                      
*                                                                              
* Returns:                                                                   
*   0 if processed, nonzero if ignored                                     
\*****************************************************************************/

PUBLIC LONG APIENTRY
PrintfWndProc(
HWND   hwnd,
UINT   uiMessage,
WPARAM wParam,
LONG   lParam
)
{
    PAINTSTRUCT rPS;
    HTXT        hTxt;
    PTXT        pTxt;

    hTxt  = (HTXT)GetWindowLong(hwnd, 0);

    if ( hTxt ) {
        pTxt = *hTxt;
    }

    switch (uiMessage) {
    case WM_CREATE:
        {
            /* set the WindowLong before any other message tries to
                         * reference it during the create of a window
                         */
            LPCREATESTRUCT csWindowLong = (LPCREATESTRUCT) lParam;

            hTxt = (HTXT)csWindowLong->lpCreateParams;

            SetWindowLong(hwnd, 0, (LONG)hTxt);
            SetPrintfFont(hwnd, ghfontPrintf);
            wprintfSetScrollRange(hwnd, FALSE);
        }
        break;

    case WM_DESTROY:
        {
            INT i, iQueue;

            EnterCrit(pTxt);

            iQueue = TOP(pTxt);
            for (i = 0; i < (pTxt)->iCount; i++, INC(pTxt, iQueue))
                if ((pTxt)->arLines[iQueue].hText != NULL) {
                    LocalFree ((HANDLE)(pTxt)->arLines[iQueue].hText);
                    pTxt->arLines[iQueue].hText = NULL;
                }

            LeaveCrit(pTxt);
            // DeleteCriticalSection(&pTxt->csSync);

            LocalFree((HANDLE)hTxt);
            break;
        }

    case WM_SIZE:
        EnterCrit(pTxt);
        if (!iSem) {
            wprintfSetScrollRange(hwnd, TRUE);
        }
        DebugVScroll(hwnd, pTxt, 0);
        LeaveCrit(pTxt);
        break;

    case WM_VSCROLL:
        EnterCrit(pTxt);

        switch (LOWORD(wParam)) {
        case SB_LINEDOWN:
            DebugVScroll(hwnd, pTxt, 1);
            break;
        case SB_LINEUP:
            DebugVScroll(hwnd, pTxt, -1);
            break;
        case SB_PAGEUP:
            DebugVScroll(hwnd, pTxt, -LinesInDebugWindow(hwnd));
            break;
        case SB_PAGEDOWN:
            DebugVScroll(hwnd, pTxt, LinesInDebugWindow(hwnd));
            break;
        case SB_THUMBTRACK:
        case SB_THUMBPOSITION:
            DebugVScroll(hwnd, pTxt, HIWORD(wParam) - pTxt->iTop);
            break;
        case SB_ENDSCROLL:
            break;
        }

        LeaveCrit(pTxt);
        break;

    case WM_HSCROLL:
        EnterCrit(pTxt);

        switch (LOWORD(wParam)) {
        case SB_LINEDOWN:
            DebugHScroll (hwnd, pTxt, 1);
            break;
        case SB_LINEUP:
            DebugHScroll (hwnd, pTxt, -1);
            break;
        case SB_PAGEUP:
            DebugHScroll (hwnd, pTxt, -CharsInDebugWindow(hwnd));
            break;
        case SB_PAGEDOWN:
            DebugHScroll (hwnd, pTxt, CharsInDebugWindow(hwnd));
            break;
        case SB_THUMBTRACK:
        case SB_THUMBPOSITION:
            DebugHScroll(hwnd, pTxt, HIWORD(wParam) - pTxt->iLeft);
            break;
        case SB_ENDSCROLL:
            break;
        }

        LeaveCrit(pTxt);
        break;

    case WM_PAINT:
        EnterCrit(pTxt);

        BeginPaint(hwnd, (LPPAINTSTRUCT) & rPS);
        DebugPaint(hwnd, &rPS);
        EndPaint(hwnd, (LPPAINTSTRUCT) & rPS);

        LeaveCrit(pTxt);
        break;

    case WM_KEYDOWN:
        EnterCrit(pTxt);

        switch (wParam) {
        case VK_UP:
            DebugVScroll(hwnd, pTxt, -1);
            break;
        case VK_DOWN:
            DebugVScroll(hwnd, pTxt, 1);
            break;
        case VK_PRIOR:
            DebugVScroll(hwnd, pTxt, -LinesInDebugWindow(hwnd));
            break;
        case VK_NEXT:
            DebugVScroll(hwnd, pTxt, LinesInDebugWindow(hwnd));
            break;
        case VK_LEFT:
            DebugHScroll(hwnd, pTxt, -1);
            break;
        case VK_RIGHT:
            DebugHScroll(hwnd, pTxt, 1);
            break;
        }

        LeaveCrit(pTxt);
        break;

    case WM_KEYUP:
        break;

    case WM_VWPRINTF:
        return mwprintf( hwnd, (LPSTR)"%s", (LPSTR)wParam );

    default:
        return DefWindowProc(hwnd, uiMessage, wParam, lParam);
    }
    return 0L;
}


/*****************************************************************************\
* DebugScroll
*   
* Scrolls the debug window vertically
*
* Arguments:
*    HWND hwnd - handle to the debug window
*    PTXT pTxt - pointer to the text to scroll
*    INT n - number of lines to scroll
*
* Returns:
*    VOID
\*****************************************************************************/

PRIVATE VOID
DebugVScroll(
HWND hwnd,
PTXT pTxt,
INT  n
)
{
    RECT rect;
    INT  iMinPos, iMaxPos;

    GetScrollRange(hwnd, SB_VERT, (LPINT) &iMinPos, (LPINT) &iMaxPos);
    GetClientRect(hwnd, (LPRECT) &rect);
    rect.left += OFFSETX;
    rect.top  += OFFSETY;

    n = BOUND(pTxt->iTop + n, iMinPos, iMaxPos) - pTxt->iTop;
    if (n == 0)
        return;

    pTxt->iTop += n;
    ScrollWindow(hwnd, 0, -n * pTxt->Tdy, (LPRECT) &rect, (LPRECT) &rect);
    SetScrollPos(hwnd, SB_VERT, pTxt->iTop, TRUE);
}

/*****************************************************************************\
* DebugHScroll
*
* Performs the horizontal scroll calculations
*
* Arguments:
*    HWND hwnd - handle to the debug window
*    PTXT pTxt - pointer to the text to scroll
*    INT n - number of characters to scroll
*
* Returns:
*    VOID
\*****************************************************************************/

PRIVATE VOID
DebugHScroll(
HWND hwnd,
PTXT pTxt,
INT  n
)
{
    RECT rect;
    INT  iMinPos, iMaxPos;

    GetScrollRange (hwnd, SB_HORZ, (LPINT) &iMinPos, (LPINT) &iMaxPos);
    GetClientRect (hwnd, (LPRECT) & rect);
    rect.left += OFFSETX;
    rect.top  += OFFSETY;

    n = BOUND(pTxt->iLeft + n, iMinPos, iMaxPos) - pTxt->iLeft;
    if (n == 0)
        return;

    pTxt->iLeft += n;
    ScrollWindow(hwnd, -n * pTxt->Tdx, 0, (LPRECT) & rect, (LPRECT) & rect);
    SetScrollPos(hwnd, SB_HORZ, pTxt->iLeft, TRUE);
}

/*****************************************************************************\
* LinesInDebugWindow
*
* Calculates the number of lines in the debug window
*
* Arguments:
*    HWND hwnd - handle to the debug window
*
* Returns:
*    INT - number of lines in the debug window
\*****************************************************************************/

PRIVATE INT
LinesInDebugWindow (
HWND hwnd
)
{
    RECT CRect;
    PTXT pTxt;

    pTxt = *(HTXT)GetWindowLong(hwnd, 0);
    GetClientRect(hwnd, &CRect);
    if ( pTxt->Tdy == 0 ) {
        return 0;
    }
    return pTxt ? (CRect.bottom - CRect.top - OFFSETY) / pTxt->Tdy : 0;
}


/*****************************************************************************\
* CharsInDebugWindow
*
* Calculates the number of characters horizontally in the debug window
*
* Arguments:
*    HWND hwnd - handle to the debug window
*
* Returns:
*    INT - number of horizontal characters in the debug window
\*****************************************************************************/

PRIVATE INT
CharsInDebugWindow (
HWND hwnd
)
{
    RECT CRect;
    PTXT pTxt;

    pTxt = *(HTXT)GetWindowLong (hwnd, 0);
    GetClientRect(hwnd, (LPRECT) & CRect);
    if ( pTxt->Tdx == 0 ) {
        return 0;
    }
    return pTxt ? (CRect.right - CRect.left - OFFSETX) / pTxt->Tdx : 0;
}

PRIVATE INT
mwprintf(
    HWND hwnd,
    LPSTR format,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -