temuhin
September 28th, 2008, 05:12 AM
Dear Folks,
I have a small problem using the SelectObject function in GDI. The code won´t compile unless I explicitly cast the returnvalue of SelectObject to HPEN.
According to the MSDN documentation, the function returns an HGDIOBJ object and casting should not be necessary.
Any reason for this? Did I do anything wrong?
HPEN green_pen = CreatePen(PS_SOLID,1,RGB(0,255,0));
HPEN old_pen;
old_pen = (HPEN)SelectObject( hdc, green_pen);
MoveToEx(hdc,10,10,NULL);
I have included the complete code below, however, the codesnippet above should suffice.
Regards
t
// include the basic windows header file
#include <windows.h>
#include <windowsx.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <string>
#define PI 3.1415
using namespace std;
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1:0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1:0)
// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
HDC hdc;
ofstream output;
// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// the handle for the window, filled by a function
HWND hWnd;
COLORREF red = RGB(255,0,0);
COLORREF green = RGB(0,255,0);
COLORREF blue = RGB(0,0,255);
// this struct holds information for the window class
WNDCLASSEX wc;
// clear out the window class for use
ZeroMemory(&wc, sizeof(WNDCLASSEX));
// fill in the struct with the needed information
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"WindowClass1";
// register the window class
RegisterClassEx(&wc);
// create the window and use the result as the handle
hWnd = CreateWindowEx(NULL,
L"WindowClass1", // name of the window class
L"Our First Windowed Program", // title of the window
WS_OVERLAPPEDWINDOW, // window style
300, // x-position of the window
300, // y-position of the window
500, // width of the window
400, // height of the window
NULL, // we have no parent window, NULL
NULL, // we aren't using menus, NULL
hInstance, // application handle
NULL); // used with multiple windows, NULL
hdc = GetDC(hWnd);
// create pen
HPEN green_pen = CreatePen(PS_SOLID,1,RGB(0,255,0));
HPEN old_pen;
old_pen = (HPEN)SelectObject( hdc, green_pen);
MoveToEx(hdc,10,10,NULL);
// display the window on the screen
ShowWindow(hWnd, nCmdShow);
TextOut(hdc,5,5,L"test",strlen("test"));
// enter the main loop:
// this struct holds Windows event messages
MSG msg;
output.open("LOGG.TXT",ios::out);
float s = cos(0.0);
//output.close();
// wait for the next message in the queue, store the result in 'msg'
while(true)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(KEYUP(VK_LEFT))
{
SetTextColor(hdc,RGB(rand()%256,rand()%256,rand()%256));
SetBkColor(hdc,RGB(0,0,0));
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc,100,100,L"Test DEMO", strlen("Test DEMO"));
}
if(KEYUP(VK_DOWN))
{
double f=200.0;
double T = 1/f;
int counter=1;
COLORREF blue = RGB(0,0,255);
for(int i=0;i<500;i=i+5)
{
int y = (int)(100*sin(2*PI*f*i)+250);
//SetPixel(hdc,i,y,blue);
//SetPixel(hdc,i,250,blue);
LineTo(hdc,i,y);
output << counter << "\t" <<y <<"\n";
counter++;
}
output.close();
}
}
ReleaseDC(hWnd,hdc);
}
// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
RECT rect;
// sort through and find what code to run for the message given
switch(message)
{
case WM_CREATE:
{
MessageBox(NULL, L"CREATED",L"",MB_OK);
}
break;
case WM_PAINT:
{
//hdc = GetDC(hWnd);
//ReleaseDC(hWnd,hdc);
GetClientRect(hWnd,&rect);
ValidateRect(hWnd,&rect);
return 0;
}
break;
// this message is read when the window is closed
case WM_DESTROY:
{
// close the application entirely
PostQuitMessage(0);
return 0;
} break;
default:
break;
}
// Handle any messages the switch statement didn't
return DefWindowProc (hWnd, message, wParam, lParam);
}
I have a small problem using the SelectObject function in GDI. The code won´t compile unless I explicitly cast the returnvalue of SelectObject to HPEN.
According to the MSDN documentation, the function returns an HGDIOBJ object and casting should not be necessary.
Any reason for this? Did I do anything wrong?
HPEN green_pen = CreatePen(PS_SOLID,1,RGB(0,255,0));
HPEN old_pen;
old_pen = (HPEN)SelectObject( hdc, green_pen);
MoveToEx(hdc,10,10,NULL);
I have included the complete code below, however, the codesnippet above should suffice.
Regards
t
// include the basic windows header file
#include <windows.h>
#include <windowsx.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <string>
#define PI 3.1415
using namespace std;
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1:0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1:0)
// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
HDC hdc;
ofstream output;
// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// the handle for the window, filled by a function
HWND hWnd;
COLORREF red = RGB(255,0,0);
COLORREF green = RGB(0,255,0);
COLORREF blue = RGB(0,0,255);
// this struct holds information for the window class
WNDCLASSEX wc;
// clear out the window class for use
ZeroMemory(&wc, sizeof(WNDCLASSEX));
// fill in the struct with the needed information
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"WindowClass1";
// register the window class
RegisterClassEx(&wc);
// create the window and use the result as the handle
hWnd = CreateWindowEx(NULL,
L"WindowClass1", // name of the window class
L"Our First Windowed Program", // title of the window
WS_OVERLAPPEDWINDOW, // window style
300, // x-position of the window
300, // y-position of the window
500, // width of the window
400, // height of the window
NULL, // we have no parent window, NULL
NULL, // we aren't using menus, NULL
hInstance, // application handle
NULL); // used with multiple windows, NULL
hdc = GetDC(hWnd);
// create pen
HPEN green_pen = CreatePen(PS_SOLID,1,RGB(0,255,0));
HPEN old_pen;
old_pen = (HPEN)SelectObject( hdc, green_pen);
MoveToEx(hdc,10,10,NULL);
// display the window on the screen
ShowWindow(hWnd, nCmdShow);
TextOut(hdc,5,5,L"test",strlen("test"));
// enter the main loop:
// this struct holds Windows event messages
MSG msg;
output.open("LOGG.TXT",ios::out);
float s = cos(0.0);
//output.close();
// wait for the next message in the queue, store the result in 'msg'
while(true)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(KEYUP(VK_LEFT))
{
SetTextColor(hdc,RGB(rand()%256,rand()%256,rand()%256));
SetBkColor(hdc,RGB(0,0,0));
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc,100,100,L"Test DEMO", strlen("Test DEMO"));
}
if(KEYUP(VK_DOWN))
{
double f=200.0;
double T = 1/f;
int counter=1;
COLORREF blue = RGB(0,0,255);
for(int i=0;i<500;i=i+5)
{
int y = (int)(100*sin(2*PI*f*i)+250);
//SetPixel(hdc,i,y,blue);
//SetPixel(hdc,i,250,blue);
LineTo(hdc,i,y);
output << counter << "\t" <<y <<"\n";
counter++;
}
output.close();
}
}
ReleaseDC(hWnd,hdc);
}
// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
RECT rect;
// sort through and find what code to run for the message given
switch(message)
{
case WM_CREATE:
{
MessageBox(NULL, L"CREATED",L"",MB_OK);
}
break;
case WM_PAINT:
{
//hdc = GetDC(hWnd);
//ReleaseDC(hWnd,hdc);
GetClientRect(hWnd,&rect);
ValidateRect(hWnd,&rect);
return 0;
}
break;
// this message is read when the window is closed
case WM_DESTROY:
{
// close the application entirely
PostQuitMessage(0);
return 0;
} break;
default:
break;
}
// Handle any messages the switch statement didn't
return DefWindowProc (hWnd, message, wParam, lParam);
}