Så er jeg stødt på et par problemer.
Min kode:
#define _WIN32_IE 0x0501
#include <windows.h>
#include <commctrl.h>
#include "resource.h"
enum WindowsId
{
pcListBox = 256,
iPodListBox,
toolbar,
statusbar,
};
void DisplayError(char* errorString);
void initMenu(HWND hWnd);
void initFonts(HWND hWnd);
void initControls(HWND hWnd);
void initToolbar(HWND hWnd);
void onSize(HWND hWnd, int xSize, int ySize);
bool isOver(int, int, RECT);
HINSTANCE hInstance;
HWND pcListHandle, iPodListHandle, toolbarHandle, statusbarHandle;
int pcListEndX = 305;
LRESULT CALLBACK WindowProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
{
hInstance = hInst;
INITCOMMONCONTROLSEX cc;
memset(&cc, 0, sizeof(cc));
cc.dwSize = sizeof(cc);
cc.dwICC = 0xFFFFFFFF;
InitCommonControlsEx(&cc);
WNDCLASSEX wc;
HWND MainWindow;
MSG msg;
memset(&wc, 0, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.hInstance = hInst;
wc.lpszClassName = "WindowClass";
wc.lpfnWndProc = WindowProc;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
if(!RegisterClassEx(&wc))
return 0;
MainWindow = CreateWindowEx(0, "WindowClass", "iSync", WS_OVERLAPPEDWINDOW, 0, 0, 800, 700, NULL, NULL, hInst, 0);
if(MainWindow == NULL)
{
DisplayError("Kunne ikke oprette hovedvinduet");
return 0;
}
ShowWindow(MainWindow, nShow);
UpdateWindow(MainWindow);
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch(nMsg)
{
case WM_CREATE:
{
initMenu(hWnd);
initControls(hWnd);
initFonts(hWnd);
initToolbar(hWnd);
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_SIZE:
onSize(hWnd, LOWORD(lParam), HIWORD(lParam));
break;
case WM_MOUSEMOVE:
{
RECT controlPos;
GetWindowRect(pcListHandle, &controlPos);
if(isOver(LOWORD(lParam), HIWORD(lParam), controlPos))
SendMessage(statusbarHandle, SB_SETTEXT, 0, (LPARAM)"Test");
else
SendMessage(statusbarHandle, SB_SETTEXT, 0, (LPARAM)"");
}
break;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case ID_FILE_EXIT:
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
}
}
break;
case WM_GETMINMAXINFO:
{
MINMAXINFO *MinMaxInfo = (MINMAXINFO*)lParam;
MinMaxInfo->ptMinTrackSize.x = 800;
MinMaxInfo->ptMinTrackSize.y = 700;
}
break;
}
return DefWindowProc(hWnd, nMsg, wParam, lParam);
}
void DisplayError(char* errorString)
{
MessageBox(NULL, errorString, "Fejl!", MB_OK | MB_ICONERROR);
return;
}
void initMenu(HWND hWnd)
{
HMENU hMenu, hSubMenuFile, hSubMenuHelp;
hMenu = CreateMenu();
hSubMenuFile = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenuFile, "&Filer");
AppendMenu(hSubMenuFile, MF_STRING, ID_FILE_EXIT, "&Afslut");
hSubMenuHelp = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenuHelp, "&Hjælp");
AppendMenu(hSubMenuHelp, MF_STRING, ID_HELP_HELP, "&iSync-Hjælp");
AppendMenu(hSubMenuHelp, MF_STRING, ID_HELP_ABOUT, "&Om iSync");
if(SetMenu(hWnd, hMenu) == 0)
DisplayError("Kunne ikke oprette menuen");
}
void initControls(HWND hWnd)
{
toolbarHandle = CreateWindowEx(0,
TOOLBARCLASSNAME,
NULL,
WS_CHILD | TBSTYLE_WRAPABLE | WS_VISIBLE,
0,0,0,0,
hWnd,
(HMENU)toolbar,
hInstance,
NULL);
pcListHandle = CreateWindowEx(WS_EX_CLIENTEDGE,
WC_LISTVIEW,
"",
WS_GROUP | WS_CHILD | WS_TABSTOP | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | LVS_REPORT,
150, 55, 620, 250,
hWnd,
(HMENU)pcListBox,
hInstance,
NULL);
iPodListHandle = CreateWindowEx(WS_EX_CLIENTEDGE,
WC_LISTVIEW,
"",
WS_GROUP | WS_CHILD | WS_TABSTOP | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL,
150, 360, 620, 270,
hWnd,
(HMENU)iPodListBox,
hInstance,
NULL);
statusbarHandle = CreateWindowEx(0,
STATUSCLASSNAME,
NULL,
WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP,
0,0,0,0,
hWnd,
(HMENU)statusbar,
hInstance,
NULL);
int statwidths[] = {200, -1};
SendMessage(statusbarHandle, SB_SETPARTS, sizeof(statwidths)/sizeof(int), (LPARAM) statwidths);
SendMessage(statusbarHandle, SB_SETTEXT, 0, (LPARAM)"Velkommen til iSync");
}
void initFonts(HWND hWnd)
{
HFONT arial;
HWND List = GetDlgItem(hWnd, pcListBox);
long lfHeight;
HDC hdc = GetDC(List);
lfHeight = -MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 75);
ReleaseDC(hWnd, hdc);
arial = CreateFont(lfHeight, 0, 0, 0, 0, false, 0, 0, 0, 0, 0, 0, 0, "Arial");
if(arial)
{
SendDlgItemMessage(hWnd, pcListBox, WM_SETFONT, (WPARAM) arial, true);
}
else
DisplayError("Kunne ikke oprette font");
}
void onSize(HWND hWnd, int xSize, int ySize)
{
pcListEndX = 55+ySize-395;
HWND wndControl;
wndControl = GetDlgItem(hWnd, pcListBox);
if(pcListEndX >= 400)
{
MoveWindow(wndControl, 150, 55, xSize-160, 400, false);
wndControl = GetDlgItem(hWnd, iPodListBox);
MoveWindow(wndControl, 150, 505, xSize-160, ySize-545, false);
}
else
{
MoveWindow(wndControl, 150, 55, xSize-160, ySize-395, false);
wndControl = GetDlgItem(hWnd, iPodListBox);
MoveWindow(wndControl, 150, pcListEndX+55, xSize-160, ySize-(pcListEndX+95), false);
}
SendMessage(toolbarHandle, TB_AUTOSIZE, 0, 0);
SendMessage(statusbarHandle, WM_SIZE, 0, 0);
}
void initToolbar(HWND hWnd)
{
SendMessage(toolbarHandle, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
TBBUTTON tbb[3];
TBADDBITMAP tbap;
tbap.hInst = HINST_COMMCTRL;
tbap.nID = IDB_STD_SMALL_COLOR;
SendMessage(toolbarHandle, TB_ADDBITMAP, 0, (LPARAM) &tbap);
memset(&tbb, 0, sizeof(tbb));
tbb[0].iBitmap = STD_FILENEW;
tbb[0].fsState = TBSTATE_ENABLED;
tbb[0].fsStyle = TBSTYLE_BUTTON;
tbb[0].idCommand = ID_FILE_EXIT;
tbb[1].iBitmap = STD_FILEOPEN;
tbb[1].fsState = TBSTATE_ENABLED;
tbb[1].fsStyle = TBSTYLE_BUTTON;
tbb[1].idCommand = ID_HELP_ABOUT;
tbb[2].iBitmap = STD_FILESAVE;
tbb[2].fsState = TBSTATE_ENABLED;
tbb[2].fsStyle = TBSTYLE_BUTTON;
tbb[2].idCommand = ID_HELP_HELP;
SendMessage(toolbarHandle, TB_ADDBUTTONS, sizeof(tbb)/sizeof(TBBUTTON), (LPARAM) &tbb);
SendMessage(toolbarHandle, TB_AUTOSIZE, 0, 0);
}
bool isOver(int x, int y, RECT overRect)
{
if(x >= overRect.left && x <= overRect.right && y >= overRect.top && y <= overRect.bottom)
return true;
else
return false;
}
Jeg har netop ændret mine to listboxes til listviews pga. man ikke kan lave kolonner i listboxes.
Med disse ændringer har jeg dog fået nogle problemer, som jeg håber i kan løse.
Problem 1:
Jeg kan ikke lave kolonner i mine listviews.
Problem 2:
Jeg kan ikke tilføje tekst, som i en listbox.
Problem 3:
Jeg kan ikke ændre farven på hver enkelt række.
(Problem 4:
Min toolbar ser lidt sjov ud, det eneste der er sket (ud over at tilføje knapperne) er at lige under min menu er der kommet en ekstra streg.)