| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1238 人关注过本帖
标题:关于WM_PAINT的问题
只看楼主 加入收藏
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:9 
关于WM_PAINT的问题
请教各位前辈,我想在win32程序的窗体上画直线,并且要在窗体载入时自动画上去。因此在WM_PAINT消息中使用了MoveToEx和LineTo来实现,但窗体一重画,本来在窗体上的控件就被擦掉了,请问这种问题该如此解决?或者说如何保持控件不被擦除。十分谢谢。
对了,我需要不用MFC解决的办法。
搜索更多相关主题的帖子: 如何 
2014-12-20 22:32
天使梦魔
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:37
帖 子:564
专家分:2754
注 册:2007-8-29
收藏
得分:1 
那就放在一起,重画控件和图形
2014-12-21 15:10
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
哦,谢谢。那请问控件该如何重画呢?我是用CreatWindow()函数添加的控件,但不知道要重画该怎么写。还望赐教。
2014-12-21 21:33
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:9 
无代码,没法瞎猜
2014-12-22 09:49
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
谢谢关注。是了,把代码贴出来
#include <windows.h>
#define IDM_START 101
#define IDM_EXIT 102
#define IDM_NORMAL 103
#define IDM_DIFFICULT 104
/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HDC MainHDC;
/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";
//菜单项句柄定义
HMENU hMenu;HMENU hMenuGame;HMENU hMenuSetting;
HWND hSubWindow;
//菜单项句柄定义结束
int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "MyProject",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           300,                 /* The programs width */
           500,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        case WM_CREATE:
            //HWND hEdt;
            //hEdt = CreateWindow(TEXT("EDIT"), TEXT("填写新窗口标题"), WS_CHILD|WS_VISIBLE|WS_BORDER, 50, 50, 200, 40, hwnd, NULL, NULL, NULL);
            hMenu=CreateMenu();hMenuGame=CreateMenu();hMenuSetting=CreateMenu();
            //Game菜单
            AppendMenu(hMenuGame,MF_STRING,IDM_START,TEXT("Start"));
            AppendMenu(hMenuGame,MF_STRING,IDM_EXIT,TEXT("Exit"));
            AppendMenu(hMenu,MF_POPUP,(LONG)hMenuGame,TEXT("Game"));
            //Setting菜单
            AppendMenu(hMenuSetting,MF_STRING,IDM_NORMAL,TEXT("Normal"));
            AppendMenu(hMenuSetting,MF_STRING,IDM_DIFFICULT,TEXT("Difficult"));
            AppendMenu(hMenu,MF_POPUP,(LONG)hMenuSetting,TEXT("Setting"));
            //加载做好的菜单
            SetMenu(hwnd,hMenu);
            //制作用于显示操作区的图片框
            hSubWindow = CreateWindow(TEXT("Static"), TEXT("填写新窗口标题"), WS_CHILD|WS_VISIBLE|WS_BORDER|SS_BITMAP, 60, 40, 160, 340, hwnd, NULL, NULL, NULL);//此控件在WM_PAINT消息处理之后被擦掉。请问怎样可以不被擦掉。
            break;
        case WM_PAINT://添加了这个消息处理之后,上面的hSubWindow句柄所生成的图片框就不在了
            MainHDC=GetDC(hwnd);
            MoveToEx(MainHDC,10,10,NULL);
            LineTo(MainHDC,40,40);
            ReleaseDC(hwnd,MainHDC);
            break;
        case WM_COMMAND:
            switch(HIWORD(wParam))
            {
            case 0://0为菜单的HIWORD
                switch(LOWORD(wParam))
                {
                case IDM_EXIT:
                    exit(0);break;
                }
                break;
            }
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
2014-12-22 13:32
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
程序代码:
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
            MoveToEx(hdc,10,10,NULL);
            LineTo(hdc,40,40);
            EndPaint(hwnd, &ps);
        }
        break;

其他错误(如果你不认为是错误的话,那就算了)自己改,
比如 char szClassName[ ] = "WindowsApp"; 应当为 TCHAR szClassName[] = TEXT("WindowsApp");
"MyProject" 应当为 TEXT("MyProject")
2014-12-22 15:44
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
答案太给力了,非常感谢!终于了解窗体绘制线条的完整部骤了。
2014-12-22 18:29
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
回复 6楼 rjsp
还想请教一下,若想改变线条颜色或粗细等,应该如何做呢?
2014-12-22 18:43
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
程序代码:
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
            HPEN hpen = CreatePen( PS_SOLID, 3, RGB(255,0,0) );
            HPEN hpen_old = SelectObject( hdc, hpen );
            MoveToEx(hdc,10,10,NULL);
            LineTo(hdc,40,40);
            SelectObject( hdc, hpen_old );
            DeleteObject( hpen );
            EndPaint(hwnd, &ps);
        }
        break;

你最好找本书看看
2014-12-23 08:34
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
回复 9楼 rjsp
非常感谢。主要是不想靠MFC,一直想物色一本C++ API的教程。如果您知道较好的关于C++ API入门的书也请多多推荐哈。呵呵
2014-12-24 09:50
快速回复:关于WM_PAINT的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017942 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved