2013-11-09 36 views
4

我已經對某些按鈕控件進行了分類,因爲我自己繪製了整個UI(到對話框的hdc)。 這是爲了避免閃爍,其意圖是所有的繪圖都是通過一個單獨的memDC完成的 - 從而防止了UI的交錯更新。因此,我將所有內容都繪製到對話框的背景上,然後將某些按鈕放置在應對鼠標事件作出反應的UI區域上。到現在爲止還挺好。或者我想。使用GCC和MSVC編譯時的不同行爲

我使用下面的WndProc對按鈕進行了分類,期望Windows除了繪圖之外將按照正常方式執行所有操作。創建和子類

LRESULT CALLBACK invisibleBtnProc(HWND hwndBtn, UINT uMsg, WPARAM wParam, LPARAM lParam) 
{ 
    long oldProc = GetWindowLong(hwndBtn, GWL_USERDATA); 

    switch (uMsg) 
    { 
     case WM_PAINT: 
      ValidateRect(hwndBtn, NULL); 
      return 0; 

     case WM_ERASEBKGND: 
      return 1; 
    } 
    return CallWindowProc((WNDPROC)oldProc, hwndBtn, uMsg, wParam, lParam); 
} 

按鈕用下面的代碼:

for (i=0; i<n; i++) 
{ 
    btn = CreateWindow(WC_BUTTON, L"", WS_VISIBLE|WS_CHILD, 0,0,0,0, hwndDlg, (HMENU)(firstBigBtnId+i), hInst, NULL); 
    long btnProcCur = GetWindowLong(btn, GWL_WNDPROC); 
    SetWindowLong(btn, GWL_USERDATA, btnProcCur); 

    SetWindowLong(btn, GWL_WNDPROC, (long) invisibleBtnProc); 
} 

當我建立了這個代碼的MinGW &代碼::塊,它完美的作品。 (無論是在調試還是發佈版本中)

不幸的是,當使用MSVC &構建VS2010時,我觀察到不同的行爲。調試模式構建是可以的,但發佈版本不是。當其中一個不可見的按鈕被點擊時,系統正在繪製它,掩蓋了底層的「按鈕」。

我有一個很大的WMF(emf?我忘記了)需要繪製 - 它非常緩慢,並且在窗口大小調整時會產生閃爍,因爲那些想知道爲什麼自定義繪製所有東西的方法。

這裏是我所看到的:

enter image description here

注意,在此之前,我試圖點擊最左邊的按鈕是不可見的 - 就像右邊的一個。只有在點擊它時,窗口才會決定繪製它。 調整父窗口大小 - (觸發對話框的InvalidateRect調用的對話框)將刪除錯誤的繪圖。再次單擊該按鈕會使其被繪製。

任何想法,我犯了一個錯誤的想法?

編輯:添加下面的代碼爲SCCCE(這顯示,當與GCC調試&發佈建立在同一個不受歡迎的行爲,原本計劃只在調試版本顯示)

#include <windows.h> 

/* Declare Windows procedure */ 
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); 

RECT btnRect; 
const int btnSize = 150; 
const int btnId = 1000; 
HINSTANCE hInst; 


/* Make the class name into a global variable */ 
char szClassName[ ] = "CodeBlocksWindowsApp"; 

