为什么调用CreateWindow函数的时候WinProc函数没有反应?

jtceleron 2011-10-05 11:30:34
我在WinProc函数写了WM_CREATE消息的处理语句,但是为什么调用CreateWindow函数的时候WinProc函数里对应的语句没有反应?
...全文
91 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Roy_Smiling 2011-10-05
  • 打赏
  • 举报
回复
WinProc是回调函数,当有消息的时候OS 会调用的。你调用CreateWindow函数,跟WinProc有神码关系。
haican963 2011-10-05
  • 打赏
  • 举报
回复
我也刚看windows程序设计。不知道是怎样没反应啊?应该有反应啊~
老邓 2011-10-05
  • 打赏
  • 举报
回复
发错了,用这个吧。
先跑起来再说。
#include <Windows.h>
#include <tchar.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
return 0;
case WM_PAINT:
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}

return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE, PTSTR /*pszCmdLine*/, int /*nCmdShow*/)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = _T("Windows Demo Class");
wcex.hIconSm = wcex.hIcon;

if (!RegisterClassEx(&wcex))
{
MessageBox(NULL, _T("RegisterClassEx failed!"), _T("Windows Demo"), MB_ICONERROR);
return 0;
}

HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW,
_T("Windows Demo Class"),
_T("Windows Demo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);

MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return (int)msg.wParam;
}
老邓 2011-10-05
  • 打赏
  • 举报
回复
#include <atlbase.h>
#include <atlwin.h>

CComModule _Module;

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static CAxWindow axWindow;

switch (uMsg)
{
case WM_CREATE:
RECT rc;
GetClientRect(hWnd, &rc);
axWindow.Create(hWnd, rc, 0, WS_CHILD | WS_VISIBLE);
axWindow.CreateControl(OLESTR("shell.Explorer.2"));
IWebBrowser2* webBrowser;
axWindow.QueryControl(__uuidof(IWebBrowser2), (void**)&webBrowser);
VARIANT varURL;
VariantInit(&varURL);
varURL.vt = VT_BSTR;
varURL.bstrVal = SysAllocString(_T("http://2011.hebei.teacher.com.cn/"));
webBrowser->Navigate2(&varURL, 0, 0, 0, 0);
VariantClear(&varURL);
webBrowser->Release();
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}

return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE, PTSTR /*pszCmdLine*/, int /*nCmdShow*/)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = _T("Windows Demo Class");
wcex.hIconSm = wcex.hIcon;

if (!RegisterClassEx(&wcex))
{
MessageBox(NULL, _T("RegisterClassEx failed!"), _T("Windows Demo"), MB_ICONERROR);
return 0;
}

HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW,
_T("Windows Demo Class"),
_T("Windows Demo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);

MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return (int)msg.wParam;
}
有点小问题,颜色不能交替变换 #include<windows.h> #include #include LRESULT CALLBACK WinProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ); int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) { //定义一个窗口类 WNDCLASS wincls; wincls.cbClsExtra=0; wincls.cbWndExtra=0; wincls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wincls.hCursor=LoadCursor(NULL,IDC_CROSS); wincls.hIcon=LoadIcon(NULL,IDI_ERROR); wincls.lpfnWndProc=WinProc; wincls.lpszClassName="Lucky_wei"; wincls.lpszMenuName=NULL; wincls.hInstance=hInstance; wincls.style=CS_HREDRAW | CS_VREDRAW; if(!RegisterClass(&wincls;)) { MessageBox(NULL,"窗口类注册失败","注册窗口",NULL); return 1; } // //创建窗口 ,定义一个变量用来保存成功建立的窗口的句柄 HWND hwnd; hwnd=CreateWindow("Lucky_wei","旋转小风车",WS_OVERLAPPEDWINDOW,0,0,600,600,NULL,NULL,hInstance,NULL); if(!hwnd) { MessageBox(NULL,"创建窗口失败","创建窗口",NULL); return 1; } // // //显示并更新窗口 ShowWindow(hwnd,SW_SHOWNORMAL); UpdateWindow(hwnd); // // //定义消息结构体,实现消息循环 MSG msg; while(GetMessage(&msg;,NULL,0,0)) { TranslateMessage(&msg;); DispatchMessage(&msg;); } return msg.wParam; } // // // //编写窗口过程函数 LRESULT CALLBACK WinProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { int xogr,yogr,a,b,i; float xbegin[3],ybegin[3],xend[3],yend[3]; HDC hDC; PAINTSTRUCT ps; HBRUSH hBrush; HPEN hPen; RECT clientRect; COLORREF color[4]={RGB(255,0,0),RGB(0,255,0),RGB(0,0,255),RGB(255,255,255)}; int BrushStyle[3]={BLACK_BRUSH,GRAY_BRUSH,WHITE_BRUSH}; switch(uMsg) { case WM_PAINT: hDC=BeginPaint(hwnd,&ps;); GetClientRect(hwnd,&clientRect;); xogr=clientRect.left+100; yogr=clientRect.top+100; a=xogr+200; b=yogr+200; xbegin[0]=400; xbegin[1]=400-100*2.732; xbegin[2]=xbegin[1]; ybegin[0]=b-100; ybegin[1]=yogr; ybegin[2]=400; xend[0]=xbegin[1]; xend[1]=xbegin[2]; xend[2]=xbegin[0]; yend[0]=ybegin[1]; yend[1]=ybegin[2]; yend[2]=ybegin[0]; for(i=0;i<3;i++) { SelectObject(hDC,hPen); hBrush=CreateSolidBrush(color[i]); SelectObject(hDC,hBrush); Pie(hDC,xogr,yogr,a,b,xbegin[i],ybegin[i],xend[i],yend[i]); Sleep(100); } InvalidateRect(hwnd,NULL,1); DeleteObject(hPen); DeleteObject(hBrush); EndPaint(hwnd,&ps;); break; case WM_TIMER: if(wParam==1111) InvalidateRect(hwnd,NULL,1); break; case WM_CLOSE: if(IDYES==MessageBox(hwnd,"是否真的结束?","message",MB_YESNO)) { DestroyWindow(hwnd); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,uMsg,wParam,lParam); break; } return 0; }

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