在c ++中更改Rectangle函数的边框颜色

时间:2015-06-13 17:49:01

标签: c++ winapi mfc masm32

如何在C ++ / MASM32中更改Rectangle function的边框颜色?

2 个答案:

答案 0 :(得分:2)

你可以试试这样的东西,我只是举个例子你可以根据你的要求改变它。

BOOL CRectangleDlg::OnEraseBkgnd(CDC* pDC)
{
    CBrush brushBlue(RGB(0, 0, 255));// inner color blue.
   CBrush* pOldBrush = pDC->SelectObject(&brushBlue);

   // create and select a thick, black pen
   CPen penBlack;
   penBlack.CreatePen(PS_SOLID, 3, RGB(255, 0, 0));// red color with width 3
   CPen* pOldPen = pDC->SelectObject(&penBlack);

   // get our client rectangle
   CRect rect;
   GetClientRect(rect);// pass rect coordinates here

   // shrink our rect 20 pixels in each direction
   rect.DeflateRect(20, 20);

   // draw a thick black rectangle filled with blue

   pDC->Rectangle(rect);

   // put back the old objects
   pDC->SelectObject(pOldBrush);
   pDC->SelectObject(pOldPen);

    return true;//CDialog::OnEraseBkgnd(pDC);
}

答案 1 :(得分:2)

这是一个快速的MASM32示例,我为您快速掀起:

include masm32rt.inc

RGB MACRO red, green, blue
    EXITM % blue SHL 16 + green SHL 8 + red
ENDM

WIDTH_MAIN_WINDOW       equ 550
HEIGHT_MAIN_WINDOW      equ 300

.data
szWndClsMain            BYTE    "RECT_MAIN", 0
szAppName               BYTE    "Rectangle Border Color Example", 0

.code
start:

    call    StartUp

    push    0
    call    ExitProcess

StartUp proc uses ebx
LOCAL   msg:MSG
LOCAL   hInst, hMain:DWORD
    push    NULL
    call    GetModuleHandle
    mov     hInst, eax    

    call    RegisterClasses

    ; ##### Coords to center window
    invoke  GetSystemMetrics, SM_CXSCREEN
    sub     eax, WIDTH_MAIN_WINDOW
    shr     eax, 1
    xchg    eax, ebx 

    invoke  GetSystemMetrics, SM_CYSCREEN
    sub     eax, HEIGHT_MAIN_WINDOW
    shr     eax, 1

    ; ##### Create main window
    invoke  CreateWindowEx,WS_EX_APPWINDOW or WS_EX_CONTROLPARENT, \
                            offset szWndClsMain, \
                            offset szAppName, \
                            WS_OVERLAPPED or WS_SYSMENU or WS_MINIMIZEBOX or WS_CLIPCHILDREN or WS_VISIBLE, \
                            ebx, eax, \
                            WIDTH_MAIN_WINDOW, HEIGHT_MAIN_WINDOW, \
                            HWND_DESKTOP, NULL, \
                            hInst, NULL
    mov     hMain, eax  

    .WHILE TRUE
        invoke GetMessage, ADDR msg,NULL,0,0
        .BREAK .IF (!eax)
        invoke IsDialogMessage, addr hMain, ADDR msg
        .IF eax ==FALSE
            invoke TranslateMessage, ADDR msg
            invoke DispatchMessage, ADDR msg
        .ENDIF
    .ENDW
    ret  
StartUp endp

RegisterClasses proc
LOCAL wc:WNDCLASSEX

    ; ##### Create main window
    invoke  RtlZeroMemory, addr wc, sizeof WNDCLASSEX
    mov     wc.cbSize, SIZEOF WNDCLASSEX       
    mov     wc.style, CS_HREDRAW or CS_VREDRAW 
    mov     wc.lpfnWndProc, OFFSET ProcWndMain    
    mov     wc.hInstance, esi                       
    mov     wc.hbrBackground, COLOR_3DFACE + 1 
    mov     wc.lpszClassName, OFFSET szWndClsMain
    invoke  RegisterClassEx, addr wc   

    ret
RegisterClasses endp

ProcWndMain proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
LOCAL   ps:PAINTSTRUCT, pOldPen, pNewPen:DWORD

    mov     eax,uMsg
    .if eax==WM_CREATE

    .elseif eax == WM_PAINT
        invoke  BeginPaint, hWin, addr ps

        invoke  CreatePen,PS_SOLID, 3, RGB(255, 0, 0)     ; Red
        mov     pNewPen, eax

        invoke  SelectObject, ps.hdc, eax
        mov     pOldPen, eax

        invoke  Rectangle, ps.hdc, 10, 10, 200, 200

        invoke  SelectObject, ps.hdc, pOldPen
        invoke  DeleteObject, pNewPen

        invoke  CreatePen,PS_SOLID, 3, RGB(0,0,255)      ; blue
        mov     pNewPen, eax

        invoke  SelectObject, ps.hdc, eax
        mov     pOldPen, eax

        invoke  Rectangle, ps.hdc, 250, 10, 445, 200

        invoke  SelectObject, ps.hdc, pOldPen
        invoke  DeleteObject, pNewPen        


        invoke EndPaint, hWin, addr ps

    .elseif eax==WM_CLOSE 
        invoke DestroyWindow,hWin
    .elseif eax==WM_DESTROY
        invoke PostQuitMessage,NULL
    .else
        invoke DefWindowProc,hWin,uMsg,wParam,lParam
        ret
    .endif
    xor    eax,eax
    ret
ProcWndMain endp        
end start

enter image description here