//高手帮我哦,全SDK,做个完全自定义编辑框,可我写的这个代码子窗口得不到键盘消息
/*
高手帮我哦,全SDK,
想做个完全自定义编辑框。
hwnd1是hwnd2 的母窗口,子窗口HWND2做成编辑框(完全用自己的代码)
回调函数:hwnd1 (WndProc1),hwnd2 (WndProc2)
子窗口HWND2只能得到鼠标消息,键盘消息得不到,
而且也显示不了WM_PAINT中要显示的“22222”,
*/
#include "stdafx.h"
#include "stdio.h"
HINSTANCE hinst;LRESULT CALLBACK WndProc1(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(msg==WM_DESTROY)
{ PostQuitMessage(0);return  0;}
if(msg==WM_KEYDOWN||msg==WM_LBUTTONDOWN)
{MessageBox(hwnd,"1111111111111111","1111111111111",MB_OK);}
return DefWindowProc(hwnd, msg, wParam, lParam);   
}
LRESULT CALLBACK WndProc2(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(msg==WM_DESTROY)
{ PostQuitMessage(0);return  0;}
if(msg==WM_KEYDOWN||msg==WM_LBUTTONDOWN)
{MessageBox(hwnd,"2222222222222","22222222222222",MB_OK);}
if(msg==WM_PAINT)
{
PAINTSTRUCT ps;HDC hdc;
BeginPaint(hwnd,&ps);
char * str="22222222222";
TextOut(hdc,0,0,str,strlen(str));
}
int n=DefWindowProc(hwnd, msg, wParam, lParam);
return n;
   
}
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WndProc2(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
hinst = hInstance; 
MSG msg;
WNDCLASSEX wcex1;
wcex1.cbSize = sizeof(WNDCLASSEX); 
wcex1.style = CS_HREDRAW | CS_VREDRAW;
wcex1.lpfnWndProc = (WNDPROC)WndProc1;
wcex1.cbClsExtra = 0;
wcex1.cbWndExtra = 0;
wcex1.hInstance = hInstance;
wcex1.hIcon = NULL;//LoadIcon(hInstance, (LPCTSTR)IDI_WINSAMPLE1);
wcex1.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex1.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex1.lpszMenuName = NULL;//(LPCSTR)IDC_WINSAMPLE1;
wcex1.lpszClassName = "winsample";
wcex1.hIconSm = NULL;//LoadIcon(wcex1.hInstance, (LPCTSTR)IDI_SMALL);
RegisterClassEx(&wcex1);
HWND hwnd=CreateWindow("winsample", "winsample", WS_OVERLAPPEDWINDOW,
0,0,800,600,
  //CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 
  NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

WNDCLASSEX wcex2;
wcex2.cbSize = sizeof(WNDCLASSEX); 
wcex2.style = CS_HREDRAW | CS_VREDRAW;
wcex2.lpfnWndProc = (WNDPROC)WndProc2;
wcex2.cbClsExtra = 0;
wcex2.cbWndExtra = 0;
wcex2.hInstance = hInstance;
wcex2.hIcon = NULL;//LoadIcon(hInstance, (LPCTSTR)IDI_WINSAMPLE1);
wcex2.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex2.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex2.lpszMenuName = NULL;//(LPCSTR)IDC_WINSAMPLE1;
wcex2.lpszClassName = "win2";
wcex2.hIconSm = NULL;//LoadIcon(wcex2.hInstance, (LPCTSTR)IDI_SMALL);
RegisterClassEx(&wcex2);
//HWND hwnd2=CreateWindow("win2", "win2", WS_CHILDWINDOW|WS_VISIBLE, \
  //CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, \
  0,0,50,50,\
  hwnd, NULL, hInstance, NULL);
HWND hwnd2=CreateWindowEx(WS_EX_CLIENTEDGE,"win2", "win2", WS_CHILDWINDOW|WS_VISIBLE,   
  0,0,50,50,
  hwnd, NULL, hInstance, NULL);
ShowWindow(hwnd2, nCmdShow);
UpdateWindow(hwnd2); while (GetMessage(&msg, NULL, 0, 0)) 
{

{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} return msg.wParam;
}

解决方案 »

  1.   

    只看出一个问题
    if(msg==WM_PAINT)
    {
    PAINTSTRUCT ps;
     DC hdc = GetWindowDC(hwnd);
    BeginPaint(hwnd,&ps);
    char * str="22222222222";
    TextOut(hdc,0,0,str,strlen(str));
    EndPaint(hwnd,&ps);
    }
      

  2.   

    哦,EndPaint(hwnd,&ps);忘了,可是加上也不行,这里什么绘图都不会显示。
      

  3.   

    你哪个hdc赋值了吗?
     DC hdc = GetWindowDC(hwnd);
      

  4.   

    {
    //PAINTSTRUCT ps;
                       HDC hdc;
                       //应该加上hdc=GetDC(hwnd);// BeginPaint(hwnd,&ps);
    char * str="22222222222";
    TextOut(hdc,0,0,str,strlen(str));
    }          试一下if(msg==WM_CHAR||msg==WM_LBUTTONDOWN)呢?不确定,我这没有编译环境
      

  5.   

    子窗口HWND2只能得到鼠标消息,键盘消息得不到,
    我觉得可能关键在于窗口style上。
      

  6.   

    DC hdc = GetWindowDC(hwnd);
    BeginPaint(hwnd,&ps);
    我没记错的话,应该这么用:
    HDC hdc = BeginPaint(hwnd,&ps);另外,建议搂主还是用switch/case来组织消息处理的过程,否则看起来乱乱的,没有头绪。
      

  7.   


    if(msg==WM_DESTROY)
    { PostQuitMessage(0);return  0;}
    if(msg==WM_KEYDOWN||msg==WM_LBUTTONDOWN)
    {MessageBox(hwnd,"2222222222222","22222222222222",MB_OK);}
    if(msg==WM_PAINT)
    {
    PAINTSTRUCT ps;HDC hdc;
    BeginPaint(hwnd,&ps);
    char * str="22222222222";
    TextOut(hdc,0,0,str,strlen(str));
    }
    int n=DefWindowProc(hwnd, msg, wParam, lParam);
    return n;
       //////////////////
    把这里的代码好好组织一下
    还有在WM_PAINT里,很少用GetDc来取得DC