VC++创建窗口不显示

按照书上写了书上说“要显示窗口中输出文字或者图形,需要用到设备描述表(DC)。我不会用啊,菜鸟求助各位程序如下#include<windows.h>LRESULTCALL... 按照书上写了 书上说“要显示窗口中输出文字或者图形,需要用到设备描述表(DC)。我不会用啊,菜鸟求助各位
程序如下
#include<windows.h>
LRESULT CALLBACK WndProc(HWND , UINT,WPARAM, LPARAM);
int APIENTRY WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;
WNDCLASS wndclass;
HWND hWnd;

wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "wootao system";

if ( ! RegisterClass (&wndclass) )
return FALSE;

hWnd = CreateWindow ("phoenix","NULL",
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL,NULL,
hInstance, NULL);

ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);

while ( GetMessage (&msg, NULL, 0, 0) )
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam)
{
static HDC hDC,hMemDC;

switch (message){
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hWnd, &ps);
TextOut(hDC,10,20,"sb",28);
return 0;
case WM_DESTROY:
PostQuitMessage (0);
return 0;

}
return DefWindowProc (hWnd, message, wParam, lParam);
}
展开
 我来答
百度网友2416aca46
2010-05-01 · TA获得超过1662个赞
知道小有建树答主
回答量:544
采纳率:0%
帮助的人:375万
展开全部
你的程序有几处错误:

hWnd = CreateWindow ("phoenix","NULL",
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL,NULL,
hInstance, NULL); //第一处错误

ShowWindow (hWnd, nCmdShow); //第二处错误

//按照如下方法修改,即可正常显示窗口。
hWnd = CreateWindow ("wootao system","xx",
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL,NULL,
hInstance, NULL);

ShowWindow (hWnd, SW_SHOW);

同学你应该注意到,CreateWindow的第一个参数是类名,也就是要和你在wndclass.lpszClassName = "wootao system";中指定的"wootao system"一模一样,第二个参数是窗口名,随便取了,但是我建议你为了编码规范最好不要使用类似于"NULL"之类的窗口名。
第二个问题,ShowWindow (hWnd, nCmdShow);只是一个函数原型,在调用的时候你应该根据实际情况修改其中的参数,比如你要显示窗口nCmdShow就应该用SW_SHOW来代替,隐藏窗口就用SW_HIDE,具体情况你可以查阅MSDN。另外ShowWindow的第一个参数也不一定是hWnd,在你的程序中只是因为你用
hWnd = CreateWindow ()创建了一个窗口,而hWnd就是返回的窗口句柄,所以恰好你能够在ShowWindow中使用hWnd,其实这里的hWnd只是一个名字而已,如果你喜欢,你换成xyz也是可以的当然我不建议你这么做。
L_o_o_n_i_e
2010-05-01 · TA获得超过4.2万个赞
知道大有可为答主
回答量:8507
采纳率:38%
帮助的人:5205万
展开全部
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
...
HDC hDC;
TEXTMETRIC tm;
PAINTSTRUCT ps;
COLORREF color;
HFONT font;
HPEN hP1; // pen
...
switch(message)
{
case WM_CREATE:
hDC=GetDC(hwnd);
...
ReleaseDC(hwnd,hDC);
return 0;
case WM_PAINT:
hDC = BeginPaint(hWnd, &ps);
SetGraphicsMode(hDC,GM_ADVANCED);
SetWindowExtEx(hDC,700,400,NULL);
// 颜色,线型 供画线用
color=RGB(0,128,128);
hP1=CreatePen(PS_SOLID,0,color);
SelectObject(hDC,hP1);
// 建font 用 font
font=CreateFont(
24,10,0,0, FW_NORMAL,0,0,0, ANSI_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,NULL,"myfont"
);
SelectObject(hDC,font);
TextOut(hDC,10,20,"sb",28);
EndPaint(hwnd,&ps); // end paint
return 0;
case WM_DESTROY:
PostQuitMessage (0);
return 0;
default: return DefWindowProc(hwnd,message,wParam,lParam);
}
===================================
下面是我的show_color 程序,用鼠标点颜色条改变RGB,椭圆显示混合颜色。
// cl show_color.cpp
#include <Afxwin.h>
#include <math.h>
#define DEBUG 1

HWND hWndMain;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow);
char one_line[80];
int len,NN;
LPTSTR argv;
RECT RectR, RectG, RectB, RectX;
int RectDy,RectW,RectH;
int x_r=495,x_g=495,x_b=495,x_shift=100;
long int v_r,v_g,v_b;

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int

