Немодальное окно в WinAPI

Тема в разделе "WASM.WIN32", создана пользователем Benzin, 7 окт 2007.

  1. Benzin

    Benzin Сергей

    Публикаций:
    0
    Регистрация:
    26 авг 2007
    Сообщения:
    41
    Адрес:
    St.Petersburg
    Все привет. Возник вопрос по программке в WinAPI. Пример на создание немодального окна, 3 кнопки, статик, и 3 контрола, на диалоге, диалог вызываетс при левом щелчке мыши, при правом исчезает. Диалог создается при помощи CreateWindow. Скомпилил, но когда щелкаю левой кнопкой сам диалог не появляется. Подскажите пожалуйста в чем проблема.

    Код (Text):
    1. #include <windows.h>
    2. #include "CreateDlg.h"
    3.  
    4. #define ID_STATIC 2000
    5. #define ID_BUTTON1 2000
    6. #define ID_BUTTON2 2001
    7. #define ID_BUTTON3 2003
    8. #define ID_HELP 2004
    9.  
    10.  
    11. BOOL RegClass(WNDPROC, LPCTSTR, UINT);
    12. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
    13. LRESULT CALLBACK DlgProc (HWND, UINT, WPARAM, LPARAM);
    14.  
    15. HWND CreateDlg(HWND, WORD*);
    16. HINSTANCE hInst;
    17. char szClassName[]="WindowAppClass";
    18.  
    19. //Главная функция
    20. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
    21. {
    22.     HWND hwnd;
    23.     MSG msg;
    24.     hInst=hInstance;
    25.    
    26.     if (!RegClass(WndProc, szClassName, COLOR_DESKTOP))
    27.         return FALSE;
    28.  
    29.     int wScreen=GetSystemMetrics(SM_CXSCREEN);
    30.     int hScreen=GetSystemMetrics(SM_CYSCREEN);
    31.  
    32.     hwnd = CreateWindow(szClassName, "Первое приложения. Диалог", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, wScreen, hScreen, 0, 0, hInstance, NULL);
    33.    
    34.     if (!hwnd)
    35.         return FALSE;
    36.  
    37.     while (GetMessage(&msg, 0, 0, 0))  
    38.     {TranslateMessage(&msg); DispatchMessage(&msg);}
    39.    
    40.     return msg.wParam;
    41. }
    42. //Функция регистрации класса
    43. BOOL RegClass(WNDPROC Proc, LPCTSTR szName, UINT brBackground)
    44. {
    45.     WNDCLASS w;    
    46.     w.style = CS_HREDRAW | CS_VREDRAW;
    47.     w.lpfnWndProc = Proc;
    48.     w.cbClsExtra = w.cbWndExtra = 0;
    49.     w.hInstance = hInst;
    50.     w.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    51.     w.hCursor = LoadCursor(NULL, IDC_ARROW);
    52.     w.hbrBackground = (HBRUSH)(brBackground + 1);
    53.     w.lpszMenuName = (LPCTSTR)NULL;
    54.     w.lpszClassName = szName;
    55.     return RegisterClass(&w);
    56.  
    57. }
    58.  
    59. //Фуккция окна
    60. LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    61. {
    62.     static WORD *pdlgtemplate;
    63.     static HWND hdlg;
    64.  
    65.     switch (msg)
    66.     {
    67.     case WM_CREATE:
    68.         {
    69.             pdlgtemplate=(PWORD)LocalAlloc(LPTR,2000);
    70.             hdlg = CreateDlg(hwnd, pdlgtemplate);
    71.             return 0;
    72.         }
    73.  
    74.     case WM_LBUTTONDOWN: {ShowWindow(hdlg,SW_SHOWNORMAL); return 0;}
    75.     case WM_RBUTTONDOWN: {ShowWindow(hdlg,SW_HIDE); return 0;} 
    76.    
    77.     case WM_DESTROY:
    78.         {
    79.             DestroyWindow(hdlg);
    80.             LocalFree(LocalHandle(pdlgtemplate));
    81.             PostQuitMessage(0);
    82.             return 0;
    83.         }
    84.     }  
    85.  
    86.     return DefWindowProc(hwnd, msg, wParam, lParam);
    87. }
    88.  
    89. HWND CreateDlg(HWND hwnd, WORD *pdlgtemplate)
    90. {
    91.     char const caption[]= " Изменение режима работы приложения";
    92.     char const stattxt[]= "Сейчас следует:";
    93.     char const modeoff[]= "завершить работу приложения";
    94.     char const modedat[]= "перезагрузить данные";
    95.     char const modepsw[]= "сменить имя пользователя";
    96.  
    97.    
    98.     WORD *p;
    99.     p=pdlgtemplate;
    100.    
    101.     int cxChar, cyChar;
    102.     {
    103.         TEXTMETRIC tm;
    104.         HDC hdc;
    105.         hdc = GetDC(hwnd);
    106.        
    107.         GetTextMetrics(hdc, &tm);
    108.         ReleaseDC(hwnd, hdc);
    109.        
    110.         cxChar = tm.tmAveCharWidth+1;
    111.         cyChar = tm.tmHeight + tm.tmExternalLeading;
    112.     }
    113.  
    114.     DWORD dlgunit = GetDialogBaseUnits();
    115.     int dlgwunit = LOWORD(dlgunit);
    116.     int dlghunit = HIWORD(dlgunit);
    117.    
    118.     cxChar = cxChar*4/dlgwunit;
    119.     cyChar = cyChar*8/dlghunit;
    120.  
    121.     int wDlg, hDlg, wItem, hItem, left, top;
    122.  
    123.    
    124.     DWORD lStyle = DS_CENTER | DS_MODALFRAME | WS_POPUPWINDOW | WS_CAPTION;
    125.  
    126.     wDlg = lstrlen(caption)*cxChar;
    127.     hDlg = cyChar*10;
    128.     DlgTemplate(p, lStyle, 7, 0, 0, wDlg, hDlg, (LPSTR)caption);
    129.  
    130.     //1
    131.     hItem = cyChar;
    132.     top=left=hItem/2;
    133.     hItem+=left;
    134.     wItem = (wDlg-left-left);
    135.     lStyle = WS_CHILD | WS_VISIBLE | BS_GROUPBOX | WS_TABSTOP;
    136.     DlgItemTemplate(p, lStyle, left, top, wItem, 4*hItem+left, ID_STATIC, (LPSTR)"button", (LPSTR)stattxt);
    137.  
    138.     //2
    139.     wItem = lstrlen(modeoff)*cxChar+10;
    140.     top+=hItem;
    141.     lStyle = BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP;
    142.     DlgItemTemplate(p, lStyle, hItem, top, wItem, hItem, ID_BUTTON1, (LPSTR)"button", (LPSTR)modeoff);
    143.  
    144.     //3
    145.     wItem = lstrlen(modedat)*cxChar+10;
    146.     top+=hItem;
    147.     lStyle = BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE;
    148.     DlgItemTemplate(p, lStyle, hItem, top, wItem, hItem, ID_BUTTON2, (LPSTR)"button", (LPSTR)modedat);
    149.  
    150.     //4
    151.     wItem = lstrlen(modepsw)*cxChar+10;
    152.     top+=hItem;
    153.     lStyle = BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE;
    154.     DlgItemTemplate(p, lStyle, hItem, top, wItem, hItem, ID_BUTTON3, (LPSTR)"button", (LPSTR)modepsw);
    155.  
    156.     //5
    157.     wItem = (wDlg-left-left-hItem-hItem)/3;
    158.     top+=hItem+hItem/2+left;   
    159.     lStyle = BS_DEFPUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP;
    160.     DlgItemTemplate(p, lStyle, left, top, wItem, hItem, IDOK, (LPSTR)"button", (LPSTR)"Да");
    161.  
    162.     //6
    163.     lStyle = BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP;
    164.     DlgItemTemplate(p, lStyle, left+wItem+hItem, top, wItem, hItem, IDCANCEL, (LPSTR)"button", (LPSTR)"Отмена");
    165.  
    166.     //7
    167.     DlgItemTemplate(p, lStyle, left+wItem+hItem+wItem+hItem, top, wItem, hItem, ID_HELP, (LPSTR)"button", (LPSTR)"Справка");
    168.  
    169.     //*****
    170.     HWND hdlg;
    171.     hdlg = CreateDialogIndirect (hInst, (LPDLGTEMPLATE)pdlgtemplate, hwnd, (DLGPROC)DlgProc);    
    172.  
    173.     return hdlg;
    174. }
    175.  
    176. LRESULT CALLBACK DlgProc (HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam)
    177. {
    178.     switch(msg)
    179.     {
    180.     case WM_COMMAND:
    181.         {
    182.             switch (LOWORD(wParam))
    183.             {
    184.             case IDOK:
    185.             case IDCANCEL:
    186.                 { EndDialog(hdlg, TRUE);
    187.                 return TRUE;
    188.                 }
    189.             }
    190.         }
    191.     }
    192.     return FALSE;
    193. }
    CreateDlg.h
    Код (Text):
    1. LPWORD lpwAlign(LPWORD);
    2. int nCopyAnsiToWideChar(LPWORD, LPSTR);
    3. void DlgTemplate(PWORD& p, DWORD lStyle, int items, int x, int y, int cx, int cy, LPSTR txt);
    4. void DlgItemTemplate(PWORD& p, DWORD lStyle, int x, int y, int cx, int cy, WORD id, LPSTR classname, LPSTR txt);
    5.  
    6.  
    7. LPWORD lpwAlign(LPWORD lpln)
    8. {
    9.     ULONG ul=(ULONG)lpln;
    10.     ul+=3; ul>>=2; ul<<=2; return (LPWORD)ul;
    11. }
    12.  
    13.  
    14. int nCopyAnsiToWideChar(LPWORD lpWCStr, LPSTR lpAnsiln)
    15. {
    16.     int cchAnsi=lstrlen(lpAnsiln);
    17.     return MultiByteToWideChar(GetACP(), MB_PRECOMPOSED, lpAnsiln, cchAnsi, lpWCStr, cchAnsi) + 1;
    18. }
    19.  
    20. void DlgTemplate(PWORD& p, DWORD lStyle, int items, int x, int y, int cx, int cy, LPSTR txt)
    21. {
    22.     *p++=LOWORD(lStyle);
    23.     *p++=HIWORD(lStyle);
    24.     *p++=0;
    25.     *p++=0;
    26.     *p++=items;
    27.     *p++=x;
    28.     *p++=y;
    29.     *p++=cx;
    30.     *p++=cy;
    31.     *p++=0;
    32.     *p++=0;
    33.    
    34.     int nchar=nCopyAnsiToWideChar(p,TEXT(txt));
    35.     *p++=nchar;
    36.     p=lpwAlign((LPWORD)p);
    37. }
    38.  
    39. void DlgItemTemplate(PWORD& p, DWORD lStyle, int x, int y, int cx, int cy, WORD id, LPSTR classname, LPSTR txt)
    40. {
    41.     *p++=LOWORD(lStyle);
    42.     *p++=HIWORD(lStyle);
    43.     *p++=0;
    44.     *p++=0;
    45.     *p++=x;
    46.     *p++=y;
    47.     *p++=cx;
    48.     *p++=cy;
    49.     *p++=id;
    50.    
    51.     int nchar=nCopyAnsiToWideChar(p,TEXT(classname));
    52.    
    53.     p+=nchar;
    54.    
    55.     if(lstrlen(txt)>0)
    56.         nchar=nCopyAnsiToWideChar(p, TEXT(txt));
    57.     else nchar=nCopyAnsiToWideChar(p, TEXT(""));
    58.    
    59.     p+=nchar;
    60.     p+=0;
    61.     p=lpwAlign((LPWORD)p);
    62. }