int WINAPI WinMain (HINSTANCE hThisInstance, 
        HINSTANCE hPrevInstance, 
        LPSTR lpszArgument, 
        int nCmdShow) 
{ 
    HWND hwnd;    /* This is the handle for our window */ 
    MSG messages;   /* Here messages to the application are saved */ 
    WNDCLASSEX wincl;  /* Data structure for the windowclass */ 

    /* The Window structure */ 
    wincl.hInstance = hThisInstance; 
    wincl.lpszClassName = szClassName; 
    wincl.lpfnWndProc = WindowProcedure;  /* This function is called by windows */ 
    wincl.style = CS_DBLCLKS;     /* Catch double-clicks */ 
    wincl.cbSize = sizeof (WNDCLASSEX); 

    /* Use default icon and mouse-pointer */ 
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); 
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); 
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW); 
    wincl.lpszMenuName = NULL;     /* No menu */ 
    wincl.cbClsExtra = 0;      /* No extra bytes after the window class */ 
    wincl.cbWndExtra = 0;      /* structure or the window instance */ 
    /* Use Windows's default colour as the background of the window */ 
    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 */ 
      "Code::Blocks Template Windows App",  /* Title Text */ 
      WS_OVERLAPPEDWINDOW, /* default window */ 
      CW_USEDEFAULT,  /* Windows decides the position */ 
      CW_USEDEFAULT,  /* where the window ends up on the screen */ 
      544,     /* The programs width */ 
      375,     /* 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, nCmdShow); 

    /* 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 invisibleBtnProc(HWND hwndBtn, UINT uMsg, WPARAM wParam, LPARAM lParam) 
{ 
    long oldProc = GetWindowLong(hwndBtn, GWL_USERDATA); 

    switch (uMsg) 
    { 
     case WM_PAINT: 
      ValidateRect(hwndBtn, NULL); 
      return 0; 

     case WM_ERASEBKGND: 
      return 1; 
    } 
    return CallWindowProc((WNDPROC)oldProc, hwndBtn, uMsg, wParam, lParam); 
} 

void onSize(HWND hwnd, WPARAM wParam, LPARAM lParam) 
{ 
    RECT mRect; 
    GetClientRect(hwnd, &mRect); 
    btnRect.left = (mRect.right - btnSize)/2; 
    btnRect.top = (mRect.bottom - btnSize)/2; 
    btnRect.right = btnRect.left + btnSize; 
    btnRect.bottom = btnRect.top + btnSize; 

    HWND btn; 
    btn = GetDlgItem(hwnd, btnId); 
    MoveWindow(btn, btnRect.left, btnRect.top, btnSize, btnSize, false); 

    InvalidateRect(hwnd, NULL, false); 
} 

void onPaint(HWND hwnd, WPARAM wParam, LPARAM lParam) 
{ 
    HDC hdc; 
    PAINTSTRUCT ps; 
    HBRUSH bkBrush, redBrush; 
    RECT mRect; 

    GetClientRect(hwnd, &mRect); 

    hdc = BeginPaint(hwnd, &ps); 

     bkBrush = CreateSolidBrush(RGB(51,51,51)); 
     redBrush = CreateSolidBrush(RGB(255,0,0)); 
     FillRect(hdc, &mRect, bkBrush); 
     FillRect(hdc, &btnRect, redBrush); 
     DeleteObject(bkBrush); 
     DeleteObject(redBrush); 

    EndPaint(hwnd, &ps); 
} 

/* This function is called by the Windows function DispatchMessage() */ 
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    switch (message)     /* handle the messages */ 
    { 
    case WM_CREATE: 
      HWND tmp; 
      tmp = CreateWindow("Button", "Press Me", WS_VISIBLE|WS_CHILD, 0,0,0,0, hwnd, (HMENU)btnId, hInst, NULL); 
      long oldProc; 
      oldProc = GetWindowLong(tmp, GWL_WNDPROC); 
      SetWindowLong(tmp, GWL_USERDATA, oldProc); 
      SetWindowLong(tmp, GWL_WNDPROC, (long)invisibleBtnProc); 
      return 0; 

     case WM_DESTROY: 
      PostQuitMessage (0);  /* send a WM_QUIT to the message queue */ 
      break; 

     case WM_SIZE: 
      onSize(hwnd, wParam, lParam); 
      return 0; 

     case WM_PAINT: 
      onPaint(hwnd, wParam, lParam); 
      return 0; 

     case WM_COMMAND: 
      switch (LOWORD(wParam)) 
      { 
       case btnId: 
        MessageBeep(MB_ICONEXCLAMATION); 
        break; 
      } 
      return 0; 

     default:      /* for messages that we don't deal with */ 
      return DefWindowProc (hwnd, message, wParam, lParam); 
    } 

    return 0; 
} 
+1

你用'GCC -Wall -Wextra'編譯和改進代碼,直到你有沒有報警?你有沒有使用最近的GCC(例如[GCC 4.8](http://gcc.gnu.org/gcc-4.8/)...)?您是否考慮過使用跨平臺工具包(如[Qt](http://qt.digia.com/))? –

+0

VS的調試和發佈版本之間的一個區別是未初始化的變量有不同的值(一個是零,另一個是非零,我忘記了) - 這可能是原因嗎? –

+0

@BasileStarynkevitch - 我用'g ++ -Wall -Wextra'。剩餘的唯一警告是未使用的參數(主要是wParam和lParam)。我'gcc -dumpversion'返回'4.7.2'(與'g ++ -dumpversion'一樣)。上帝不!我希望這個項目很小。我不喜歡gtk和Qt。 wxWidgets經常製作一個2MB的程序,我可以在100kb以下做(使用我自己的C++類來包裝win32 api和窗口對象 - 提供類似或相同的界面和靈活性)雖然有好的暗示建議,但在這種情況下不適用。 – enhzflep

回答

3

設置BS_OWNERDRAW風格告訴Windows它不會畫出按鈕本身,但是你要對此負責。這就是訣竅。

你沒有太多需要改變。只需用這種風格創建按鈕。

tmp = CreateWindow("Button", "Press Me", WS_VISIBLE|WS_CHILD|BS_OWNERDRAW, 0,0,0,0, hwnd, (HMENU)btnId, hInst, NULL); 

然後在你的invisibleBtnProc您可以添加

case WM_DRAWITEM: 
    ValidateRect(hwndBtn, NULL); 
    return TRUE;