问题描述
你好,
我正在学习为Windows编写代码的过程,并且正在从一本书(Jonathan S. Harbour的《 Beginning Game Programming Second Edition》开始)中遵循示例,但是不幸的是,这些示例之一使用SetPixel函数.因此,当我创建一个窗口并尝试绘制一条线时,可以说整个窗口.看来在计算机上它仅画线的一半,而另一面是空白的.有谁知道这可能是什么原因?
我正在使用Visual Studio 2008,Direct X 11.0
链接到我所看到的屏幕截图:
[我的屏幕]
更新:下面列出的代码
Hello,
I am in the process of learning to code for windows and I am following examples out of a book (Beginning Game Programming Second Edition by Jonathan S. Harbour), but unfortunately one of these examples uses SetPixel function. So when I create a window and try to draw a line, lets say across the window. It seems that on computer it only draws half of the line and other side is blank. Does anyone know what might cause this?
I am using Visual Studio 2008, Direct X 11.0
Link to screen shot of what I see:
[My Screen]
Update: Code Listed below
//header files to include
#include<windows.h>
#include<stdlib.h>
#include<time.h>
//application title
#define APPTITLE "Hello World"
//function prototypes (forward declarations)
BOOL InitInstance(HINSTANCE, int);
ATOM MyRegisterClass(HINSTANCE);
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);
//the window event callback function
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
char *szHello = "Hello World!";
RECT rt;
int x, y, n;
COLORREF c;
switch (message)
{
case WM_PAINT:
//get the dimensions of the window
GetClientRect(hWnd, &rt);
//start drawing on devicce context
hdc = BeginPaint (hWnd, &ps);
//draw some text
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
c = RGB(0, 0, 0);
y = 300;
for(x=0; x <= 1280; x++)
{
SetPixel(hdc, x, y, c);
}
//stop drawing
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
//helper function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
//create the window class structure
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
//fill the struct with info
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = APPTITLE;
wc.hIconSm = NULL;
//set up the window with the class info
return RegisterClassEx(&wc);
}
//helper function to create the window and refresh it
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
//create a new window
hWnd = CreateWindow(
APPTITLE, //window class
APPTITLE, //title bar
WS_OVERLAPPEDWINDOW, //window style
CW_USEDEFAULT, //x position of window
CW_USEDEFAULT, //y position of window
1280, //width of the window
720, //height of the window
NULL, //parent window
NULL, //menu
hInstance, //application instance
NULL); //window parameters
//was there an error creating the window?
if(!hWnd)
return FALSE;
//display the window
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//entry point for a Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
//declare variables
MSG msg;
//register the class
MyRegisterClass(hInstance);
//initialize application
if(!InitInstance (hInstance, nCmdShow))
return FALSE;
//set random number seed
srand(time(NULL));
//main message loop
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
推荐答案
这篇关于使用SetPixel绘制线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!