sjaycohn
January 17th, 2008, 08:33 PM
Hi, I wrote a Win32 Program in C++ for Windows XP using Bloodshed DevC++. It is an 800 by 600 window with fixed size, and contains a center black box surrounded by two black rectangles. These rectangles have four white text strings printed in them using TextOut. The bottom two don't show up because the window is smaller than it should be. Why is my window less than 800x600?
How do I fix this?
#define WIN32_LEAN_AND_MEAN // just say no to MFC
#include <windows.h> // include all the windows headers
#include <windowsx.h> // include useful macros
#include <mmsystem.h> // very important and include WINMM.LIB too!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string>
using namespace std;
// DEFINES ////////////////////////////////////////////////
// defines for windows
#define WINDOW_CLASS_NAME "WINCLASS1"
HWND main_window_handle = NULL; // globally track main window
HINSTANCE hinstance_app = NULL; // globally track hinstance
RECT rect = {0,0,800,100}, rect2 = {0,500,800,600};
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT ps; // used in WM_PAINT
HDC hdc; // handle to a device context
switch(msg)
{
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
HRGN rect3 = CreateRectRgn(100,100,700,500);
FillRgn(hdc,rect3,(HBRUSH)CreateSolidBrush(RGB(0,0,0)));
HRGN hrgn = CreateRectRgn(0,0,800,100);
FillRgn(hdc,hrgn,CreateSolidBrush(RGB(0,0,0)));
HRGN hrgn2 = CreateRectRgn(0,500,800,600);
FillRgn(hdc,hrgn2,CreateSolidBrush(RGB(0,0,0)));
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc,RGB(255,255,255));
TextOut(hdc,0,0,"Message1",8);
TextOut(hdc,720,0,"Message2",8);
TextOut(hdc,0,575,"Message3",8);
TextOut(hdc,720,575,"Message4",8);
EndPaint(hwnd,&ps);
return(0);
} break;
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
} break;
default:break;
} // end switch
return (DefWindowProc(hwnd, msg, wparam, lparam));
}
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg; // generic message
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(127,127,127));
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
hinstance_app = hinstance;
if (!RegisterClassEx(&winclass))
return(0);
// create the window
hwnd = CreateWindowEx (
0,
WINDOW_CLASS_NAME,
"Windows App",
WS_SYSMENU | WS_OVERLAPPED | WS_CAPTION | WS_DLGFRAME,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
HWND_DESKTOP,
NULL,
hinstance,
NULL
);
ShowWindow (hwnd, ncmdshow);
main_window_handle = hwnd;
while(TRUE)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return(msg.wParam);
}
How do I fix this?
#define WIN32_LEAN_AND_MEAN // just say no to MFC
#include <windows.h> // include all the windows headers
#include <windowsx.h> // include useful macros
#include <mmsystem.h> // very important and include WINMM.LIB too!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string>
using namespace std;
// DEFINES ////////////////////////////////////////////////
// defines for windows
#define WINDOW_CLASS_NAME "WINCLASS1"
HWND main_window_handle = NULL; // globally track main window
HINSTANCE hinstance_app = NULL; // globally track hinstance
RECT rect = {0,0,800,100}, rect2 = {0,500,800,600};
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT ps; // used in WM_PAINT
HDC hdc; // handle to a device context
switch(msg)
{
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
HRGN rect3 = CreateRectRgn(100,100,700,500);
FillRgn(hdc,rect3,(HBRUSH)CreateSolidBrush(RGB(0,0,0)));
HRGN hrgn = CreateRectRgn(0,0,800,100);
FillRgn(hdc,hrgn,CreateSolidBrush(RGB(0,0,0)));
HRGN hrgn2 = CreateRectRgn(0,500,800,600);
FillRgn(hdc,hrgn2,CreateSolidBrush(RGB(0,0,0)));
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc,RGB(255,255,255));
TextOut(hdc,0,0,"Message1",8);
TextOut(hdc,720,0,"Message2",8);
TextOut(hdc,0,575,"Message3",8);
TextOut(hdc,720,575,"Message4",8);
EndPaint(hwnd,&ps);
return(0);
} break;
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
} break;
default:break;
} // end switch
return (DefWindowProc(hwnd, msg, wparam, lparam));
}
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg; // generic message
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(127,127,127));
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
hinstance_app = hinstance;
if (!RegisterClassEx(&winclass))
return(0);
// create the window
hwnd = CreateWindowEx (
0,
WINDOW_CLASS_NAME,
"Windows App",
WS_SYSMENU | WS_OVERLAPPED | WS_CAPTION | WS_DLGFRAME,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
HWND_DESKTOP,
NULL,
hinstance,
NULL
);
ShowWindow (hwnd, ncmdshow);
main_window_handle = hwnd;
while(TRUE)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return(msg.wParam);
}