Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

WinApi Okna dialogowe niemodalne - niepoprawne działanie.

Ostatnio zmodyfikowano 2021-06-11 20:56
Autor Wiadomość
sekretarz11
Temat założony przez niniejszego użytkownika
WinApi Okna dialogowe niemodalne - niepoprawne działanie.
» 2021-06-10 14:52:51
Witam. Mam problem z programem i poprawnym działaniem dialogów niemodalnych. Chyba nie do końca zrozumiałem kurs na ten temat.

Chciałem stworzyć aplikację która po naciśnięciu odpowiednich przycisków na dialogach niemodalnych wykona jakąś czynność.
W moim przypadku po nacisnieciu 1, miał narysować kółko,
a po 2,3,4 wyświetlić jakąś informację.

Próbowałem już wielu kombinacji lecz dostaje albo błędy że "ret" nie jest int, tylko HWND, że CreateDialog nie może być int tylko HWND,  albo że nie można przyrównać HWND z int i wiele innych.

Lecz nie potrafię połączyć obsługi przycisków z wyświetlaniem np. tego kółka w programie głównym.

Z góry dziękuję za pomoc.


Stworzyłem plik: pasek.h
C/C++
#define IDD_PASEK 200
#define IDC_PRZYC1 201
#define IDC_PRZYC2 202
#define IDC_PRZYC3 203
#define IDC_PRZYC4 204