nCmdShow)
{
MSG Msg;
int i;
RectDy = 50; RectW = 400; RectH = 30;
RectR.left = 0; RectR.right=RectW; RectR.bottom=200; RectR.top = RectR.bottom + RectH;
RectG.left = 0; RectB.left = 0;
RectG.right=RectW; RectB.right=RectW;
RectG.bottom=RectR.bottom + RectDy; RectB.bottom=RectG.bottom + RectDy;
RectG.top = RectG.bottom + RectH;
RectB.top = RectB.bottom + RectH;
v_r = (x_r - x_shift) * 255 / RectW;
v_g = (x_g - x_shift) * 255 / RectW;
v_b = (x_b - x_shift) * 255 / RectW;

if(!InitWindowsClass(hInstance))
return FALSE;
if(!InitWindows(hInstance,nCmdShow))
return FALSE;
ShowWindow(hWndMain,nCmdShow);
UpdateWindow(hWndMain);
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static long nXChar,nYChar,xInc,yInc;
static int xClient, yClient; // width height of client area
static int xClientMax; // maximum width of client area
static int xChar, yChar; // horizontal vertical scrolling unit
static int xPos,yPos; // current horizontal vertical scrolling position
static int xMax,yMax; // maximum horizontal scrolling position
SCROLLINFO si;

HDC hdc;
short x;
TEXTMETRIC tm;
PAINTSTRUCT ps;
COLORREF color;
HFONT font;
HPEN hP1; // pen
HBRUSH hBr,hBrR,hBrG,hBrB;
CPoint aP,mousePos;
int i;
switch(message)
{
case WM_LBUTTONDOWN: case WM_LBUTTONUP:
mousePos.x = LOWORD( lParam );
mousePos.y = HIWORD( lParam );
if (mousePos.x >= RectR.left+x_shift && mousePos.x <= RectR.right+x_shift){
if (mousePos.y > RectR.bottom && mousePos.y < RectR.top) x_r = mousePos.x;
if (mousePos.y > RectG.bottom && mousePos.y < RectG.top) x_g = mousePos.x;
if (mousePos.y > RectB.bottom && mousePos.y < RectB.top) x_b = mousePos.x;
v_r = (x_r - x_shift) * 255 / RectW;
v_g = (x_g - x_shift) * 255 / RectW;
v_b = (x_b - x_shift) * 255 / RectW;
ShowWindow(hwnd, SW_HIDE);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
};
return 0;
case WM_CREATE:

hdc=GetDC(hwnd);
GetTextMetrics(hdc,&tm);
nXChar=tm.tmAveCharWidth;
nYChar=tm.tmHeight;
xInc=1;yInc=1; xChar=nXChar; yChar=nYChar; // for scroll window
ReleaseDC(hwnd,hdc);
xClientMax = 800;
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps); // begin paint
SetGraphicsMode(hdc,GM_ADVANCED);
SetWindowExtEx(hdc,700,400,NULL); // cx=700,cy=400 logical unit
// SetViewportExtEx(hdc,600,400,NULL);
SetViewportExtEx(hdc,600,400,NULL);
SetViewportOrgEx(hdc,x_shift,10,NULL);
SetMapMode(hdc,MM_ANISOTROPIC);

color=RGB(0,128,128);
hP1=CreatePen(PS_SOLID,0,color);
SelectObject(hdc,hP1);

hBrR = CreateSolidBrush( RGB(255,0,0));
hBrG = CreateSolidBrush( RGB(0,255,0));
hBrB = CreateSolidBrush( RGB(0,0,255));
hBr = CreateSolidBrush( RGB(200,200,200));
SelectObject(hdc,hBrR);
FillRect(hdc, &RectR, hBrR);
RectX=RectR; RectX.left=x_r-x_shift; SelectObject(hdc,hBr); FillRect(hdc, &RectX, hBr);
SelectObject(hdc,hBrG);
FillRect(hdc, &RectG, hBrG);
RectX=RectG; RectX.left=x_g-x_shift; SelectObject(hdc,hBr); FillRect(hdc, &RectX, hBr);
SelectObject(hdc,hBrB);
FillRect(hdc, &RectB, hBrB);
RectX=RectB; RectX.left=x_b-x_shift; SelectObject(hdc,hBr); FillRect(hdc, &RectX, hBr);

font=CreateFont(
24,10,0,0, FW_NORMAL,0,0,0, ANSI_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,NULL,"myfont"
);
SelectObject(hdc,font);
GetTextMetrics(hdc,&tm);
nYChar=tm.tmHeight;
color=RGB(0,0,0);
sprintf(one_line,"R=%3d",v_r);
TextOut(hdc,RectR.right+10,RectR.bottom,one_line,strlen(one_line));
sprintf(one_line,"G=%3d",v_g);
TextOut(hdc,RectG.right+10,RectG.bottom,one_line,strlen(one_line));
sprintf(one_line,"B=%3d",v_b);
TextOut(hdc,RectB.right+10,RectB.bottom,one_line,strlen(one_line));
color = RGB(v_r,v_g,v_b);
hBr = CreateSolidBrush(color);
SelectObject(hdc,hBr);
Ellipse(hdc, 100, 30, 326, 144);

EndPaint(hwnd,&ps); // end paint
return 0L;

case WM_SIZE:
yClient = HIWORD (lParam);
xClient = LOWORD (lParam);
yMax = max (0, 600 - yClient/yChar); // need to calculate
yPos = min (yPos, yMax); // current position not exceed the maximum
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
si.nMin = 0; si.nMax = yMax; si.nPage = yClient / yChar;
si.nPos = yPos;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
xMax = max (0, 2 + (1600 - xClient)/xChar);
xPos = min (xPos, xMax);
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
si.nMin = 0;
si.nMax = xMax;
si.nPage = xClient / xChar;
si.nPos = xPos;
SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
UpdateWindow (hwnd);
return 0;
case WM_VSCROLL:
switch(LOWORD (wParam))
{
case SB_PAGEUP:
yInc = -4; break;
case SB_PAGEDOWN:
yInc = 4; break;
case SB_LINEUP: // User clicked the left arrow.
yInc = -1; break;
case SB_LINEDOWN: // User clicked the right arrow.
yInc = 1; break;
case SB_THUMBTRACK: // User dragged the scroll box.
yInc = HIWORD(wParam) - yPos; break;
default:
yInc = 0; break;
}
if (yInc = max(-yPos, min(yInc, yMax - yPos)))
{
yPos += yInc;
ScrollWindowEx(hwnd, 0, -yChar * yInc,
(CONST RECT *) NULL, (CONST RECT *) NULL,
(HRGN) NULL, (LPRECT) NULL, SW_INVALIDATE);
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
si.nPos = yPos;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
UpdateWindow (hwnd);
};

return 0;

case WM_HSCROLL:
switch(LOWORD (wParam))
{
case SB_PAGEUP:
xInc = -4; break;
case SB_PAGEDOWN:
xInc = 4; break;
case SB_LINEUP: // User clicked the left arrow.
xInc = -1; break;
case SB_LINEDOWN: // User clicked the right arrow.
xInc = 1; break;
case SB_THUMBTRACK: // User dragged the scroll box.
xInc = HIWORD(wParam) - xPos; break;
default:
xInc = 0; break;
}
if (xInc = max(-xPos, min(xInc, xMax - xPos)))
{
xPos += xInc;
ScrollWindowEx(hwnd, -xChar * xInc, 0,
(CONST RECT *) NULL, (CONST RECT *) NULL,
(HRGN) NULL, (LPRECT) NULL, SW_INVALIDATE);
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
si.nPos = xPos;
SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
UpdateWindow (hwnd);
};

return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd,message,wParam,lParam);

}

}

BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,"END");
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName="Windows Fill";
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
return(RegisterClass(&wndclass));
}

BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
{
HWND hWnd;
hWnd=CreateWindow(
"Windows Fill",
"Show_color",
WS_OVERLAPPEDWINDOW | WS_SYSMENU | WS_BORDER | WS_HSCROLL | WS_VSCROLL,
100,100,800,400,
NULL,
NULL,
hInstance,
NULL
);
if(!hWnd)
return FALSE;
hWndMain=hWnd;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
缑盛戚夜绿
2020-04-12 · TA获得超过3752个赞
知道大有可为答主
回答量:3105
采纳率:32%
帮助的人:172万
展开全部
你的程序最后一句错了,就是wndproc回调函数,不能return
0。
而应该返回默认的窗口执行过程:
return
defwindowproc
(hwnd,
message,
wparam,
lparam)
;
写程序要仔细呀。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
函宏肖清舒
2019-07-28 · TA获得超过4017个赞
知道大有可为答主
回答量:3131
采纳率:31%
帮助的人:149万
展开全部
你的程序最后一句错了,就是WndProc回调函数,不能return
0。
而应该返回默认的窗口执行过程:
return
DefWindowProc
(hwnd,
message,
wParam,
lParam)
;
写程序要仔细呀。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式