Fonts going crazy (win32 API GUI)
I downloaded the source for a free program that I wanted to add my own features to as my first attempt at C++ and I managed to do that fairly easily using MSVC++ (on my universities network). However the GUI was made using a .res file and since I only have Dev-C++ at home, I tried learning win32 API and doing it that way. I managed to get most parts working but I basically used the windows application template that Dev-C++ gives you, added a load of CreateWindowEX statements and copied what was in the original programs BOOL CALLBACK MainCallB method into an LRESULT CALLBACK WindowProcedure. At first nothing in the case: WM_INITDIALOG part worked and the fonts on the buttons were all wrong. I tried changing the WM_INITDIALOG to WM_PAINT and that made it do some of the stuff in that case but made the fonts in the text areas go all crazy and everything is painted all scattered if I minimise and maximise again. This is a long piece of code but I don't have a clue whch part is causing the problem because it compiles without any problem and I don't want to have to start from scratch and add a load of extra styles and classes that I don't need.
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INPUT 1
#define OUTPUT 2
#define ENCRYPT 3
#define DECRYPT 4
#define CLOSE 5
#define CLEARIN 6
#define CLEAROUT 7
#define AUTOID 8
#define INPUTLABEL 9
#define OUTPUTLABEL 10
#define AUTOIDLABEL 11
#define GAMEIDLABEL 12
#define GAMEID 13
#define REIGONLABEL 14
#define REIGON 15
#define TEXTSIZE 32767
unsigned short gameid=0;
unsigned char dogameid=1,region=0,INITMAX=1;
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
char szClassName[] = "WindowsApp";
HINSTANCE ghInstance; // May not work
HICON iWindow; // May not work
HFONT hFont, hNewFont; // May not work
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil) {
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"MAXCrypt", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
588, /* The programs width */
478, /* 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 */
);
CreateWindowEx (
0,
"Static",
"Input:",
0x50000000,
9,39,76,16,
hwnd,
(HMENU) INPUTLABEL,
GetModuleHandle(NULL),
NULL
);
CreateWindowEx (
0,
"Static",
"Output:",
0x50000000,
300,39,76,16,
hwnd,
(HMENU) OUTPUTLABEL,
GetModuleHandle(NULL),
NULL
);
CreateWindowEx (
0,
"Static",
"Auto Identifier:",
0x50000000,
21,396,90,19,
hwnd,
(HMENU) AUTOIDLABEL,
GetModuleHandle(NULL),
NULL
);
CreateWindowEx (
0,
"Static",
"Reigon:",
0x50000000,
111,396,90,19,
hwnd,
(HMENU) REIGONLABEL,
GetModuleHandle(NULL),
NULL
);
CreateWindowEx (
0,
"Static",
"Game ID:",
0x50000000,
3,420,46,16,
hwnd,
(HMENU) GAMEIDLABEL,
GetModuleHandle(NULL),
NULL
);
CreateWindowEx (
0,
"Button",
"Clear",
0x50010000,
216,30,70,22,
hwnd,
(HMENU) CLEARIN,
GetModuleHandle(NULL),
NULL
);
CreateWindowEx (
0,
"Button",
"Clear",
0x50010000,
507,30,70,22,
hwnd,
(HMENU) CLEAROUT,
GetModuleHandle(NULL),
NULL
);
CreateWindowEx (
0,
"Button",
"Encrypt",
0x50010000,
216,396,70,22,
hwnd,
(HMENU) ENCRYPT,
GetModuleHandle(NULL),
NULL
);
CreateWindowEx (
0,
"Button",
"Decrypt",
0x50010000,
294,396,70,22,
hwnd,
(HMENU) DECRYPT,
GetModuleHandle(NULL),
NULL
);
CreateWindowEx (
0,
"Button",
"",
0x50010003,
3,396,16,16,
hwnd,
(HMENU) AUTOID, // Checkbox
GetModuleHandle(NULL),
NULL
);
CreateWindowEx (
0,
"Button",
"Close",
0x50010000,
507,396,70,22,
hwnd,
(HMENU) CLOSE,
GetModuleHandle(NULL),
NULL
);
CreateWindowEx (
0x00000200,
"Edit",
"",
0x50210044,
3,57,283,334,
hwnd,
(HMENU) INPUT,
GetModuleHandle(NULL),
NULL
);
CreateWindowEx (
0x00000200,
"Edit",
"",
0x50210844,
294,57,283,334,
hwnd,
(HMENU) OUTPUT,
GetModuleHandle(NULL),
NULL
);
CreateWindowEx (
0x00000200,
"Edit",
"",
0x50010000,
51,422,49,18,
hwnd,
(HMENU) GAMEID,
GetModuleHandle(NULL),
NULL
);
CreateWindowEx (
0,
"ComboBox",
"",
0x50010003,
111,420,82,90,
hwnd,
(HMENU) REIGON,
GetModuleHandle(NULL),
NULL
);
ShowWindow (hwnd, nFunsterStil);
while (GetMessage (&messages, NULL, 0, 0)) {
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
LOGFONT lf;
HDC hDC;
PAINTSTRUCT Ps;
HFONT hFont;
switch(uMsg) {
case WM_PAINT:
//the almighty buildseeds() function!! without this, the crypto routines are useless
// buildseeds();
if(INITMAX) {
//setup font
hFont = (HFONT)SendMessage(hwndDlg, WM_GETFONT, 0, 0);
GetObject(hFont, sizeof(LOGFONT), &lf);
strcpy(lf.lfFaceName,"Courier");
hNewFont = CreateFontIndirect(&lf);
SendDlgItemMessage(hwndDlg,INPUT,WM_SETFONT,(WPARA M)hNewFont,FALSE);
SendDlgItemMessage(hwndDlg,OUTPUT,WM_SETFONT,(WPAR AM)hNewFont,FALSE);
SendDlgItemMessage(hwndDlg,GAMEID,WM_SETFONT,(WPAR AM)hNewFont,FALSE);
//set text limits
SendDlgItemMessage(hwndDlg,INPUT,EM_SETLIMITTEXT,( TEXTSIZE-1),0);
SendDlgItemMessage(hwndDlg,GAMEID,EM_SETLIMITTEXT, 3,0);
//tick checkbox
CheckDlgButton(hwndDlg, AUTOID, BST_CHECKED);
//fill region list box
SendMessage(GetDlgItem(hwndDlg,REIGON),CB_ADDSTRIN G,(WPARAM)0,(LPARAM)("USA"));
SendMessage(GetDlgItem(hwndDlg,REIGON),CB_ADDSTRIN G,(WPARAM)0,(LPARAM)("PAL"));
SendMessage(GetDlgItem(hwndDlg,REIGON),CB_ADDSTRIN G,(WPARAM)0,(LPARAM)("Japan"));
SendMessage(GetDlgItem(hwndDlg,REIGON),CB_ADDSTRIN G,(WPARAM)0,(LPARAM)("Null"));
SendMessage(GetDlgItem(hwndDlg,REIGON),CB_SETCURSE L,(WPARAM)0,(LPARAM)0);
SetDlgItemText(hwndDlg,OUTPUT,"Ah ha!!!!!"); // Riiiiggghhhtt!!!
INITMAX=0;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CLOSE:
case WM_QUIT:
DeleteObject(hNewFont);
DestroyWindow(hwndDlg);
hwndDlg=NULL;
break;
case WM_COMMAND:
switch(HIWORD(wParam)) {
case BN_CLICKED:
switch(LOWORD(wParam)) {
case DECRYPT: //Decrypt
// DecryptParseList();
break;
case ENCRYPT: //Encrypt
// EncryptParseList();
break;
case CLOSE: //Close
DeleteObject(hNewFont);
DestroyWindow(hwndDlg);
hwndDlg=NULL;
break;
case CLEARIN: //Clear (input)
SetDlgItemText(hwndDlg,INPUT,"");
break;
case CLEAROUT: //Clear (output)
SetDlgItemText(hwndDlg,OUTPUT,"");
break;
case AUTOID: //Game ID:
dogameid^=1;
SendDlgItemMessage(hwndDlg,GAMEID,EM_SETREADONLY,d ogameid^1,0);
EnableWindow(GetDlgItem(hwndDlg,REIGON),dogameid);
break;
}
break;
}
// break;
default:
return DefWindowProc (hwndDlg, uMsg, wParam, lParam);
}
return FALSE;
}
I tried getting it to work with a resource script and this part always returns false:
hWindow = CreateDialog(ghInstance,MAKEINTRESOURCE(MAIN),NULL ,MainCallB);
if (!hWindow) {
MessageBox(NULL, "Could not create main dialog", "ERROR", MB_ICONERROR | MB_OK);
return 0;
}
EDIT: Someone suggested getting rid of the WM_PAINT case and moving it's contents into WinMain. This worked, but the text isn't being drawn properly. When I use Courier, the text appears as scattered pixels and when I use Courier New, it has strikethrough, underline and italic on it. I also need to change the size of the font on both the text areas and the buttons.
|