plik pasek.rc
C/C++
#include <windows.h>
#include "pasek.h"
IDD_PASEK DIALOGEX 0, 0, 98, 100
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION
EXSTYLE WS_EX_TOOLWINDOW
CAPTION "Pasek "
FONT 8, "MS Sans Serif"
{
   
PUSHBUTTON "&1", IDC_PRZYC1, 7, 7, 84, 14
   
PUSHBUTTON "&2", IDC_PRZYC2, 7, 31, 84, 14
   
PUSHBUTTON "&3", IDC_PRZYC3, 7, 55, 84, 14
   
PUSHBUTTON "&4", IDC_PRZYC4, 7, 79, 84, 14
   

oraz plik main.cpp

C/C++
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include "pasek.h"


/*  Declare Windows procedure  */
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
BOOL CALLBACK DlgProc( HWND, UINT, WPARAM, LPARAM );

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T( "CodeBlocksWindowsApp" );

HWND hPasek, ret;

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 = WndProc; /* 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 */
   
_T( "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 */
   
800, /* 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 */
   
);
   
HWND hPrzyc = CreateWindowEx( 0, "BUTTON", "Menu", WS_CHILD | WS_VISIBLE, 10, 10, 760, 25, hwnd, NULL, hThisInstance, NULL );
   
/* 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;
   
}

void RysujKolo( HWND hwnd )
{
   
//pobieranie kontekstu urzadzenia
   
HDC hdcOkno = GetDC( hwnd );
   
//inicjowanie pedzli
   
HBRUSH PedzelZiel, Pudelko;
   
//stworzenie kolorowych pedzli
   
PedzelZiel = CreateSolidBrush( 0x00FF00 );
   
Pudelko =( HBRUSH ) SelectObject( hdcOkno, PedzelZiel );
   
Ellipse( hdcOkno, 10, 10, 510, 510 );
   
//niszczenie pedzli
   
SelectObject( hdcOkno, Pudelko );
   
DeleteObject( PedzelZiel );
   
ReleaseDC( hwnd, hdcOkno );
}

int WINAPI WinDlg( HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil )
{
   
HWND hwnd;
   
MSG msg;
   
   
   
while( GetMessage( & msg, NULL, 0, 0 ) )
   
{
       
if( !IsDialogMessage( hPasek, & msg ) )
       
{
           
TranslateMessage( & msg );
           
DispatchMessage( & msg );
       
}
       
    }
   
return msg.wParam;
}



// Procedura okna
LRESULT CALLBACK WndProc( HWND hwnd, UINT mesg, WPARAM wParam, LPARAM lParam )
{
   
switch( mesg )
   
{
       
   
case WM_COMMAND:
       
{
           
HWND ret = CreateDialog( GetModuleHandle( NULL ), MAKEINTRESOURCE( 200 ), hwnd, NULL );
           
ShowWindow( ret, SW_SHOW );
           
           
if( ret == 201 )
           
{
               
RysujKolo( hwnd );
               
break;
           
}
           
           
else if( ret == 202 )
           
{
               
MessageBox( hwnd, "2", "2", MB_ICONINFORMATION );
               
break;
           
}
           
           
           
else if( ret == 203 )
           
{
               
MessageBox( hwnd, "3", "3", MB_ICONINFORMATION );
               
break;
           
}
           
           
else if( ret = 204 )
           
{
               
MessageBox( hwnd, "4", "4", MB_ICONINFORMATION );
               
break;
           
}
        }
       
       
       
       
       
       
       
       
break;
       
   
case WM_DESTROY:
       
PostQuitMessage( 0 );
       
break;
       
       
default:
       
return DefWindowProc( hwnd, mesg, wParam, lParam );
   
}
   
return 0;
}

P-178756
pekfos
» 2021-06-10 17:40:12
Próbujesz użyć dialogu niemodalnego jakby to był dialog modalny. Po utworzeniu dialogu niemodalnego okno główne funkcjonuje niezależnie. Nie masz zwracania kodu liczbowego. Interakcję z dialogiem powinieneś obsługiwać w procedurze dialogu, której nie masz (ostatni argument do CreateDialog()).
P-178759
sekretarz11
Temat założony przez niniejszego użytkownika
» 2021-06-11 19:28:42
Dzięki za pomoc, poprawiłem kod, natomiast nie rozumiem komunikacji między DlgProc a głównym procesem. Po naciśnięciu przycisku koło się rysuje ale niestety nie w głównym oknie tylko w oknie narzędzi. Jak to zmienić? Z góry dzięki za pomoc.

PS. Na dole umieszczam zrzut ekranu jak działa program.

C/C++
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include "pasek.h"


/*  Declare Windows procedure  */
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
BOOL CALLBACK DlgProc( HWND, UINT, WPARAM, LPARAM );

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T( "CodeBlocksWindowsApp" );

HWND hPasek;

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 = WndProc; /* 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 */
   
_T( "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 */
   
800, /* 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 */
   
);
   
HWND hPrzyc = CreateWindowEx( 0, "BUTTON", "Menu Narzêdzi", WS_CHILD | WS_VISIBLE, 10, 10, 760, 25, hwnd, NULL, hThisInstance, NULL );
   
/* 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;
   
}

void RysujKolo( HWND hwnd )
{
   
//pobieranie kontekstu urzadzenia
   
HDC hdcOkno = GetDC( hwnd );
   
//inicjowanie pedzli
   
HBRUSH PedzelZiel, PedzelCzerw, PedzelNieb, PedzelZolty, PedzelRoz, Pudelko;
   
HPEN Piornik;
   
//stworzenie kolorowych pedzli
   
PedzelZiel = CreateSolidBrush( 0x00FF00 );
   
PedzelCzerw = CreateSolidBrush( 0x0000FF );
   
PedzelNieb = CreateSolidBrush( 0xFF0000 );
   
PedzelZolty = CreateSolidBrush( 0x00FFFF );
   
PedzelRoz = CreateSolidBrush( 0xFF00FF );
   
Pudelko =( HBRUSH ) SelectObject( hdcOkno, PedzelZiel );
   
Ellipse( hdcOkno, 10, 10, 510, 510 );
   
//niszczenie pedzli
   
SelectObject( hdcOkno, Pudelko );
   
DeleteObject( PedzelZiel );
   
DeleteObject( PedzelCzerw );
   
DeleteObject( PedzelNieb );
   
DeleteObject( PedzelZolty );
   
DeleteObject( PedzelRoz );
   
ReleaseDC( hwnd, hdcOkno );
}

int WINAPI WinDlg( HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil )
{
   
HWND hwnd;
   
MSG msg;
   
   
   
while( GetMessage( & msg, NULL, 0, 0 ) )
   
{
       
if( !IsDialogMessage( hPasek, & msg ) )
       
{
           
TranslateMessage( & msg );
           
DispatchMessage( & msg );
       
}
       
    }
   
return msg.wParam;
}


LRESULT CALLBACK WndProc( HWND hwnd, UINT mesg, WPARAM wParam, LPARAM lParam )
{
   
switch( mesg )
   
{
   
case WM_COMMAND:
       
{
           
HWND ret = CreateDialog( GetModuleHandle( NULL ), MAKEINTRESOURCE( 200 ), hwnd, DlgProc );
           
ShowWindow( ret, SW_SHOW );
           
       
}
       
break;
       
   
case WM_DESTROY:
       
PostQuitMessage( 0 );
       
break;
       
       
default:
       
return DefWindowProc( hwnd, mesg, wParam, lParam );
   
}
   
   
return 0;
}

// Procedura dialogowa
BOOL CALLBACK DlgProc( HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam )
{
   
switch( Msg )
   
{
   
case WM_COMMAND:
       
{
           
// reakcja na przyciski
           
switch( LOWORD( wParam ) )
           
{
           
case 204:
               
{
                   
RysujKolo( hwnd );
                   
break;
               
}
               
           
case 202: break;
           
}
        }
       
break;
       
       
default: return FALSE;
   
}
   
   
return TRUE;
}

Program
Program
P-178766
pekfos
» 2021-06-11 20:39:19
Pierwszy argument w procedurze okna (dialogu) to uchwyt na te właśnie okno. Żeby się odwoływać do innych okien, musisz sobie sam zorganizować jakiś mechanizm. Na przykład możesz zapisać sobie uchwyt do okna głównego w zmiennej globalnej.
P-178768
sekretarz11
Temat założony przez niniejszego użytkownika
» 2021-06-11 20:56:27
Dzięki wielkie. Wreszcie zrozumiałem i działa program prawidłowo. ;)
P-178769
« 1 »
  Strona 1 z 1