Dobrý deň. Začínam pracovať s Win32 a pri štúdiu som narazil na problém. Vytvoril som si jednoduchý program:
#include <windows.h>
#include "resource.h"
const char g_szClassName[] = "myWindowClass";
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_CREATE:
{
HMENU hMenu, hSubMenu;
HICON hIcon, hIconSm;
hMenu = CreateMenu();
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "Koniec");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "Súbor");
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO, "Automatické vypnutie PC");
AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO2, "Vzdialené vypnutie PC");
AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO3, "Štruktúra zložiek");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "Menu");
SetMenu(hwnd, hMenu);
hIcon = LoadImage(NULL, "ikona.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
if(hIcon)
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
else
MessageBox(hwnd, "Nemôžem načítať veľkú ikonu!", "Chyba", MB_OK | MB_ICONERROR);
hIconSm = LoadImage(NULL, "ikona.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
if(hIconSm)
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
else
MessageBox(hwnd, "Nemôžem načítať malú ikonu!", "Chyba", MB_OK | MB_ICONERROR);
}
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_FILE_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
case ID_STUFF_GO:
WinExec("cmd.exe /C shutdown -s -t 30", SW_SHOW);
break;
case ID_STUFF_GO2:
WinExec("cmd.exe /C shutdown -i", SW_SHOW);
break;
case ID_STUFF_GO3:
WinExec("cmd.exe /C tree", SW_SHOW);
break;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
wc.lpszClassName = g_szClassName;
wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Projekt1",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 600, 400,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
Problém spočíva v tom, že po skompilovaní sa mi otvorí hotové okno ale aj konzola a ak si v menu napríklad nakliknem možnosť "Štruktúra zložiek"
AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO3, "Štruktúra zložiek");
výsledok spusteného príkazu sa vypíše do konzoly. Čo musím urobiť, aby sa to vypísalo rovno do okna?
Za odpoveď vopred všetkým ďakujem.